Skip to content

Commit bd045a3

Browse files
committed
fix: decorator expression & fn return type
1 parent 6688727 commit bd045a3

File tree

9 files changed

+87
-1
lines changed

9 files changed

+87
-1
lines changed

.changeset/big-forks-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'esrap': patch
3+
---
4+
5+
fix: support function return type

.changeset/cold-grapes-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'esrap': patch
3+
---
4+
5+
fix: support decorator expression

src/handlers.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,9 @@ function handle_type_annotation(node, state) {
475475
case 'TSNeverKeyword':
476476
state.commands.push('never');
477477
break;
478+
case 'TSSymbolKeyword':
479+
state.commands.push('symbol');
480+
break;
478481
case 'TSArrayType':
479482
handle_type_annotation(node.elementType, state);
480483
state.commands.push('[]');
@@ -1251,7 +1254,11 @@ const handlers = {
12511254

12521255
state.commands.push('(');
12531256
sequence(node.value.params, state, false, handle);
1254-
state.commands.push(') ');
1257+
state.commands.push(')');
1258+
1259+
if (node.value.returnType) handle_type_annotation(node.value.returnType, state);
1260+
1261+
state.commands.push(' ');
12551262

12561263
if (node.value.body) handle(node.value.body, state);
12571264
},
@@ -1330,6 +1337,12 @@ const handlers = {
13301337
},
13311338

13321339
PropertyDefinition(node, state) {
1340+
if (node.decorators) {
1341+
for (const decorator of node.decorators) {
1342+
handle(decorator, state);
1343+
}
1344+
}
1345+
13331346
if (node.accessibility) {
13341347
state.commands.push(node.accessibility, ' ');
13351348
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Fields {
2+
static uuid() {
3+
return function (target: any, propertyKey: string | symbol) {
4+
Object.defineProperty(target, propertyKey, { value: 'random uuid...', writable: true });
5+
};
6+
}
7+
}
8+
9+
class User {
10+
@Fields.uuid()
11+
id: string;
12+
}
13+
14+
const u = new User();
15+
16+
console.log(u.id); // will print "random uuid..."
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": 3,
3+
"names": [],
4+
"sources": [
5+
"input.js"
6+
],
7+
"sourcesContent": [
8+
"class Fields {\n\tstatic uuid() {\n\t\treturn function (target: any, propertyKey: string | symbol) {\n\t\t\tObject.defineProperty(target, propertyKey, { value: 'random uuid...', writable: true });\n\t\t};\n\t}\n}\n\nclass User {\n\[email protected]()\n\tid: string;\n}\n\nconst u = new User();\nconsole.log(u.id); // will print \"random uuid...\"\n"
9+
],
10+
"mappings": "MAAM,MAAM,CAAC,CAAC;QACN,IAAI,GAAG,CAAC;mBACG,MAAW,OAAE,WAA4B,mBAAE,CAAC;GAC5D,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,WAAW,IAAI,KAAK,EAAE,gBAAgB,EAAE,QAAQ,EAAE,IAAI;EACrF,CAAC;CACF,CAAC;AACF,CAAC;;MAEK,IAAI,CAAC,CAAC;EACV,MAAM,CAAC,IAAI;CACZ,EAAE;AACH,CAAC;;MAEK,CAAC,OAAO,IAAI;;AAClB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE"
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Fields {
2+
static uuid() {
3+
return function (target: any, propertyKey: string | symbol) {
4+
Object.defineProperty(target, propertyKey, { value: 'random uuid...', writable: true });
5+
};
6+
}
7+
}
8+
9+
class User {
10+
@Fields.uuid()
11+
id: string;
12+
}
13+
14+
const u = new User();
15+
console.log(u.id); // will print "random uuid..."
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Fields {
2+
static cuid(): string {
3+
return 'random cuid...';
4+
}
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version": 3,
3+
"names": [],
4+
"sources": [
5+
"input.js"
6+
],
7+
"sourcesContent": [
8+
"class Fields {\n\tstatic cuid(): string {\n\t\treturn 'random cuid...'\n\t}\n}\n"
9+
],
10+
"mappings": "MAAM,MAAM,CAAC,CAAC;QACN,IAAI,WAAW,CAAC;SACf,gBAAgB;CACxB,CAAC;AACF,CAAC"
11+
}

test/samples/ts-return-type/input.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Fields {
2+
static cuid(): string {
3+
return 'random cuid...'
4+
}
5+
}

0 commit comments

Comments
 (0)