diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 737b174e7367..0ec491241272 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -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: @@ -2470,6 +2471,11 @@ export default (superClass: Class): Class => 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, diff --git a/packages/babel-parser/test/fixtures/typescript/class/declare-accessor/input.ts b/packages/babel-parser/test/fixtures/typescript/class/declare-accessor/input.ts new file mode 100644 index 000000000000..6ca324a281db --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/declare-accessor/input.ts @@ -0,0 +1,4 @@ +class Foo { + declare get foo() + declare set foo(v) +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/declare-accessor/output.json b/packages/babel-parser/test/fixtures/typescript/class/declare-accessor/output.json new file mode 100644 index 000000000000..5cd5577a3aa0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/declare-accessor/output.json @@ -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": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/input.ts b/packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/input.ts new file mode 100644 index 000000000000..fe1189b6ea44 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/input.ts @@ -0,0 +1,4 @@ +class C { + declare get: string + declare set: string; +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/output.json b/packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/output.json new file mode 100644 index 000000000000..5c5e5704397b --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/declare-get-set-field/output.json @@ -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": [] + } +} \ No newline at end of file