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 1 commit
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
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -32,6 +32,7 @@ export const ErrorMessages = Object.freeze({
ConstructorIsAsync: "Constructor can't be an async function",
ConstructorIsGenerator: "Constructor can't be a generator",
DeclarationMissingInitializer: "%0 require an initialization value",
DeclareAccessor: "'declare' is not allowed in %0ters.",
DecoratorBeforeExport:
"Decorators must be placed *before* the 'export' keyword. You can set the 'decoratorsBeforeExport' option to false to use the 'export @decorator class {}' syntax",
DecoratorConstructor:
Expand Down
9 changes: 7 additions & 2 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1509,7 +1509,14 @@ export default class StatementParser extends ExpressionParser {
// https://tc39.es/proposal-class-fields/#prod-ClassElementName
parseClassElementName(member: N.ClassMember): N.Expression | N.Identifier {
const key = this.parsePropertyName(member, /* isPrivateNameAllowed */ true);
this.checkClassElementName(member, key);
return key;
}

checkClassElementName(
member: N.ClassMember,
key: N.Expression | N.Identifier,
) {
if (
!member.computed &&
member.static &&
Expand All @@ -1525,8 +1532,6 @@ export default class StatementParser extends ExpressionParser {
) {
this.raise(key.start, Errors.ConstructorClassPrivateField);
}

return key;
}

parseClassStaticBlock(
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -2191,6 +2191,20 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return this.tsParseModifier(["public", "protected", "private"]);
}

checkClassElementName(
member: N.ClassMember,
key: N.Expression | N.Identifier,
) {
if (
// $FlowIgnore
member.declare &&
(key.name === "get" || key.name === "set")
) {
this.raise(member.start, Errors.DeclareAccessor, key.name);
}
super.checkClassElementName(member, key);
}

parseClassMember(
classBody: N.ClassBody,
member: any,
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": []
}
}