Skip to content

Commit 663261b

Browse files
authoredMar 14, 2024··
fix(es/parser): Allow export after decorators when valid (#8739)
**Related issue:** - Closes #5276
1 parent 10f2506 commit 663261b

File tree

4 files changed

+122
-2
lines changed

4 files changed

+122
-2
lines changed
 

‎crates/swc_ecma_parser/src/parser/class_and_fn.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,14 @@ impl<I: Tokens> Parser<I> {
263263
}
264264

265265
if is!(self, "export") {
266-
if !allow_export {
266+
if !self.ctx().in_class && !self.ctx().in_function && !allow_export {
267267
syntax_error!(self, self.input.cur_span(), SyntaxError::ExportNotAllowed);
268268
}
269269

270-
if !self.syntax().decorators_before_export() {
270+
if !self.ctx().in_class
271+
&& !self.ctx().in_function
272+
&& !self.syntax().decorators_before_export()
273+
{
271274
syntax_error!(self, span!(self, start), SyntaxError::DecoratorOnExport);
272275
}
273276
} else if !is!(self, "class") {

‎crates/swc_ecma_parser/tests/js.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ where
9393
Syntax::Es(EsConfig {
9494
explicit_resource_management: true,
9595
import_attributes: true,
96+
decorators: true,
9697
..Default::default()
9798
}),
9899
EsVersion::Es2015,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export class MyClass {
2+
@MyDecorator()
3+
export = true;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"type": "Module",
3+
"span": {
4+
"start": 1,
5+
"end": 63,
6+
"ctxt": 0
7+
},
8+
"body": [
9+
{
10+
"type": "ExportDeclaration",
11+
"span": {
12+
"start": 1,
13+
"end": 63,
14+
"ctxt": 0
15+
},
16+
"declaration": {
17+
"type": "ClassDeclaration",
18+
"identifier": {
19+
"type": "Identifier",
20+
"span": {
21+
"start": 14,
22+
"end": 21,
23+
"ctxt": 0
24+
},
25+
"value": "MyClass",
26+
"optional": false
27+
},
28+
"declare": false,
29+
"span": {
30+
"start": 8,
31+
"end": 63,
32+
"ctxt": 0
33+
},
34+
"decorators": [],
35+
"body": [
36+
{
37+
"type": "ClassProperty",
38+
"span": {
39+
"start": 28,
40+
"end": 61,
41+
"ctxt": 0
42+
},
43+
"key": {
44+
"type": "Identifier",
45+
"span": {
46+
"start": 47,
47+
"end": 53,
48+
"ctxt": 0
49+
},
50+
"value": "export",
51+
"optional": false
52+
},
53+
"value": {
54+
"type": "BooleanLiteral",
55+
"span": {
56+
"start": 56,
57+
"end": 60,
58+
"ctxt": 0
59+
},
60+
"value": true
61+
},
62+
"typeAnnotation": null,
63+
"isStatic": false,
64+
"decorators": [
65+
{
66+
"type": "Decorator",
67+
"span": {
68+
"start": 28,
69+
"end": 42,
70+
"ctxt": 0
71+
},
72+
"expression": {
73+
"type": "CallExpression",
74+
"span": {
75+
"start": 29,
76+
"end": 42,
77+
"ctxt": 0
78+
},
79+
"callee": {
80+
"type": "Identifier",
81+
"span": {
82+
"start": 29,
83+
"end": 40,
84+
"ctxt": 0
85+
},
86+
"value": "MyDecorator",
87+
"optional": false
88+
},
89+
"arguments": [],
90+
"typeArguments": null
91+
}
92+
}
93+
],
94+
"accessibility": null,
95+
"isAbstract": false,
96+
"isOptional": false,
97+
"isOverride": false,
98+
"readonly": false,
99+
"declare": false,
100+
"definite": false
101+
}
102+
],
103+
"superClass": null,
104+
"isAbstract": false,
105+
"typeParams": null,
106+
"superTypeParams": null,
107+
"implements": []
108+
}
109+
}
110+
],
111+
"interpreter": null
112+
}

0 commit comments

Comments
 (0)
Please sign in to comment.