From 5d6295988bfd32c2254d6c376259def082066a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 5 Aug 2020 07:08:42 -0400 Subject: [PATCH] fix: do not eat get/set after async is parsed --- .../babel-parser/src/parser/expression.js | 2 +- .../async-shorthand-method/input.js | 4 ++ .../async-shorthand-method/output.json | 71 +++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/input.js create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 26527c8300bb..820620eb8395 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1754,7 +1754,7 @@ export default class ExpressionParser extends LValParser { } // get PropertyName[?Yield, ?Await] () { FunctionBody[~Yield, ~Await] } // set PropertyName[?Yield, ?Await] ( PropertySetParameterList ) { FunctionBody[~Yield, ~Await] } - if (prop.key.name === "get" || prop.key.name === "set") { + else if (prop.key.name === "get" || prop.key.name === "set") { isAccessor = true; isGenerator = this.eat(tt.star); // tt.star is allowed in `maybeAsyncOrAccessorProp`, we will throw in `parseObjectMethod` later prop.kind = prop.key.name; diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/input.js b/packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/input.js new file mode 100644 index 000000000000..a165b65ecfa5 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/input.js @@ -0,0 +1,4 @@ +({ + async get() {}, + async set() {} +}) diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/output.json new file mode 100644 index 000000000000..11c4f4bd7407 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/async-shorthand-method/output.json @@ -0,0 +1,71 @@ +{ + "type": "File", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}}, + "program": { + "type": "Program", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}}, + "expression": { + "type": "ObjectExpression", + "start":1,"end":39,"loc":{"start":{"line":1,"column":1},"end":{"line":4,"column":1}}, + "properties": [ + { + "type": "ObjectMethod", + "start":5,"end":19,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":16}}, + "method": true, + "key": { + "type": "Identifier", + "start":11,"end":14,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":11},"identifierName":"get"}, + "name": "get" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":17,"end":19,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":16}}, + "body": [], + "directives": [] + } + }, + { + "type": "ObjectMethod", + "start":23,"end":37,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":16}}, + "method": true, + "key": { + "type": "Identifier", + "start":29,"end":32,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":11},"identifierName":"set"}, + "name": "set" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":35,"end":37,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":16}}, + "body": [], + "directives": [] + } + } + ], + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file