Fix parsing of class member named declare (#822)

This commit is contained in:
강동윤 2020-06-04 02:52:40 +09:00 committed by GitHub
parent c7e4783837
commit d82e6ab69d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 226 additions and 2 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "swc_ecma_parser"
version = "0.24.4"
version = "0.24.5"
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
license = "Apache-2.0/MIT"
repository = "https://github.com/swc-project/swc.git"

View File

@ -299,7 +299,55 @@ impl<'a, I: Tokens> Parser<'a, I> {
let decorators = self.parse_decorators(false)?;
if self.syntax().typescript() && eat!("declare") {
self.emit_err(make_span(self.input.prev_span()), SyntaxError::TS1031);
let accessibility = if self.input.syntax().typescript() {
self.parse_access_modifier()?
} else {
None
};
// Handle declare(){}
if self.is_class_method()? {
let key = Either::Right(PropName::Ident(Ident::new(
js_word!("declare"),
span!(start),
)));
let is_optional = self.input.syntax().typescript() && eat!('?');
return self.make_method(
|p| p.parse_unique_formal_params(),
MakeMethodArgs {
start,
accessibility,
decorators,
is_abstract: false,
is_optional,
is_async: false,
is_generator: false,
static_token: None,
key,
kind: MethodKind::Method,
},
);
} else if self.is_class_property()? {
// Property named `declare`
let key = Either::Right(PropName::Ident(Ident::new(
js_word!("declare"),
span!(start),
)));
let is_optional = self.input.syntax().typescript() && eat!('?');
return self.make_property(
start,
decorators,
accessibility,
key,
false,
is_optional,
false,
false,
);
} else {
self.emit_err(make_span(self.input.prev_span()), SyntaxError::TS1031);
}
}
let accessibility = if self.input.syntax().typescript() {

View File

@ -0,0 +1,3 @@
class A {
declare() {}
}

View File

@ -0,0 +1,85 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 28,
"ctxt": 0
},
"body": [
{
"type": "ClassDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 6,
"end": 7,
"ctxt": 0
},
"value": "A",
"typeAnnotation": null,
"optional": false
},
"declare": false,
"span": {
"start": 0,
"end": 28,
"ctxt": 0
},
"decorators": [],
"body": [
{
"type": "ClassMethod",
"span": {
"start": 14,
"end": 26,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 14,
"end": 21,
"ctxt": 0
},
"value": "declare",
"typeAnnotation": null,
"optional": false
},
"function": {
"params": [],
"decorators": [],
"span": {
"start": 14,
"end": 26,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 24,
"end": 26,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
},
"kind": "method",
"isStatic": false,
"accessibility": null,
"isAbstract": false,
"isOptional": false
}
],
"superClass": null,
"isAbstract": false,
"typeParams": null,
"superTypeParams": null,
"implements": []
}
],
"interpreter": null
}

View File

@ -0,0 +1,3 @@
class A {
declare() {}
}

View File

@ -0,0 +1,85 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 28,
"ctxt": 0
},
"body": [
{
"type": "ClassDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 6,
"end": 7,
"ctxt": 0
},
"value": "A",
"typeAnnotation": null,
"optional": false
},
"declare": false,
"span": {
"start": 0,
"end": 28,
"ctxt": 0
},
"decorators": [],
"body": [
{
"type": "ClassMethod",
"span": {
"start": 14,
"end": 26,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 14,
"end": 21,
"ctxt": 0
},
"value": "declare",
"typeAnnotation": null,
"optional": false
},
"function": {
"params": [],
"decorators": [],
"span": {
"start": 14,
"end": 26,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 24,
"end": 26,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
},
"kind": "method",
"isStatic": false,
"accessibility": null,
"isAbstract": false,
"isOptional": false
}
],
"superClass": null,
"isAbstract": false,
"typeParams": null,
"superTypeParams": null,
"implements": []
}
],
"interpreter": null
}