fix(es/parser): Handle class members with accessor as the name (#7046)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/7042.
This commit is contained in:
suxin2017 2023-03-10 14:26:14 +08:00 committed by GitHub
parent 740a78ba88
commit 9acaf4deeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 269 additions and 0 deletions

View File

@ -463,6 +463,56 @@ impl<I: Tokens> Parser<I> {
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() {

View File

@ -0,0 +1,5 @@
class SomeClass {
accessor() {
return 'accessor';
}
}

View File

@ -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
}

View File

@ -0,0 +1,3 @@
class SomeClass {
accessor: 'accessor'
}

View File

@ -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
}

View File

@ -0,0 +1,5 @@
class SomeClass {
accessor() {
return 'accessor';
}
}

View File

@ -0,0 +1,5 @@
class SomeClass {
accessor() {
return 'accessor';
}
}

View File

@ -0,0 +1,3 @@
class SomeClass {
accessor accessor: 'accessor'
}

View File

@ -0,0 +1,3 @@
class SomeClass {
accessor accessor;
}