[ts] raise SyntaxError for declare before getter/setter (#13143)
* fix: raise `SyntaxError` for `declare` before getter/setter * fix: allow `declare` when class property name is `get` or `set`
This commit is contained in:
parent
e92d6b2458
commit
368bf893fa
@ -67,6 +67,7 @@ const TSErrors = Object.freeze({
|
|||||||
ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier",
|
ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier",
|
||||||
ConstructorHasTypeParameters:
|
ConstructorHasTypeParameters:
|
||||||
"Type parameters cannot appear on a constructor declaration.",
|
"Type parameters cannot appear on a constructor declaration.",
|
||||||
|
DeclareAccessor: "'declare' is not allowed in %0ters.",
|
||||||
DeclareClassFieldHasInitializer:
|
DeclareClassFieldHasInitializer:
|
||||||
"Initializers are not allowed in ambient contexts.",
|
"Initializers are not allowed in ambient contexts.",
|
||||||
DeclareFunctionHasImplementation:
|
DeclareFunctionHasImplementation:
|
||||||
@ -2470,6 +2471,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
if (typeParameters && isConstructor) {
|
if (typeParameters && isConstructor) {
|
||||||
this.raise(typeParameters.start, TSErrors.ConstructorHasTypeParameters);
|
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;
|
if (typeParameters) method.typeParameters = typeParameters;
|
||||||
super.pushClassMethod(
|
super.pushClassMethod(
|
||||||
classBody,
|
classBody,
|
||||||
|
|||||||
4
packages/babel-parser/test/fixtures/typescript/class/declare-accessor/input.ts
vendored
Normal file
4
packages/babel-parser/test/fixtures/typescript/class/declare-accessor/input.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class Foo {
|
||||||
|
declare get foo()
|
||||||
|
declare set foo(v)
|
||||||
|
}
|
||||||
73
packages/babel-parser/test/fixtures/typescript/class/declare-accessor/output.json
vendored
Normal file
73
packages/babel-parser/test/fixtures/typescript/class/declare-accessor/output.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
4
packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/input.ts
vendored
Normal file
4
packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/input.ts
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class C {
|
||||||
|
declare get: string
|
||||||
|
declare set: string;
|
||||||
|
}
|
||||||
71
packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/output.json
vendored
Normal file
71
packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/output.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user