diff --git a/crates/swc_ecma_parser/src/parser/class_and_fn.rs b/crates/swc_ecma_parser/src/parser/class_and_fn.rs index f46bdbc441a3..d915acabe811 100644 --- a/crates/swc_ecma_parser/src/parser/class_and_fn.rs +++ b/crates/swc_ecma_parser/src/parser/class_and_fn.rs @@ -463,6 +463,56 @@ impl Parser { None }; + if let Some(accessor_token) = accessor_token { + // Handle accessor(){} + if self.is_class_method() { + let key = Key::Public(PropName::Ident(Ident::new( + js_word!("accessor"), + accessor_token, + ))); + let is_optional = self.input.syntax().typescript() && eat!(self, '?'); + return self.make_method( + |p| p.parse_unique_formal_params(), + MakeMethodArgs { + start, + accessibility, + decorators, + is_abstract: false, + is_optional, + is_override: false, + is_async: false, + is_generator: false, + static_token: None, + key, + kind: MethodKind::Method, + }, + ); + } else if self.is_class_property(/* asi */ true) + || (self.syntax().typescript() && is!(self, '?')) + { + // Property named `accessor` + + let key = Key::Public(PropName::Ident(Ident::new( + js_word!("accessor"), + accessor_token, + ))); + let is_optional = self.input.syntax().typescript() && eat!(self, '?'); + return self.make_property( + start, + decorators, + accessibility, + key, + false, + None, + is_optional, + false, + declare, + false, + false, + ); + } + } + if let Some(static_token) = static_token { // Handle static(){} if self.is_class_method() { diff --git a/crates/swc_ecma_parser/tests/typescript/issue-7042/case1/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-7042/case1/input.ts new file mode 100644 index 000000000000..11d12faa3fb6 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-7042/case1/input.ts @@ -0,0 +1,5 @@ +class SomeClass { + accessor() { + return 'accessor'; + } +} diff --git a/crates/swc_ecma_parser/tests/typescript/issue-7042/case1/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-7042/case1/input.ts.json new file mode 100644 index 000000000000..ed00f6cb2036 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-7042/case1/input.ts.json @@ -0,0 +1,103 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 70, + "ctxt": 0 + }, + "body": [ + { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 7, + "end": 16, + "ctxt": 0 + }, + "value": "SomeClass", + "optional": false + }, + "declare": false, + "span": { + "start": 1, + "end": 70, + "ctxt": 0 + }, + "decorators": [], + "body": [ + { + "type": "ClassMethod", + "span": { + "start": 23, + "end": 68, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 23, + "end": 31, + "ctxt": 0 + }, + "value": "accessor", + "optional": false + }, + "function": { + "params": [], + "decorators": [], + "span": { + "start": 23, + "end": 68, + "ctxt": 0 + }, + "body": { + "type": "BlockStatement", + "span": { + "start": 34, + "end": 68, + "ctxt": 0 + }, + "stmts": [ + { + "type": "ReturnStatement", + "span": { + "start": 44, + "end": 62, + "ctxt": 0 + }, + "argument": { + "type": "StringLiteral", + "span": { + "start": 51, + "end": 61, + "ctxt": 0 + }, + "value": "accessor", + "raw": "'accessor'" + } + } + ] + }, + "generator": false, + "async": false, + "typeParameters": null, + "returnType": null + }, + "kind": "method", + "isStatic": false, + "accessibility": null, + "isAbstract": false, + "isOptional": false, + "isOverride": false + } + ], + "superClass": null, + "isAbstract": false, + "typeParams": null, + "superTypeParams": null, + "implements": [] + } + ], + "interpreter": null +} diff --git a/crates/swc_ecma_parser/tests/typescript/issue-7042/case2/input.ts b/crates/swc_ecma_parser/tests/typescript/issue-7042/case2/input.ts new file mode 100644 index 000000000000..3ededbe63799 --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-7042/case2/input.ts @@ -0,0 +1,3 @@ +class SomeClass { + accessor: 'accessor' +} diff --git a/crates/swc_ecma_parser/tests/typescript/issue-7042/case2/input.ts.json b/crates/swc_ecma_parser/tests/typescript/issue-7042/case2/input.ts.json new file mode 100644 index 000000000000..50500d23adfe --- /dev/null +++ b/crates/swc_ecma_parser/tests/typescript/issue-7042/case2/input.ts.json @@ -0,0 +1,92 @@ +{ + "type": "Script", + "span": { + "start": 1, + "end": 45, + "ctxt": 0 + }, + "body": [ + { + "type": "ClassDeclaration", + "identifier": { + "type": "Identifier", + "span": { + "start": 7, + "end": 16, + "ctxt": 0 + }, + "value": "SomeClass", + "optional": false + }, + "declare": false, + "span": { + "start": 1, + "end": 45, + "ctxt": 0 + }, + "decorators": [], + "body": [ + { + "type": "ClassProperty", + "span": { + "start": 23, + "end": 43, + "ctxt": 0 + }, + "key": { + "type": "Identifier", + "span": { + "start": 23, + "end": 31, + "ctxt": 0 + }, + "value": "accessor", + "optional": false + }, + "value": null, + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 31, + "end": 43, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsLiteralType", + "span": { + "start": 33, + "end": 43, + "ctxt": 0 + }, + "literal": { + "type": "StringLiteral", + "span": { + "start": 33, + "end": 43, + "ctxt": 0 + }, + "value": "accessor", + "raw": "'accessor'" + } + } + }, + "isStatic": false, + "decorators": [], + "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 +} diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case1/input.ts b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case1/input.ts new file mode 100644 index 000000000000..11d12faa3fb6 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case1/input.ts @@ -0,0 +1,5 @@ +class SomeClass { + accessor() { + return 'accessor'; + } +} diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case1/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case1/output.js new file mode 100644 index 000000000000..23169dee3aa7 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case1/output.js @@ -0,0 +1,5 @@ +class SomeClass { + accessor() { + return 'accessor'; + } +} \ No newline at end of file diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case2/input.ts b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case2/input.ts new file mode 100644 index 000000000000..ab49b057e3aa --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case2/input.ts @@ -0,0 +1,3 @@ +class SomeClass { + accessor accessor: 'accessor' +} diff --git a/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case2/output.js b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case2/output.js new file mode 100644 index 000000000000..a9bc6d088f41 --- /dev/null +++ b/crates/swc_ecma_transforms_typescript/tests/fixture/issue-7042/case2/output.js @@ -0,0 +1,3 @@ +class SomeClass { + accessor accessor; +} \ No newline at end of file