Skip to content

Commit 31344cd

Browse files
authored
fix: correctly parse async arrow functions with generics (#19)
* claude code fix * more claude code fixes and tests * wip * fix arrow function with generic * add tests * fix type * fix for generic async arrow function proceeding an import statement * format * changeset
1 parent 18ff2c4 commit 31344cd

File tree

9 files changed

+463
-7
lines changed

9 files changed

+463
-7
lines changed

.changeset/flat-poets-beam.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/acorn-typescript': patch
3+
---
4+
5+
fix: correctly parse async arrow functions with generics

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ export function tsPlugin(options?: {
430430
return (
431431
base.type === 'Identifier' &&
432432
base.name === 'async' &&
433-
this.lastTokEndLoc.column === base.end &&
433+
this.lastTokEnd === base.end &&
434434
!this.canInsertSemicolon() &&
435435
base.end - base.start === 5 &&
436436
base.start === this.potentialArrowAt
@@ -3228,7 +3228,9 @@ export function tsPlugin(options?: {
32283228
return node.expression;
32293229
}
32303230

3231-
toAssignableList(exprList: any[], isBinding: boolean): any {
3231+
toAssignableList(exprList: any[] | null, isBinding: boolean): any {
3232+
if (!exprList) exprList = [];
3233+
32323234
for (let i = 0; i < exprList.length; i++) {
32333235
const expr = exprList[i];
32343236

@@ -4163,7 +4165,7 @@ export function tsPlugin(options?: {
41634165
override = modified.override;
41644166
readonly = modified.readonly;
41654167
if (allowModifiers === false && (accessibility || readonly || override)) {
4166-
this.raise(startLoc.start, TypeScriptError.UnexpectedParameterModifier);
4168+
this.raise(startLoc.column, TypeScriptError.UnexpectedParameterModifier);
41674169
}
41684170
}
41694171

src/middleware.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ export declare class AcornParseClass extends Parser {
4747
scopeStack: any[];
4848
inModule: any;
4949
undefinedExports: any;
50-
lastTokEndLoc: any;
51-
lastTokStartLoc: any;
50+
lastTokEndLoc: Position;
51+
lastTokStartLoc: Position;
5252
context: any[];
53-
endLoc: any;
54-
startLoc: any;
53+
endLoc: Position;
54+
startLoc: Position;
5555
potentialArrowInForAwait: boolean;
5656
type: TokenType & Record<string, any>;
5757
start: number;
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"type": "Program",
3+
"start": 0,
4+
"end": 73,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 3,
12+
"column": 46
13+
}
14+
},
15+
"body": [
16+
{
17+
"type": "ImportDeclaration",
18+
"start": 0,
19+
"end": 25,
20+
"loc": {
21+
"start": {
22+
"line": 1,
23+
"column": 0
24+
},
25+
"end": {
26+
"line": 1,
27+
"column": 25
28+
}
29+
},
30+
"importKind": "value",
31+
"specifiers": [],
32+
"source": {
33+
"type": "Literal",
34+
"start": 15,
35+
"end": 25,
36+
"loc": {
37+
"start": {
38+
"line": 1,
39+
"column": 15
40+
},
41+
"end": {
42+
"line": 1,
43+
"column": 25
44+
}
45+
},
46+
"value": "./config",
47+
"raw": "'./config'"
48+
}
49+
},
50+
{
51+
"type": "VariableDeclaration",
52+
"start": 27,
53+
"end": 73,
54+
"loc": {
55+
"start": {
56+
"line": 3,
57+
"column": 0
58+
},
59+
"end": {
60+
"line": 3,
61+
"column": 46
62+
}
63+
},
64+
"declarations": [
65+
{
66+
"type": "VariableDeclarator",
67+
"start": 33,
68+
"end": 72,
69+
"loc": {
70+
"start": {
71+
"line": 3,
72+
"column": 6
73+
},
74+
"end": {
75+
"line": 3,
76+
"column": 45
77+
}
78+
},
79+
"id": {
80+
"type": "Identifier",
81+
"start": 33,
82+
"end": 52,
83+
"loc": {
84+
"start": {
85+
"line": 3,
86+
"column": 6
87+
},
88+
"end": {
89+
"line": 3,
90+
"column": 25
91+
}
92+
},
93+
"name": "loadDataWithGeneric"
94+
},
95+
"init": {
96+
"type": "ArrowFunctionExpression",
97+
"start": 55,
98+
"end": 72,
99+
"loc": {
100+
"start": {
101+
"line": 3,
102+
"column": 28
103+
},
104+
"end": {
105+
"line": 3,
106+
"column": 45
107+
}
108+
},
109+
"typeParameters": {
110+
"type": "TSTypeParameterDeclaration",
111+
"start": 61,
112+
"end": 64,
113+
"loc": {
114+
"start": {
115+
"line": 3,
116+
"column": 34
117+
},
118+
"end": {
119+
"line": 3,
120+
"column": 37
121+
}
122+
},
123+
"params": [
124+
{
125+
"type": "TSTypeParameter",
126+
"start": 62,
127+
"end": 63,
128+
"loc": {
129+
"start": {
130+
"line": 3,
131+
"column": 35
132+
},
133+
"end": {
134+
"line": 3,
135+
"column": 36
136+
}
137+
},
138+
"name": "T"
139+
}
140+
]
141+
},
142+
"params": [],
143+
"id": null,
144+
"expression": false,
145+
"generator": false,
146+
"async": true,
147+
"body": {
148+
"type": "BlockStatement",
149+
"start": 70,
150+
"end": 72,
151+
"loc": {
152+
"start": {
153+
"line": 3,
154+
"column": 43
155+
},
156+
"end": {
157+
"line": 3,
158+
"column": 45
159+
}
160+
},
161+
"body": []
162+
}
163+
}
164+
}
165+
],
166+
"kind": "const"
167+
}
168+
],
169+
"sourceType": "module"
170+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {} from './config'
2+
3+
const loadDataWithGeneric = async <T>() => {};
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{
2+
"type": "Program",
3+
"start": 0,
4+
"end": 46,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 46
13+
}
14+
},
15+
"body": [
16+
{
17+
"type": "VariableDeclaration",
18+
"start": 0,
19+
"end": 46,
20+
"loc": {
21+
"start": {
22+
"line": 1,
23+
"column": 0
24+
},
25+
"end": {
26+
"line": 1,
27+
"column": 46
28+
}
29+
},
30+
"declarations": [
31+
{
32+
"type": "VariableDeclarator",
33+
"start": 6,
34+
"end": 45,
35+
"loc": {
36+
"start": {
37+
"line": 1,
38+
"column": 6
39+
},
40+
"end": {
41+
"line": 1,
42+
"column": 45
43+
}
44+
},
45+
"id": {
46+
"type": "Identifier",
47+
"start": 6,
48+
"end": 25,
49+
"loc": {
50+
"start": {
51+
"line": 1,
52+
"column": 6
53+
},
54+
"end": {
55+
"line": 1,
56+
"column": 25
57+
}
58+
},
59+
"name": "loadDataWithGeneric"
60+
},
61+
"init": {
62+
"type": "ArrowFunctionExpression",
63+
"start": 28,
64+
"end": 45,
65+
"loc": {
66+
"start": {
67+
"line": 1,
68+
"column": 28
69+
},
70+
"end": {
71+
"line": 1,
72+
"column": 45
73+
}
74+
},
75+
"typeParameters": {
76+
"type": "TSTypeParameterDeclaration",
77+
"start": 34,
78+
"end": 37,
79+
"loc": {
80+
"start": {
81+
"line": 1,
82+
"column": 34
83+
},
84+
"end": {
85+
"line": 1,
86+
"column": 37
87+
}
88+
},
89+
"params": [
90+
{
91+
"type": "TSTypeParameter",
92+
"start": 35,
93+
"end": 36,
94+
"loc": {
95+
"start": {
96+
"line": 1,
97+
"column": 35
98+
},
99+
"end": {
100+
"line": 1,
101+
"column": 36
102+
}
103+
},
104+
"name": "T"
105+
}
106+
]
107+
},
108+
"params": [],
109+
"id": null,
110+
"expression": false,
111+
"generator": false,
112+
"async": true,
113+
"body": {
114+
"type": "BlockStatement",
115+
"start": 43,
116+
"end": 45,
117+
"loc": {
118+
"start": {
119+
"line": 1,
120+
"column": 43
121+
},
122+
"end": {
123+
"line": 1,
124+
"column": 45
125+
}
126+
},
127+
"body": []
128+
}
129+
}
130+
}
131+
],
132+
"kind": "const"
133+
}
134+
],
135+
"sourceType": "module"
136+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const loadDataWithGeneric = async <T>() => {};

0 commit comments

Comments
 (0)