Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise SyntaxError for declare before getter/setter #13143

Merged
merged 2 commits into from Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -67,6 +67,7 @@ const TSErrors = Object.freeze({
ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier",
ConstructorHasTypeParameters:
"Type parameters cannot appear on a constructor declaration.",
DeclareAccessor: "'declare' is not allowed in %0ters.",
DeclareClassFieldHasInitializer:
"Initializers are not allowed in ambient contexts.",
DeclareFunctionHasImplementation:
Expand Down Expand Up @@ -2470,6 +2471,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (typeParameters && isConstructor) {
this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
}

// $FlowIgnore
if (method.declare && (method.kind === "get" || method.kind === "set")) {
this.raise(method.start, TSErrors.DeclareAccessor, method.kind);
}
if (typeParameters) method.typeParameters = typeParameters;
super.pushClassMethod(
classBody,
Expand Down
@@ -0,0 +1,4 @@
class Foo {
declare get foo()
declare set foo(v)
}
fedeci marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,73 @@
{
"type": "File",
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"errors": [
"SyntaxError: 'declare' is not allowed in getters. (2:2)",
"SyntaxError: 'declare' is not allowed in setters. (3:2)"
],
"program": {
"type": "Program",
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":54,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9},"identifierName":"Foo"},
"name": "Foo"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":10,"end":54,"loc":{"start":{"line":1,"column":10},"end":{"line":4,"column":1}},
"body": [
{
"type": "TSDeclareMethod",
"start":14,"end":31,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":19}},
"declare": true,
"static": false,
"key": {
"type": "Identifier",
"start":26,"end":29,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":17},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"kind": "get",
"id": null,
"generator": false,
"async": false,
"params": []
},
{
"type": "TSDeclareMethod",
"start":34,"end":52,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},
"declare": true,
"static": false,
"key": {
"type": "Identifier",
"start":46,"end":49,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":17},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"kind": "set",
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":50,"end":51,"loc":{"start":{"line":3,"column":18},"end":{"line":3,"column":19},"identifierName":"v"},
"name": "v"
}
]
}
]
}
}
],
"directives": []
}
}
@@ -0,0 +1,4 @@
class C {
declare get: string
declare set: string;
}
@@ -0,0 +1,71 @@
{
"type": "File",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"program": {
"type": "Program",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":56,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"},
"name": "C"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":56,"loc":{"start":{"line":1,"column":8},"end":{"line":4,"column":1}},
"body": [
{
"type": "ClassProperty",
"start":12,"end":31,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":21}},
"declare": true,
"static": false,
"key": {
"type": "Identifier",
"start":20,"end":23,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13},"identifierName":"get"},
"name": "get"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":23,"end":31,"loc":{"start":{"line":2,"column":13},"end":{"line":2,"column":21}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":25,"end":31,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":21}}
}
},
"value": null
},
{
"type": "ClassProperty",
"start":34,"end":54,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":22}},
"declare": true,
"static": false,
"key": {
"type": "Identifier",
"start":42,"end":45,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":13},"identifierName":"set"},
"name": "set"
},
"computed": false,
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":45,"end":53,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":21}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":47,"end":53,"loc":{"start":{"line":3,"column":15},"end":{"line":3,"column":21}}
}
},
"value": null
}
]
}
}
],
"directives": []
}
}