Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: decorator extractor #7

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions __fixtures__/schema-data/decorators/class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { admin, creator, external, internal } from '@hyperweb/decorators';
import { BigNumber } from 'jsd-std';

export interface State {
count: BigNumber;
}

export class Counter {
private state: State;

constructor(initialState: State) {
this.state.count = initialState;
}

// Public by default (no decorator needed)
@external
@admin
public getCount(): BigNumber {
return this.state.count;
}

// Only admin and creator can increment
@internal
@admin
public increment(amount: BigNumber): void {
this.state.count = this.state.count.add(amount);
}

// Only creator can decrement
@creator
public decrement(amount: BigNumber): void {
if (this.state.count.lt(amount)) {
throw new Error('Count cannot be negative');
}
this.state.count = this.state.count.sub(amount);
}
}
12 changes: 12 additions & 0 deletions __fixtures__/schema-data/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export * from './class';
export * from './object';
class MyClass {
@permission('debug', 'level')
@performance
async fetchData() {
// ... method implementation
}
}

export default MyClass;
export {MyClass};
30 changes: 30 additions & 0 deletions __fixtures__/schema-data/decorators/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { BigNumber } from "jsd-std";

interface State {
count: BigNumber;
}

// Core contract logic
export const start = (initialCount: BigNumber) => {
let state: State = {
count: initialCount
};

// HOW TO EVEN DO DECORATORS
return {
getCount: () => state.count,

increment: (amount: BigNumber) => {
state.count = state.count.add(amount);
return state.count;
},

decrement: (amount: BigNumber) => {
if (state.count.lt(amount)) {
throw new Error("Count cannot be negative");
}
state.count = state.count.sub(amount);
return state.count;
}
};
};
120 changes: 120 additions & 0 deletions __output__/schema-data/decorators.bundle.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions __output__/schema-data/decorators.bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

115 changes: 115 additions & 0 deletions __output__/schema-data/decorators.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"state": {
"type": "object",
"properties": {
"count": {
"type": "any"
}
}
},
"methods": [
{
"name": "fetchData",
"parameters": [],
"returnType": {
"type": "object",
"properties": {
"then": {
"type": "any"
},
"catch": {
"type": "any"
},
"finally": {
"type": "any"
},
"__@toStringTag@159": {
"type": "string"
}
}
}
}
],
"decorators": [
{
"name": "external",
"args": [],
"targetName": "getCount",
"targetType": "method",
"location": {
"file": "class.ts",
"line": 16,
"column": 3
}
},
{
"name": "admin",
"args": [],
"targetName": "getCount",
"targetType": "method",
"location": {
"file": "class.ts",
"line": 17,
"column": 3
}
},
{
"name": "internal",
"args": [],
"targetName": "increment",
"targetType": "method",
"location": {
"file": "class.ts",
"line": 23,
"column": 3
}
},
{
"name": "admin",
"args": [],
"targetName": "increment",
"targetType": "method",
"location": {
"file": "class.ts",
"line": 24,
"column": 3
}
},
{
"name": "creator",
"args": [],
"targetName": "decrement",
"targetType": "method",
"location": {
"file": "class.ts",
"line": 30,
"column": 3
}
},
{
"name": "permission",
"args": [
"debug",
"level"
],
"targetName": "fetchData",
"targetType": "method",
"location": {
"file": "index.ts",
"line": 4,
"column": 3
}
},
{
"name": "performance",
"args": [],
"targetName": "fetchData",
"targetType": "method",
"location": {
"file": "index.ts",
"line": 5,
"column": 3
}
}
]
}
3 changes: 2 additions & 1 deletion __output__/schema-data/inheritance-contract.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"type": "any"
}
}
]
],
"decorators": []
}
2 changes: 1 addition & 1 deletion __output__/schema-data/public-methods.bundle.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion __output__/schema-data/public-methods.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@
"type": "any"
}
}
]
],
"decorators": []
}
3 changes: 2 additions & 1 deletion __output__/schema-data/state-export.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@
}
}
},
"methods": []
"methods": [],
"decorators": []
}
Loading