Skip to content

Commit

Permalink
fix(es/parser): Allow export after decorators when valid (#8739)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #5276
  • Loading branch information
kdy1 committed Mar 14, 2024
1 parent 10f2506 commit 663261b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
7 changes: 5 additions & 2 deletions crates/swc_ecma_parser/src/parser/class_and_fn.rs
Expand Up @@ -263,11 +263,14 @@ impl<I: Tokens> Parser<I> {
}

if is!(self, "export") {
if !allow_export {
if !self.ctx().in_class && !self.ctx().in_function && !allow_export {
syntax_error!(self, self.input.cur_span(), SyntaxError::ExportNotAllowed);
}

if !self.syntax().decorators_before_export() {
if !self.ctx().in_class
&& !self.ctx().in_function
&& !self.syntax().decorators_before_export()
{
syntax_error!(self, span!(self, start), SyntaxError::DecoratorOnExport);
}
} else if !is!(self, "class") {
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_parser/tests/js.rs
Expand Up @@ -93,6 +93,7 @@ where
Syntax::Es(EsConfig {
explicit_resource_management: true,
import_attributes: true,
decorators: true,
..Default::default()
}),
EsVersion::Es2015,
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-5276/input.js
@@ -0,0 +1,4 @@
export class MyClass {
@MyDecorator()
export = true;
}
112 changes: 112 additions & 0 deletions crates/swc_ecma_parser/tests/js/issue-5276/input.js.json
@@ -0,0 +1,112 @@
{
"type": "Module",
"span": {
"start": 1,
"end": 63,
"ctxt": 0
},
"body": [
{
"type": "ExportDeclaration",
"span": {
"start": 1,
"end": 63,
"ctxt": 0
},
"declaration": {
"type": "ClassDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 14,
"end": 21,
"ctxt": 0
},
"value": "MyClass",
"optional": false
},
"declare": false,
"span": {
"start": 8,
"end": 63,
"ctxt": 0
},
"decorators": [],
"body": [
{
"type": "ClassProperty",
"span": {
"start": 28,
"end": 61,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 47,
"end": 53,
"ctxt": 0
},
"value": "export",
"optional": false
},
"value": {
"type": "BooleanLiteral",
"span": {
"start": 56,
"end": 60,
"ctxt": 0
},
"value": true
},
"typeAnnotation": null,
"isStatic": false,
"decorators": [
{
"type": "Decorator",
"span": {
"start": 28,
"end": 42,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 29,
"end": 42,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 29,
"end": 40,
"ctxt": 0
},
"value": "MyDecorator",
"optional": false
},
"arguments": [],
"typeArguments": null
}
}
],
"accessibility": null,
"isAbstract": false,
"isOptional": false,
"isOverride": false,
"readonly": false,
"declare": false,
"definite": false
}
],
"superClass": null,
"isAbstract": false,
"typeParams": null,
"superTypeParams": null,
"implements": []
}
}
],
"interpreter": null
}

0 comments on commit 663261b

Please sign in to comment.