Skip to content

Commit

Permalink
fix: allow declare when class property name is get or set
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Apr 17, 2021
1 parent 8b353c8 commit 2055132
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
1 change: 0 additions & 1 deletion packages/babel-parser/src/parser/error-message.js
Expand Up @@ -32,7 +32,6 @@ 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: 2 additions & 7 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -1509,14 +1509,7 @@ 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 @@ -1532,6 +1525,8 @@ export default class StatementParser extends ExpressionParser {
) {
this.raise(key.start, Errors.ConstructorClassPrivateField);
}

return key;
}

parseClassStaticBlock(
Expand Down
20 changes: 6 additions & 14 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 @@ -2191,20 +2192,6 @@ 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 Expand Up @@ -2484,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 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": []
}
}

0 comments on commit 2055132

Please sign in to comment.