From f6067d81fa4905f63493a06f170b8a3851f8d6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 1 Mar 2021 23:19:01 +0100 Subject: [PATCH] Handle computed methods --- .../src/plugins/typescript/index.js | 7 ++- .../input.ts | 3 + .../output.json | 61 +++++++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index ee2209e4b765..5251d5c299d8 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2938,15 +2938,18 @@ export default (superClass: Class): Class => parseMethod(...args: any[]) { const method = super.parseMethod(...args); - if ((method: any).abstract) { + if (method.abstract) { const hasBody = this.hasPlugin("estree") ? !!method.value.body : !!method.body; if (hasBody) { + const { key } = method; this.raise( method.start, TSErrors.AbstractMethodHasImplementation, - (method: any).key.name, + key.type === "Identifier" + ? key.name + : `[${this.input.slice(key.start, key.end)}]`, ); } } diff --git a/packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/input.ts b/packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/input.ts new file mode 100644 index 000000000000..c1c84cdba8ef --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/input.ts @@ -0,0 +1,3 @@ +abstract class Foo { + abstract [foo()]() {} +} diff --git a/packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/output.json b/packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/output.json new file mode 100644 index 000000000000..35955f48a99a --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/class/abstract-method-with-body-computed/output.json @@ -0,0 +1,61 @@ +{ + "type": "File", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: Method '[foo()]' cannot have an implementation because it is marked abstract. (2:2)" + ], + "program": { + "type": "Program", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "abstract": true, + "id": { + "type": "Identifier", + "start":15,"end":18,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":18},"identifierName":"Foo"}, + "name": "Foo" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":19,"end":46,"loc":{"start":{"line":1,"column":19},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ClassMethod", + "start":23,"end":44,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":23}}, + "abstract": true, + "static": false, + "computed": true, + "key": { + "type": "CallExpression", + "start":33,"end":38,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":17}}, + "callee": { + "type": "Identifier", + "start":33,"end":36,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":15},"identifierName":"foo"}, + "name": "foo" + }, + "arguments": [] + }, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":42,"end":44,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":23}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file