From 42de14d021c7fbcea93d3bea8cd8860f565d68db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 13 Apr 2018 21:47:19 +0200 Subject: [PATCH] Fix await as function name --- packages/babel-parser/src/parser/statement.js | 12 +- .../input.js | 1 + .../output.json | 86 ++++++++++ .../input.js | 1 + .../options.json | 3 + .../input.js | 3 + .../options.json | 3 + .../input.js | 3 + .../output.json | 158 ++++++++++++++++++ 9 files changed, 267 insertions(+), 3 deletions(-) create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/input.js create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/output.json create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/input.js create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/options.json create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/input.js create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/options.json create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/input.js create mode 100644 packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/output.json diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index e05a2755de2d..b19cd78f1bd3 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -847,7 +847,6 @@ export default class StatementParser extends ExpressionParser { const oldInClassProperty = this.state.inClassProperty; this.state.inFunction = true; this.state.inMethod = false; - this.state.inAsync = isAsync; this.state.inClassProperty = false; this.initFunction(node, isAsync); @@ -874,11 +873,18 @@ export default class StatementParser extends ExpressionParser { // valid because yield is parsed as if it was outside the generator. // Therefore, this.state.inGenerator is set before or after parsing the // function id according to the "isStatement" parameter. - if (!isStatement) this.state.inGenerator = node.generator; + // The same applies to await & async functions. + if (!isStatement) { + this.state.inAsync = isAsync; + this.state.inGenerator = node.generator; + } if (this.match(tt.name) || this.match(tt._yield)) { node.id = this.parseBindingIdentifier(); } - if (isStatement) this.state.inGenerator = node.generator; + if (isStatement) { + this.state.inAsync = isAsync; + this.state.inGenerator = node.generator; + } this.parseFunctionParams(node); this.parseFunctionBodyAndFinish( diff --git a/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/input.js b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/input.js new file mode 100644 index 000000000000..60b70d4a68e9 --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/input.js @@ -0,0 +1 @@ +async function await() {} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/output.json b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/output.json new file mode 100644 index 000000000000..0cde29d7f404 --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-declaration-name/output.json @@ -0,0 +1,86 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "id": { + "type": "Identifier", + "start": 15, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "await" + }, + "name": "await" + }, + "generator": false, + "async": true, + "params": [], + "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/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/input.js b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/input.js new file mode 100644 index 000000000000..5102f04c3f84 --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/input.js @@ -0,0 +1 @@ +(async function await() {}); \ No newline at end of file diff --git a/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/options.json b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/options.json new file mode 100644 index 000000000000..848e79b9dbca --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-async-function-expression-name/options.json @@ -0,0 +1,3 @@ +{ + "throws": "invalid use of await inside of an async function (1:16)" +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/input.js b/packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/input.js new file mode 100644 index 000000000000..1df99191e510 --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/input.js @@ -0,0 +1,3 @@ +async function foo() { + function await() {} +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/options.json b/packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/options.json new file mode 100644 index 000000000000..e2abd7d95537 --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-function-declaration-name-inside-async-function/options.json @@ -0,0 +1,3 @@ +{ + "throws": "invalid use of await inside of an async function (2:11)" +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/input.js b/packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/input.js new file mode 100644 index 000000000000..97183151654b --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/input.js @@ -0,0 +1,3 @@ +async function fn() { + (function await() {}); +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/output.json b/packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/output.json new file mode 100644 index 000000000000..5711b91c540d --- /dev/null +++ b/packages/babylon/test/fixtures/es2017/async-functions/await-function-expression-name-inside-async-function/output.json @@ -0,0 +1,158 @@ +{ + "type": "File", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 15, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 17 + }, + "identifierName": "fn" + }, + "name": "fn" + }, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 20, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 24, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 24 + } + }, + "expression": { + "type": "FunctionExpression", + "start": 25, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 3 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "id": { + "type": "Identifier", + "start": 34, + "end": 39, + "loc": { + "start": { + "line": 2, + "column": 12 + }, + "end": { + "line": 2, + "column": 17 + }, + "identifierName": "await" + }, + "name": "await" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 20 + }, + "end": { + "line": 2, + "column": 22 + } + }, + "body": [], + "directives": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 24 + } + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file