From 4316410fd0ad89490bc0ddaa862c0894882f56d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 30 Dec 2019 17:09:51 -0500 Subject: [PATCH 1/3] fix: scope.inAsync should exclude reference in class property initializers --- packages/babel-parser/src/util/scope.js | 13 +- .../await-inside-class-methods/input.js | 3 + .../await-inside-class-methods/output.json | 210 ++++++++++++++ .../input.js | 3 + .../options.json | 3 + .../output.json | 187 ++++++++++++ .../input.js | 6 + .../options.json | 3 + .../output.json | 274 ++++++++++++++++++ .../await-in-async-in-class-property/input.js | 3 + .../options.json | 3 + .../output.json | 190 ++++++++++++ .../await-in-class-property-in-async/input.js | 6 + .../options.json | 3 + .../output.json | 260 +++++++++++++++++ 15 files changed, 1166 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/input.js create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/input.js create mode 100644 packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/options.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/output.json diff --git a/packages/babel-parser/src/util/scope.js b/packages/babel-parser/src/util/scope.js index f367ae5e76b6..f40aa67a1b98 100644 --- a/packages/babel-parser/src/util/scope.js +++ b/packages/babel-parser/src/util/scope.js @@ -56,7 +56,18 @@ export default class ScopeHandler { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0; } get inAsync() { - return (this.currentVarScope().flags & SCOPE_ASYNC) > 0; + for (let i = this.scopeStack.length - 1; ; i--) { + const scope = this.scopeStack[i]; + const isVarScope = scope.flags & SCOPE_VAR; + const isClassScope = scope.flags & SCOPE_CLASS; + if (isClassScope && !isVarScope) { + // If it meets a class scope before a var scope, it means it is a class property initializer + // which does not have an [Await] parameter in its grammar + return false; + } else if (isVarScope) { + return (scope.flags & SCOPE_ASYNC) > 0; + } + } } get allowSuper() { return (this.currentThisScope().flags & SCOPE_SUPER) > 0; diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/input.js b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/input.js new file mode 100644 index 000000000000..7a48dfc20087 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/input.js @@ -0,0 +1,3 @@ +() => class { + async m() { await 42 } +} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/output.json new file mode 100644 index 000000000000..04024e014271 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-class-methods/output.json @@ -0,0 +1,210 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "ClassExpression", + "start": 6, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 12, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 16, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "m" + }, + "name": "m" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 26, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 28, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "expression": { + "type": "AwaitExpression", + "start": 28, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 14 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 34, + "end": 36, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + } + ], + "directives": [] + } + } + ] + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/input.js new file mode 100644 index 000000000000..f01601af7a3a --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/input.js @@ -0,0 +1,3 @@ +class C { + #p = async () => await 42; +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/options.json new file mode 100644 index 000000000000..f26e916957c8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classPrivateProperties"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/output.json new file mode 100644 index 000000000000..ebac6a0e3b90 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-async-in-private-property/output.json @@ -0,0 +1,187 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "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": 40, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 12, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 12, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 4 + } + }, + "id": { + "type": "Identifier", + "start": 13, + "end": 14, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 4 + }, + "identifierName": "p" + }, + "name": "p" + } + }, + "value": { + "type": "ArrowFunctionExpression", + "start": 17, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 7 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "AwaitExpression", + "start": 29, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 19 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 35, + "end": 37, + "loc": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 27 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/input.js b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/input.js new file mode 100644 index 000000000000..f2cbab75f49c --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/input.js @@ -0,0 +1,6 @@ +async () => { + class C { + // here await is an identifier reference + #p = await + 42; + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/options.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/options.json new file mode 100644 index 000000000000..f26e916957c8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classPrivateProperties"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/output.json b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/output.json new file mode 100644 index 000000000000..cc5429e9fce3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-private-properties/await-in-private-property-in-async/output.json @@ -0,0 +1,274 @@ +{ + "type": "File", + "start": 0, + "end": 97, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 97, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 97, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 97, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 12, + "end": 97, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "start": 16, + "end": 95, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "id": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 24, + "end": 95, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "body": [ + { + "type": "ClassPrivateProperty", + "start": 75, + "end": 91, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 20 + } + }, + "static": false, + "key": { + "type": "PrivateName", + "start": 75, + "end": 77, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 6 + } + }, + "id": { + "type": "Identifier", + "start": 76, + "end": 77, + "loc": { + "start": { + "line": 4, + "column": 5 + }, + "end": { + "line": 4, + "column": 6 + }, + "identifierName": "p" + }, + "name": "p" + } + }, + "value": { + "type": "BinaryExpression", + "start": 80, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 19 + } + }, + "left": { + "type": "Identifier", + "start": 80, + "end": 85, + "loc": { + "start": { + "line": 4, + "column": 9 + }, + "end": { + "line": 4, + "column": 14 + }, + "identifierName": "await" + }, + "name": "await" + }, + "operator": "+", + "right": { + "type": "NumericLiteral", + "start": 88, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 17 + }, + "end": { + "line": 4, + "column": 19 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " here await is an identifier reference", + "start": 30, + "end": 70, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 44 + } + } + } + ] + } + ] + } + } + ], + "directives": [] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " here await is an identifier reference", + "start": 30, + "end": 70, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 44 + } + } + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/input.js b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/input.js new file mode 100644 index 000000000000..388ae60d9f00 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/input.js @@ -0,0 +1,3 @@ +class C { + p = async () => await + 42; +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/options.json b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/options.json new file mode 100644 index 000000000000..9c27576d4ad0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/output.json b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/output.json new file mode 100644 index 000000000000..0031d27b4529 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-async-in-class-property/output.json @@ -0,0 +1,190 @@ +{ + "type": "File", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start": 0, + "end": 41, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "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": 41, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 12, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 12, + "end": 13, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 3 + }, + "identifierName": "p" + }, + "name": "p" + }, + "computed": false, + "value": { + "type": "ArrowFunctionExpression", + "start": 16, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "AwaitExpression", + "start": 28, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "argument": { + "type": "UnaryExpression", + "start": 34, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 24 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "operator": "+", + "prefix": true, + "argument": { + "type": "NumericLiteral", + "start": 36, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 26 + }, + "end": { + "line": 2, + "column": 28 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/input.js b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/input.js new file mode 100644 index 000000000000..84a94bd55ed8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/input.js @@ -0,0 +1,6 @@ +async () => { + class C { + // here await is an identifier reference + p = await + 42; + } +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/options.json b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/options.json new file mode 100644 index 000000000000..9c27576d4ad0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classProperties"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/output.json b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/output.json new file mode 100644 index 000000000000..34bc34826eca --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-properties/await-in-class-property-in-async/output.json @@ -0,0 +1,260 @@ +{ + "type": "File", + "start": 0, + "end": 96, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 96, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 96, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 96, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 12, + "end": 96, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 6, + "column": 1 + } + }, + "body": [ + { + "type": "ClassDeclaration", + "start": 16, + "end": 94, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "id": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 9 + }, + "identifierName": "C" + }, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 24, + "end": 94, + "loc": { + "start": { + "line": 2, + "column": 10 + }, + "end": { + "line": 5, + "column": 3 + } + }, + "body": [ + { + "type": "ClassProperty", + "start": 75, + "end": 90, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 19 + } + }, + "static": false, + "key": { + "type": "Identifier", + "start": 75, + "end": 76, + "loc": { + "start": { + "line": 4, + "column": 4 + }, + "end": { + "line": 4, + "column": 5 + }, + "identifierName": "p" + }, + "name": "p" + }, + "computed": false, + "value": { + "type": "BinaryExpression", + "start": 79, + "end": 89, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 18 + } + }, + "left": { + "type": "Identifier", + "start": 79, + "end": 84, + "loc": { + "start": { + "line": 4, + "column": 8 + }, + "end": { + "line": 4, + "column": 13 + }, + "identifierName": "await" + }, + "name": "await" + }, + "operator": "+", + "right": { + "type": "NumericLiteral", + "start": 87, + "end": 89, + "loc": { + "start": { + "line": 4, + "column": 16 + }, + "end": { + "line": 4, + "column": 18 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + }, + "leadingComments": [ + { + "type": "CommentLine", + "value": " here await is an identifier reference", + "start": 30, + "end": 70, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 44 + } + } + } + ] + } + ] + } + } + ], + "directives": [] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentLine", + "value": " here await is an identifier reference", + "start": 30, + "end": 70, + "loc": { + "start": { + "line": 3, + "column": 4 + }, + "end": { + "line": 3, + "column": 44 + } + } + } + ] +} \ No newline at end of file From 7e69f06767818a86e4df6f4e8537e76ada9d5d0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 30 Dec 2019 19:43:43 -0500 Subject: [PATCH 2/3] chore: add test on await in computed class property --- .../input.js | 3 + .../output.json | 177 ++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/input.js create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/output.json diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/input.js b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/input.js new file mode 100644 index 000000000000..d18a372142f7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/input.js @@ -0,0 +1,3 @@ +async () => class { + [await 42]() { } +} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/output.json new file mode 100644 index 000000000000..2518cd543d0f --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-computed-class-property/output.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "ClassExpression", + "start": 12, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start": 18, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ClassMethod", + "start": 22, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "static": false, + "computed": true, + "key": { + "type": "AwaitExpression", + "start": 23, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "argument": { + "type": "NumericLiteral", + "start": 29, + "end": 31, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 11 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + }, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 35, + "end": 38, + "loc": { + "start": { + "line": 2, + "column": 15 + }, + "end": { + "line": 2, + "column": 18 + } + }, + "body": [], + "directives": [] + } + } + ] + } + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From 56addd8a7f5166e52c53fb185ee17ba7b54e0192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 30 Dec 2019 20:27:34 -0500 Subject: [PATCH 3/3] fix flow error :( --- packages/babel-parser/src/util/scope.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/babel-parser/src/util/scope.js b/packages/babel-parser/src/util/scope.js index f40aa67a1b98..bc90be4d03cb 100644 --- a/packages/babel-parser/src/util/scope.js +++ b/packages/babel-parser/src/util/scope.js @@ -55,6 +55,8 @@ export default class ScopeHandler { get inGenerator() { return (this.currentVarScope().flags & SCOPE_GENERATOR) > 0; } + // the following loop always exit because SCOPE_PROGRAM is SCOPE_VAR + // $FlowIgnore get inAsync() { for (let i = this.scopeStack.length - 1; ; i--) { const scope = this.scopeStack[i];