Skip to content

Commit

Permalink
Handle computed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 1, 2021
1 parent 7e73d65 commit f6067d8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -2938,15 +2938,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>

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)}]`,
);
}
}
Expand Down
@@ -0,0 +1,3 @@
abstract class Foo {
abstract [foo()]() {}
}
@@ -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": []
}
}

0 comments on commit f6067d8

Please sign in to comment.