Skip to content

Commit 3f71776

Browse files
authored
fix(es/parser): Allow abstract method named accessor (#10327)
**Related issue:** - Closes #9835
1 parent 3ce9d81 commit 3f71776

File tree

4 files changed

+369
-1
lines changed

4 files changed

+369
-1
lines changed

Diff for: .changeset/fast-mails-sin.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_ecma_parser: patch
3+
swc_core: patch
4+
---
5+
6+
fix: typescript abstract class method named accessor

Diff for: crates/swc_ecma_parser/src/parser/class_and_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ impl<I: Tokens> Parser<I> {
683683
let accessor_token = accessor_token.or_else(|| {
684684
if self.syntax().auto_accessors() && readonly.is_none() {
685685
let start = cur_pos!(self);
686-
if eat!(self, "accessor") {
686+
if !peeked_is!(self, '(') && eat!(self, "accessor") {
687687
Some(span!(self, start))
688688
} else {
689689
None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
abstract class AbstractTest<T, V = unknown> {
2+
abstract id: string;
3+
abstract header: string;
4+
5+
abstract accessor(data: T): V;
6+
7+
printId() {
8+
return this.id;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
{
2+
"type": "Script",
3+
"span": {
4+
"start": 1,
5+
"end": 185
6+
},
7+
"body": [
8+
{
9+
"type": "ClassDeclaration",
10+
"identifier": {
11+
"type": "Identifier",
12+
"span": {
13+
"start": 16,
14+
"end": 28
15+
},
16+
"ctxt": 0,
17+
"value": "AbstractTest",
18+
"optional": false
19+
},
20+
"declare": false,
21+
"span": {
22+
"start": 1,
23+
"end": 185
24+
},
25+
"ctxt": 0,
26+
"decorators": [],
27+
"body": [
28+
{
29+
"type": "ClassProperty",
30+
"span": {
31+
"start": 51,
32+
"end": 71
33+
},
34+
"key": {
35+
"type": "Identifier",
36+
"span": {
37+
"start": 60,
38+
"end": 62
39+
},
40+
"value": "id"
41+
},
42+
"value": null,
43+
"typeAnnotation": {
44+
"type": "TsTypeAnnotation",
45+
"span": {
46+
"start": 62,
47+
"end": 70
48+
},
49+
"typeAnnotation": {
50+
"type": "TsKeywordType",
51+
"span": {
52+
"start": 64,
53+
"end": 70
54+
},
55+
"kind": "string"
56+
}
57+
},
58+
"isStatic": false,
59+
"decorators": [],
60+
"accessibility": null,
61+
"isAbstract": true,
62+
"isOptional": false,
63+
"isOverride": false,
64+
"readonly": false,
65+
"declare": false,
66+
"definite": false
67+
},
68+
{
69+
"type": "ClassProperty",
70+
"span": {
71+
"start": 76,
72+
"end": 100
73+
},
74+
"key": {
75+
"type": "Identifier",
76+
"span": {
77+
"start": 85,
78+
"end": 91
79+
},
80+
"value": "header"
81+
},
82+
"value": null,
83+
"typeAnnotation": {
84+
"type": "TsTypeAnnotation",
85+
"span": {
86+
"start": 91,
87+
"end": 99
88+
},
89+
"typeAnnotation": {
90+
"type": "TsKeywordType",
91+
"span": {
92+
"start": 93,
93+
"end": 99
94+
},
95+
"kind": "string"
96+
}
97+
},
98+
"isStatic": false,
99+
"decorators": [],
100+
"accessibility": null,
101+
"isAbstract": true,
102+
"isOptional": false,
103+
"isOverride": false,
104+
"readonly": false,
105+
"declare": false,
106+
"definite": false
107+
},
108+
{
109+
"type": "ClassMethod",
110+
"span": {
111+
"start": 106,
112+
"end": 136
113+
},
114+
"key": {
115+
"type": "Identifier",
116+
"span": {
117+
"start": 115,
118+
"end": 123
119+
},
120+
"value": "accessor"
121+
},
122+
"function": {
123+
"params": [
124+
{
125+
"type": "Parameter",
126+
"span": {
127+
"start": 124,
128+
"end": 131
129+
},
130+
"decorators": [],
131+
"pat": {
132+
"type": "Identifier",
133+
"span": {
134+
"start": 124,
135+
"end": 128
136+
},
137+
"ctxt": 0,
138+
"value": "data",
139+
"optional": false,
140+
"typeAnnotation": {
141+
"type": "TsTypeAnnotation",
142+
"span": {
143+
"start": 128,
144+
"end": 131
145+
},
146+
"typeAnnotation": {
147+
"type": "TsTypeReference",
148+
"span": {
149+
"start": 130,
150+
"end": 131
151+
},
152+
"typeName": {
153+
"type": "Identifier",
154+
"span": {
155+
"start": 130,
156+
"end": 131
157+
},
158+
"ctxt": 0,
159+
"value": "T",
160+
"optional": false
161+
},
162+
"typeParams": null
163+
}
164+
}
165+
}
166+
}
167+
],
168+
"decorators": [],
169+
"span": {
170+
"start": 106,
171+
"end": 136
172+
},
173+
"ctxt": 0,
174+
"body": null,
175+
"generator": false,
176+
"async": false,
177+
"typeParameters": null,
178+
"returnType": {
179+
"type": "TsTypeAnnotation",
180+
"span": {
181+
"start": 132,
182+
"end": 135
183+
},
184+
"typeAnnotation": {
185+
"type": "TsTypeReference",
186+
"span": {
187+
"start": 134,
188+
"end": 135
189+
},
190+
"typeName": {
191+
"type": "Identifier",
192+
"span": {
193+
"start": 134,
194+
"end": 135
195+
},
196+
"ctxt": 0,
197+
"value": "V",
198+
"optional": false
199+
},
200+
"typeParams": null
201+
}
202+
}
203+
},
204+
"kind": "method",
205+
"isStatic": false,
206+
"accessibility": null,
207+
"isAbstract": true,
208+
"isOptional": false,
209+
"isOverride": false
210+
},
211+
{
212+
"type": "ClassMethod",
213+
"span": {
214+
"start": 144,
215+
"end": 183
216+
},
217+
"key": {
218+
"type": "Identifier",
219+
"span": {
220+
"start": 144,
221+
"end": 151
222+
},
223+
"value": "printId"
224+
},
225+
"function": {
226+
"params": [],
227+
"decorators": [],
228+
"span": {
229+
"start": 144,
230+
"end": 183
231+
},
232+
"ctxt": 0,
233+
"body": {
234+
"type": "BlockStatement",
235+
"span": {
236+
"start": 154,
237+
"end": 183
238+
},
239+
"ctxt": 0,
240+
"stmts": [
241+
{
242+
"type": "ReturnStatement",
243+
"span": {
244+
"start": 162,
245+
"end": 177
246+
},
247+
"argument": {
248+
"type": "MemberExpression",
249+
"span": {
250+
"start": 169,
251+
"end": 176
252+
},
253+
"object": {
254+
"type": "ThisExpression",
255+
"span": {
256+
"start": 169,
257+
"end": 173
258+
}
259+
},
260+
"property": {
261+
"type": "Identifier",
262+
"span": {
263+
"start": 174,
264+
"end": 176
265+
},
266+
"value": "id"
267+
}
268+
}
269+
}
270+
]
271+
},
272+
"generator": false,
273+
"async": false,
274+
"typeParameters": null,
275+
"returnType": null
276+
},
277+
"kind": "method",
278+
"isStatic": false,
279+
"accessibility": null,
280+
"isAbstract": false,
281+
"isOptional": false,
282+
"isOverride": false
283+
}
284+
],
285+
"superClass": null,
286+
"isAbstract": true,
287+
"typeParams": {
288+
"type": "TsTypeParameterDeclaration",
289+
"span": {
290+
"start": 28,
291+
"end": 44
292+
},
293+
"parameters": [
294+
{
295+
"type": "TsTypeParameter",
296+
"span": {
297+
"start": 29,
298+
"end": 30
299+
},
300+
"name": {
301+
"type": "Identifier",
302+
"span": {
303+
"start": 29,
304+
"end": 30
305+
},
306+
"ctxt": 0,
307+
"value": "T",
308+
"optional": false
309+
},
310+
"in": false,
311+
"out": false,
312+
"const": false,
313+
"constraint": null,
314+
"default": null
315+
},
316+
{
317+
"type": "TsTypeParameter",
318+
"span": {
319+
"start": 32,
320+
"end": 43
321+
},
322+
"name": {
323+
"type": "Identifier",
324+
"span": {
325+
"start": 32,
326+
"end": 33
327+
},
328+
"ctxt": 0,
329+
"value": "V",
330+
"optional": false
331+
},
332+
"in": false,
333+
"out": false,
334+
"const": false,
335+
"constraint": null,
336+
"default": {
337+
"type": "TsKeywordType",
338+
"span": {
339+
"start": 36,
340+
"end": 43
341+
},
342+
"kind": "unknown"
343+
}
344+
}
345+
]
346+
},
347+
"superTypeParams": null,
348+
"implements": []
349+
}
350+
],
351+
"interpreter": null
352+
}

0 commit comments

Comments
 (0)