From 4ec3b26062b3e14e6769e410aefe5063d9ed185a Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 17 Dec 2020 22:44:56 +0900 Subject: [PATCH 1/7] Parse await exp in non-async-function --- .../babel-parser/src/parser/expression.js | 51 ++++++++++++------- packages/babel-parser/src/parser/util.js | 6 +-- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 3ed03e0e1a7b..54f6356e05aa 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -55,6 +55,7 @@ import { newExpressionScope, } from "../util/expression-scope.js"; import { Errors } from "./error"; +import type State from "../tokenizer/state"; export default class ExpressionParser extends LValParser { // Forward-declaration: defined in statement.js @@ -492,8 +493,18 @@ export default class ExpressionParser extends LValParser { // Parse unary operators, both prefix and postfix. // https://tc39.es/ecma262/#prod-UnaryExpression parseMaybeUnary(refExpressionErrors: ?ExpressionErrors): N.Expression { - if (this.isContextual("await") && this.isAwaitAllowed()) { - return this.parseAwait(); + if (this.isContextual("await")) { + if (this.isAwaitAllowed()) { + return this.parseAwait(); + } else { + if (this.scope.inFunction) { + const lookahead = this.lookahead(); + if (lookahead.type.startsExpr && !this.isAmbiguousAwait(lookahead)) { + this.raise(this.state.start, Errors.AwaitNotInAsyncContext); + return this.parseAwait(); + } + } + } } const update = this.match(tt.incDec); const node = this.startNode(); @@ -2363,22 +2374,7 @@ export default class ExpressionParser extends LValParser { } if (!this.scope.inFunction && !this.options.allowAwaitOutsideFunction) { - if ( - this.hasPrecedingLineBreak() || - // All the following expressions are ambiguous: - // await + 0, await - 0, await ( 0 ), await [ 0 ], await / 0 /u, await `` - this.match(tt.plusMin) || - this.match(tt.parenL) || - this.match(tt.bracketL) || - this.match(tt.backQuote) || - // Sometimes the tokenizer generates tt.slash for regexps, and this is - // handler by parseExprAtom - this.match(tt.regexp) || - this.match(tt.slash) || - // This code could be parsed both as a modulo operator or as an intrinsic: - // await %x(0) - (this.hasPlugin("v8intrinsic") && this.match(tt.modulo)) - ) { + if (this.isAmbiguousAwait()) { this.ambiguousScriptDifferentAst = true; } else { this.sawUnambiguousESM = true; @@ -2392,6 +2388,25 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "AwaitExpression"); } + isAmbiguousAwait(state: State = this.state): boolean { + return ( + this.hasPrecedingLineBreak(state) || + // All the following expressions are ambiguous: + // await + 0, await - 0, await ( 0 ), await [ 0 ], await / 0 /u, await `` + state.type === tt.plusMin || + state.type === tt.parenL || + state.type === tt.bracketL || + state.type === tt.backQuote || + // Sometimes the tokenizer generates tt.slash for regexps, and this is + // handler by parseExprAtom + state.type === tt.regexp || + state.type === tt.slash || + // This code could be parsed both as a modulo operator or as an intrinsic: + // await %x(0) + (this.hasPlugin("v8intrinsic") && state.type === tt.modulo) + ); + } + // Parses yield expression inside generator. parseYield(): N.YieldExpression { diff --git a/packages/babel-parser/src/parser/util.js b/packages/babel-parser/src/parser/util.js index 9244cc983e51..2e3ac0e6b79d 100644 --- a/packages/babel-parser/src/parser/util.js +++ b/packages/babel-parser/src/parser/util.js @@ -90,10 +90,8 @@ export default class UtilParser extends Tokenizer { ); } - hasPrecedingLineBreak(): boolean { - return lineBreak.test( - this.input.slice(this.state.lastTokEnd, this.state.start), - ); + hasPrecedingLineBreak(state: State = this.state): boolean { + return lineBreak.test(this.input.slice(state.lastTokEnd, state.start)); } // TODO From 800d2f291165b604c0f5ffac7d2639d00b5d9de1 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Thu, 17 Dec 2020 22:45:34 +0900 Subject: [PATCH 2/7] Update tests --- .../es2017/async-functions/9/options.json | 3 - .../es2017/async-functions/9/output.json | 54 +++++++++++ .../options.json | 4 - .../output.json | 52 ++++++++++ .../input.js | 4 + .../output.json | 50 ++++++++++ .../options.json | 3 - .../output.json | 48 ++++++++++ .../options.json | 3 - .../output.json | 77 +++++++++++++++ .../input.js | 3 + .../output.json | 69 +++++++++++++ .../invalid-await/options.json | 7 +- .../invalid-await/output.json | 96 +++++++++++++++++++ 14 files changed, 455 insertions(+), 18 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/9/options.json create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json delete mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/options.json create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/input.js create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/output.json delete mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/options.json create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json delete mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/options.json create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/input.js create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/9/options.json b/packages/babel-parser/test/fixtures/es2017/async-functions/9/options.json deleted file mode 100644 index ff9c4d199005..000000000000 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/9/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token, expected \";\" (1:30)" -} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json new file mode 100644 index 000000000000..0ee180e1b892 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json @@ -0,0 +1,54 @@ +{ + "type": "File", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":40}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (1:24)" + ], + "program": { + "type": "Program", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":40}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":40}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":13,"end":20,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":20},"identifierName":"promise"}, + "name": "promise" + } + ], + "body": { + "type": "BlockStatement", + "start":22,"end":40,"loc":{"start":{"line":1,"column":22},"end":{"line":1,"column":40}}, + "body": [ + { + "type": "ExpressionStatement", + "start":24,"end":38,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":38}}, + "expression": { + "type": "AwaitExpression", + "start":24,"end":37,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":37}}, + "argument": { + "type": "Identifier", + "start":30,"end":37,"loc":{"start":{"line":1,"column":30},"end":{"line":1,"column":37},"identifierName":"promise"}, + "name": "promise" + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/options.json b/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/options.json deleted file mode 100644 index 3c9820fa55b8..000000000000 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "allowAwaitOutsideFunction": true, - "throws": "Unexpected token, expected \";\" (2:15)" -} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json new file mode 100644 index 000000000000..3f3e1cddf5b1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json @@ -0,0 +1,52 @@ +{ + "type": "File", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (2:9)" + ], + "program": { + "type": "Program", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"a"}, + "name": "a" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":13,"end":33,"loc":{"start":{"line":1,"column":13},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ReturnStatement", + "start":17,"end":31,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":16}}, + "argument": { + "type": "AwaitExpression", + "start":24,"end":31,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":16}}, + "argument": { + "type": "NumericLiteral", + "start":30,"end":31,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":16}}, + "extra": { + "rawValue": 1, + "raw": "1" + }, + "value": 1 + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/input.js b/packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/input.js new file mode 100644 index 000000000000..87023699f3e1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/input.js @@ -0,0 +1,4 @@ +function foo() { + await + foo; +} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/output.json new file mode 100644 index 000000000000..cbacec0cbd8a --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-identifier-with-line-break-in-function/output.json @@ -0,0 +1,50 @@ +{ + "type": "File", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "program": { + "type": "Program", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":15,"end":33,"loc":{"start":{"line":1,"column":15},"end":{"line":4,"column":1}}, + "body": [ + { + "type": "ExpressionStatement", + "start":19,"end":24,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7}}, + "expression": { + "type": "Identifier", + "start":19,"end":24,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":7},"identifierName":"await"}, + "name": "await" + } + }, + { + "type": "ExpressionStatement", + "start":27,"end":31,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":6}}, + "expression": { + "type": "Identifier", + "start":27,"end":30,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":5},"identifierName":"foo"}, + "name": "foo" + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/options.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/options.json deleted file mode 100644 index 6079f56395bd..000000000000 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token, expected \";\" (1:14)" -} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json new file mode 100644 index 000000000000..b12eebc08943 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json @@ -0,0 +1,48 @@ +{ + "type": "File", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (1:8)" + ], + "program": { + "type": "Program", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":6,"end":17,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":17}}, + "body": [ + { + "type": "ExpressionStatement", + "start":8,"end":15,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":15}}, + "expression": { + "type": "AwaitExpression", + "start":8,"end":15,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":15}}, + "argument": { + "type": "Identifier", + "start":14,"end":15,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":15},"identifierName":"x"}, + "name": "x" + } + } + } + ], + "directives": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/options.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/options.json deleted file mode 100644 index b260169ecd70..000000000000 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token, expected \",\" (2:25)" -} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json new file mode 100644 index 000000000000..c7d4e8fb8241 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json @@ -0,0 +1,77 @@ +{ + "type": "File", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (2:19)", + "SyntaxError: await is not allowed in async function parameters (2:19)" + ], + "program": { + "type": "Program", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":15,"end":18,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":18},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":21,"end":55,"loc":{"start":{"line":1,"column":21},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "FunctionDeclaration", + "start":25,"end":53,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":30}}, + "id": { + "type": "Identifier", + "start":34,"end":37,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":14},"identifierName":"bar"}, + "name": "bar" + }, + "generator": false, + "async": false, + "params": [ + { + "type": "AssignmentPattern", + "start":38,"end":49,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":26}}, + "left": { + "type": "Identifier", + "start":38,"end":39,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":16},"identifierName":"x"}, + "name": "x" + }, + "right": { + "type": "AwaitExpression", + "start":42,"end":49,"loc":{"start":{"line":2,"column":19},"end":{"line":2,"column":26}}, + "argument": { + "type": "NumericLiteral", + "start":48,"end":49,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":26}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":51,"end":53,"loc":{"start":{"line":2,"column":28},"end":{"line":2,"column":30}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/input.js b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/input.js new file mode 100644 index 000000000000..8b9c602112ed --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/input.js @@ -0,0 +1,3 @@ +function foo() { + await { foo }; +} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json new file mode 100644 index 000000000000..d64bc787b0ab --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json @@ -0,0 +1,69 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (2:2)" + ], + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":9,"end":12,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":12},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":15,"end":35,"loc":{"start":{"line":1,"column":15},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ExpressionStatement", + "start":19,"end":33,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":16}}, + "expression": { + "type": "AwaitExpression", + "start":19,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":15}}, + "argument": { + "type": "ObjectExpression", + "start":25,"end":32,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":15}}, + "properties": [ + { + "type": "ObjectProperty", + "start":27,"end":30,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13}}, + "extra": { + "shorthand": true + }, + "method": false, + "key": { + "type": "Identifier", + "start":27,"end":30,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13},"identifierName":"foo"}, + "name": "foo" + }, + "computed": false, + "shorthand": true, + "value": { + "type": "Identifier", + "start":27,"end":30,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13},"identifierName":"foo"}, + "name": "foo" + } + } + ] + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/options.json b/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/options.json index ed5be518c9cc..70adcb5e62d3 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/options.json +++ b/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/options.json @@ -1,6 +1,3 @@ { - "plugins": [ - "classStaticBlock" - ], - "throws": "Unexpected token, expected \";\" (5:12)" -} \ No newline at end of file + "plugins": ["classStaticBlock"] +} diff --git a/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json b/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json new file mode 100644 index 000000000000..3d29d5376d60 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json @@ -0,0 +1,96 @@ +{ + "type": "File", + "start":0,"end":95,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (5:6)" + ], + "program": { + "type": "Program", + "start":0,"end":95,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":0,"end":95,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, + "id": { + "type": "Identifier", + "start":15,"end":18,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":18},"identifierName":"foo"}, + "name": "foo" + }, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":21,"end":95,"loc":{"start":{"line":1,"column":21},"end":{"line":8,"column":1}}, + "body": [ + { + "type": "ClassDeclaration", + "start":25,"end":93,"loc":{"start":{"line":2,"column":2},"end":{"line":7,"column":3}}, + "id": { + "type": "Identifier", + "start":31,"end":32,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":9},"identifierName":"C"}, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":33,"end":93,"loc":{"start":{"line":2,"column":10},"end":{"line":7,"column":3}}, + "body": [ + { + "type": "ClassMethod", + "start":39,"end":54,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":19}}, + "static": true, + "key": { + "type": "Identifier", + "start":46,"end":49,"loc":{"start":{"line":3,"column":11},"end":{"line":3,"column":14},"identifierName":"foo"}, + "name": "foo" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":52,"end":54,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":19}}, + "body": [], + "directives": [] + } + }, + { + "type": "StaticBlock", + "start":59,"end":89,"loc":{"start":{"line":4,"column":4},"end":{"line":6,"column":5}}, + "body": [ + { + "type": "ExpressionStatement", + "start":74,"end":83,"loc":{"start":{"line":5,"column":6},"end":{"line":5,"column":15}}, + "expression": { + "type": "AwaitExpression", + "start":74,"end":82,"loc":{"start":{"line":5,"column":6},"end":{"line":5,"column":14}}, + "argument": { + "type": "NumericLiteral", + "start":80,"end":82,"loc":{"start":{"line":5,"column":12},"end":{"line":5,"column":14}}, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + } + } + ] + } + ] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file From 9a9a5ee112af81c704362a4591b98eaba57b1f3b Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 22 Dec 2020 16:12:11 +0900 Subject: [PATCH 3/7] Avoid to lookahead --- .../babel-parser/src/parser/expression.js | 59 ++++++++++--------- packages/babel-parser/src/parser/util.js | 6 +- 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 54f6356e05aa..f7d01c4e9e38 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -55,7 +55,6 @@ import { newExpressionScope, } from "../util/expression-scope.js"; import { Errors } from "./error"; -import type State from "../tokenizer/state"; export default class ExpressionParser extends LValParser { // Forward-declaration: defined in statement.js @@ -493,19 +492,15 @@ export default class ExpressionParser extends LValParser { // Parse unary operators, both prefix and postfix. // https://tc39.es/ecma262/#prod-UnaryExpression parseMaybeUnary(refExpressionErrors: ?ExpressionErrors): N.Expression { - if (this.isContextual("await")) { - if (this.isAwaitAllowed()) { - return this.parseAwait(); - } else { - if (this.scope.inFunction) { - const lookahead = this.lookahead(); - if (lookahead.type.startsExpr && !this.isAmbiguousAwait(lookahead)) { - this.raise(this.state.start, Errors.AwaitNotInAsyncContext); - return this.parseAwait(); - } - } - } + const startPos = this.state.start; + const startLoc = this.state.startLoc; + const isAwait = this.isContextual("await"); + + if (isAwait && this.isAwaitAllowed()) { + this.next(); + return this.parseAwait(startPos, startLoc); } + const update = this.match(tt.incDec); const node = this.startNode(); if (this.state.type.prefix) { @@ -537,7 +532,19 @@ export default class ExpressionParser extends LValParser { } } - return this.parseUpdate(node, update, refExpressionErrors); + const expr = this.parseUpdate(node, update, refExpressionErrors); + + if ( + isAwait && + this.scope.inFunction && + this.state.type.startsExpr && + !this.isAmbiguousAwait() + ) { + this.raise(startPos, Errors.AwaitNotInAsyncContext); + return this.parseAwait(startPos, startLoc); + } + + return expr; } // https://tc39.es/ecma262/#prod-UpdateExpression @@ -2359,10 +2366,8 @@ export default class ExpressionParser extends LValParser { // Parses await expression inside async function. - parseAwait(): N.AwaitExpression { - const node = this.startNode(); - - this.next(); + parseAwait(startPos: number, startLoc: Position): N.AwaitExpression { + const node = this.startNodeAt(startPos, startLoc); this.expressionScope.recordParameterInitializerError( node.start, @@ -2388,22 +2393,22 @@ export default class ExpressionParser extends LValParser { return this.finishNode(node, "AwaitExpression"); } - isAmbiguousAwait(state: State = this.state): boolean { + isAmbiguousAwait(): boolean { return ( - this.hasPrecedingLineBreak(state) || + this.hasPrecedingLineBreak() || // All the following expressions are ambiguous: // await + 0, await - 0, await ( 0 ), await [ 0 ], await / 0 /u, await `` - state.type === tt.plusMin || - state.type === tt.parenL || - state.type === tt.bracketL || - state.type === tt.backQuote || + this.match(tt.plusMin) || + this.match(tt.parenL) || + this.match(tt.bracketL) || + this.match(tt.backQuote) || // Sometimes the tokenizer generates tt.slash for regexps, and this is // handler by parseExprAtom - state.type === tt.regexp || - state.type === tt.slash || + this.match(tt.regexp) || + this.match(tt.slash) || // This code could be parsed both as a modulo operator or as an intrinsic: // await %x(0) - (this.hasPlugin("v8intrinsic") && state.type === tt.modulo) + (this.hasPlugin("v8intrinsic") && this.match(tt.modulo)) ); } diff --git a/packages/babel-parser/src/parser/util.js b/packages/babel-parser/src/parser/util.js index 2e3ac0e6b79d..9244cc983e51 100644 --- a/packages/babel-parser/src/parser/util.js +++ b/packages/babel-parser/src/parser/util.js @@ -90,8 +90,10 @@ export default class UtilParser extends Tokenizer { ); } - hasPrecedingLineBreak(state: State = this.state): boolean { - return lineBreak.test(this.input.slice(state.lastTokEnd, state.start)); + hasPrecedingLineBreak(): boolean { + return lineBreak.test( + this.input.slice(this.state.lastTokEnd, this.state.start), + ); } // TODO From 6536b9886ed57ca2c75edbb309fa66a692fdd5e8 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Wed, 23 Dec 2020 23:01:48 +0900 Subject: [PATCH 4/7] Support TLA error in script --- .../babel-parser/src/parser/expression.js | 10 ++++-- .../top-level-script/options.json | 3 +- .../top-level-script/output.json | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index f7d01c4e9e38..38496a423b13 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -534,11 +534,15 @@ export default class ExpressionParser extends LValParser { const expr = this.parseUpdate(node, update, refExpressionErrors); + const startsExpr = this.hasPlugin("v8intrinsic") + ? this.state.type.startsExpr + : this.state.type.startsExpr && !this.match(tt.modulo); if ( isAwait && - this.scope.inFunction && - this.state.type.startsExpr && - !this.isAmbiguousAwait() + startsExpr && + !this.isAmbiguousAwait() && + (this.scope.inFunction || + (this.hasPlugin("topLevelAwait") && !this.inModule)) ) { this.raise(startPos, Errors.AwaitNotInAsyncContext); return this.parseAwait(startPos, startLoc); diff --git a/packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/options.json b/packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/options.json index c9dc6113fea3..fc666b8d2c59 100644 --- a/packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/options.json +++ b/packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/options.json @@ -1,5 +1,4 @@ { "plugins": ["topLevelAwait"], - "sourceType": "script", - "throws": "Unexpected token, expected \";\" (1:6)" + "sourceType": "script" } diff --git a/packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/output.json b/packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/output.json new file mode 100644 index 000000000000..1b4283b27129 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/top-level-await/top-level-script/output.json @@ -0,0 +1,33 @@ +{ + "type": "File", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (1:0)" + ], + "program": { + "type": "Program", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}}, + "expression": { + "type": "AwaitExpression", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}}, + "argument": { + "type": "NumericLiteral", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From 9082ef680152e851e50b0f85bf02c941fd194ced Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Sun, 10 Jan 2021 13:26:11 +0900 Subject: [PATCH 5/7] Address reviwed points --- packages/babel-parser/src/parser/expression.js | 7 ++++++- .../test/fixtures/es2017/async-functions/9/output.json | 2 +- .../allow-await-outside-function-throw/output.json | 2 +- .../await-inside-arrow-expression-disallowed/output.json | 2 +- .../await-inside-parameters-of-nested-function/output.json | 2 +- .../invalid-await-with-object-exp-in-function/output.json | 2 +- .../class-static-block/invalid-await/output.json | 2 +- 7 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 38496a423b13..49e4cc181640 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -544,7 +544,12 @@ export default class ExpressionParser extends LValParser { (this.scope.inFunction || (this.hasPlugin("topLevelAwait") && !this.inModule)) ) { - this.raise(startPos, Errors.AwaitNotInAsyncContext); + this.raise( + startPos, + this.hasPlugin("topLevelAwait") + ? Errors.AwaitNotInAsyncContext + : Errors.AwaitNotInAsyncFunction, + ); return this.parseAwait(startPos, startLoc); } diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json index 0ee180e1b892..27d691134ed7 100644 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/9/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":40,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":40}}, "errors": [ - "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (1:24)" + "SyntaxError: 'await' is only allowed within async functions (1:24)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json index 3f3e1cddf5b1..8f13d165a4be 100644 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/allow-await-outside-function-throw/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, "errors": [ - "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (2:9)" + "SyntaxError: 'await' is only allowed within async functions (2:9)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json index b12eebc08943..506ce33b909e 100644 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-arrow-expression-disallowed/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}}, "errors": [ - "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (1:8)" + "SyntaxError: 'await' is only allowed within async functions (1:8)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json index c7d4e8fb8241..444930868eef 100644 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-nested-function/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, "errors": [ - "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (2:19)", + "SyntaxError: 'await' is only allowed within async functions (2:19)", "SyntaxError: await is not allowed in async function parameters (2:19)" ], "program": { diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json index d64bc787b0ab..cd71c7cea376 100644 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/invalid-await-with-object-exp-in-function/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, "errors": [ - "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (2:2)" + "SyntaxError: 'await' is only allowed within async functions (2:2)" ], "program": { "type": "Program", diff --git a/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json b/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json index 3d29d5376d60..a5c0a305f294 100644 --- a/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json +++ b/packages/babel-parser/test/fixtures/experimental/class-static-block/invalid-await/output.json @@ -2,7 +2,7 @@ "type": "File", "start":0,"end":95,"loc":{"start":{"line":1,"column":0},"end":{"line":8,"column":1}}, "errors": [ - "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (5:6)" + "SyntaxError: 'await' is only allowed within async functions (5:6)" ], "program": { "type": "Program", From 6e692092fb40950b3fb38eca716a3ad98d3e57f3 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Sun, 10 Jan 2021 13:45:06 +0900 Subject: [PATCH 6/7] Address reviewed points --- .../babel-parser/src/parser/expression.js | 8 +-- .../options.json | 3 - .../output.json | 58 +++++++++++++++++ .../inside-class-property/options.json | 10 +-- .../inside-class-property/output.json | 63 +++++++++++++++++++ .../top-level-await/options.json | 8 +-- .../top-level-await/output.json | 60 ++++++++++++++++++ 7 files changed, 187 insertions(+), 23 deletions(-) delete mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/options.json create mode 100644 packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/output.json create mode 100644 packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 49e4cc181640..6d0677e4695a 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -537,13 +537,7 @@ export default class ExpressionParser extends LValParser { const startsExpr = this.hasPlugin("v8intrinsic") ? this.state.type.startsExpr : this.state.type.startsExpr && !this.match(tt.modulo); - if ( - isAwait && - startsExpr && - !this.isAmbiguousAwait() && - (this.scope.inFunction || - (this.hasPlugin("topLevelAwait") && !this.inModule)) - ) { + if (isAwait && startsExpr && !this.isAmbiguousAwait()) { this.raise( startPos, this.hasPlugin("topLevelAwait") diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/options.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/options.json deleted file mode 100644 index ceaee67ee6b4..000000000000 --- a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/options.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "throws": "Unexpected token, expected \",\" (1:17)" -} diff --git a/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/output.json b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/output.json new file mode 100644 index 000000000000..287f8eae4418 --- /dev/null +++ b/packages/babel-parser/test/fixtures/es2017/async-functions/await-inside-parameters-of-async-arrow-function/output.json @@ -0,0 +1,58 @@ +{ + "type": "File", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions (1:11)", + "SyntaxError: await is not allowed in async function parameters (1:11)" + ], + "program": { + "type": "Program", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}}, + "id": null, + "generator": false, + "async": true, + "params": [ + { + "type": "AssignmentPattern", + "start":7,"end":18,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":18}}, + "left": { + "type": "Identifier", + "start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"x"}, + "name": "x" + }, + "right": { + "type": "AwaitExpression", + "start":11,"end":18,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":18}}, + "argument": { + "type": "NumericLiteral", + "start":17,"end":18,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":18}}, + "extra": { + "rawValue": 2, + "raw": "2" + }, + "value": 2 + } + } + } + ], + "body": { + "type": "BlockStatement", + "start":23,"end":25,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":25}}, + "body": [], + "directives": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/options.json b/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/options.json index 0688c012b357..ac03e3ac9677 100644 --- a/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/options.json +++ b/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/options.json @@ -1,8 +1,4 @@ { - "plugins": [ - "topLevelAwait", - "classProperties" - ], - "sourceType": "module", - "throws": "Unexpected token, expected \";\" (2:12)" -} \ No newline at end of file + "plugins": ["topLevelAwati", "classProperties"], + "sourceType": "module" +} diff --git a/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json b/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json new file mode 100644 index 000000000000..8cdb6c704f02 --- /dev/null +++ b/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json @@ -0,0 +1,63 @@ +{ + "type": "File", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions (2:6)", + "SyntaxError: 'await' is only allowed within async functions (2:6)" + ], + "program": { + "type": "Program", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExportNamedDeclaration", + "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "specifiers": [], + "source": null, + "declaration": { + "type": "ClassDeclaration", + "start":7,"end":33,"loc":{"start":{"line":1,"column":7},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14},"identifierName":"C"}, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":15,"end":33,"loc":{"start":{"line":1,"column":15},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "ClassProperty", + "start":19,"end":31,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":14}}, + "static": false, + "key": { + "type": "Identifier", + "start":19,"end":20,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":3},"identifierName":"p"}, + "name": "p" + }, + "computed": false, + "value": { + "type": "AwaitExpression", + "start":23,"end":30,"loc":{"start":{"line":2,"column":6},"end":{"line":2,"column":13}}, + "argument": { + "type": "NumericLiteral", + "start":29,"end":30,"loc":{"start":{"line":2,"column":12},"end":{"line":2,"column":13}}, + "extra": { + "rawValue": 0, + "raw": "0" + }, + "value": 0 + } + } + } + ] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/options.json b/packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/options.json index ba579946562a..6328ffd5bc9e 100644 --- a/packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/options.json +++ b/packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/options.json @@ -1,8 +1,4 @@ { "sourceType": "module", - "plugins": [ - "typescript", - "topLevelAwait" - ], - "throws": "Unexpected token, expected \";\" (2:20)" -} \ No newline at end of file + "plugins": ["typescript", "topLevelAwait"] +} diff --git a/packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/output.json b/packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/output.json new file mode 100644 index 000000000000..a99125a44c6f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/module-namespace/top-level-await/output.json @@ -0,0 +1,60 @@ +{ + "type": "File", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "errors": [ + "SyntaxError: 'await' is only allowed within async functions and at the top levels of modules (2:14)" + ], + "program": { + "type": "Program", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "TSModuleDeclaration", + "start":0,"end":39,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, + "id": { + "type": "Identifier", + "start":10,"end":11,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":11},"identifierName":"N"}, + "name": "N" + }, + "body": { + "type": "TSModuleBlock", + "start":12,"end":39,"loc":{"start":{"line":1,"column":12},"end":{"line":3,"column":1}}, + "body": [ + { + "type": "VariableDeclaration", + "start":18,"end":37,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":23}}, + "declarations": [ + { + "type": "VariableDeclarator", + "start":24,"end":36,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":22}}, + "id": { + "type": "Identifier", + "start":24,"end":25,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":11},"identifierName":"x"}, + "name": "x" + }, + "init": { + "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 + } + } + } + ], + "kind": "const" + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file From 6d3b9c54291a098a4100c3247453eb2c094f27fe Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Mon, 11 Jan 2021 10:58:12 +0900 Subject: [PATCH 7/7] Avoid duplicate errors --- packages/babel-parser/src/parser/expression.js | 15 +++++++++------ packages/babel-parser/src/tokenizer/state.js | 3 +++ .../inside-class-property/output.json | 1 - 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 6d0677e4695a..b50aed34e682 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -538,12 +538,14 @@ export default class ExpressionParser extends LValParser { ? this.state.type.startsExpr : this.state.type.startsExpr && !this.match(tt.modulo); if (isAwait && startsExpr && !this.isAmbiguousAwait()) { - this.raise( - startPos, - this.hasPlugin("topLevelAwait") - ? Errors.AwaitNotInAsyncContext - : Errors.AwaitNotInAsyncFunction, - ); + if (!this.state.invalidAwaitErrors.has(startPos)) { + this.raise( + startPos, + this.hasPlugin("topLevelAwait") + ? Errors.AwaitNotInAsyncContext + : Errors.AwaitNotInAsyncFunction, + ); + } return this.parseAwait(startPos, startLoc); } @@ -2353,6 +2355,7 @@ export default class ExpressionParser extends LValParser { ? Errors.AwaitNotInAsyncContext : Errors.AwaitNotInAsyncFunction, ); + this.state.invalidAwaitErrors.add(startLoc); } else { this.raise(startLoc, Errors.UnexpectedReservedWord, word); } diff --git a/packages/babel-parser/src/tokenizer/state.js b/packages/babel-parser/src/tokenizer/state.js index ef61fde4eaa8..4f42b15faed0 100644 --- a/packages/babel-parser/src/tokenizer/state.js +++ b/packages/babel-parser/src/tokenizer/state.js @@ -154,6 +154,9 @@ export default class State { // Tokens length in token store tokensLength: number = 0; + // Positions of invalid await errors + invalidAwaitErrors: Set = new Set(); + curPosition(): Position { return new Position(this.curLine, this.pos - this.lineStart); } diff --git a/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json b/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json index 8cdb6c704f02..71e47b18fa76 100644 --- a/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json +++ b/packages/babel-parser/test/fixtures/experimental/top-level-await/inside-class-property/output.json @@ -2,7 +2,6 @@ "type": "File", "start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}}, "errors": [ - "SyntaxError: 'await' is only allowed within async functions (2:6)", "SyntaxError: 'await' is only allowed within async functions (2:6)" ], "program": {