Skip to content

Commit c17d8cf

Browse files
authored
Merge pull request #62 from jycouet/feat-ts-improvements
fix: decorator expression & fn return type
2 parents 17130e4 + dd3e414 commit c17d8cf

File tree

15 files changed

+122
-1
lines changed

15 files changed

+122
-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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,15 @@ 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;
481+
case 'TSNullKeyword':
482+
state.commands.push('null');
483+
break;
484+
case 'TSUndefinedKeyword':
485+
state.commands.push('undefined');
486+
break;
478487
case 'TSArrayType':
479488
handle_type_annotation(node.elementType, state);
480489
state.commands.push('[]');
@@ -1273,7 +1282,11 @@ const handlers = {
12731282

12741283
state.commands.push('(');
12751284
sequence(node.value.params, state, false, handle);
1276-
state.commands.push(') ');
1285+
state.commands.push(')');
1286+
1287+
if (node.value.returnType) handle_type_annotation(node.value.returnType, state);
1288+
1289+
state.commands.push(' ');
12771290

12781291
if (node.value.body) handle(node.value.body, state);
12791292
},
@@ -1352,6 +1365,12 @@ const handlers = {
13521365
},
13531366

13541367
PropertyDefinition(node, state) {
1368+
if (node.decorators) {
1369+
for (const decorator of node.decorators) {
1370+
handle(decorator, state);
1371+
}
1372+
}
1373+
13551374
if (node.accessibility) {
13561375
state.commands.push(node.accessibility, ' ');
13571376
}
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let a: string | null = null;
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+
"let a: string | null = null"
9+
],
10+
"mappings": "IAAI,CAAgB,kBAAG,IAAI"
11+
}

test/samples/ts-null-keyword/input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let a: string | null = null
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)