From 308c54b6ad4b0241770a649e3890032b3ba61f1d Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Sat, 25 Jan 2020 15:39:20 -0500 Subject: [PATCH 1/3] test: add test case --- .../async-generic-after-await/input.ts | 4 + .../async-generic-after-await/output.json | 217 ++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json diff --git a/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/input.ts b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/input.ts new file mode 100644 index 000000000000..cf80533f9bca --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/input.ts @@ -0,0 +1,4 @@ +async () => { + await null; + async () => null; +}; diff --git a/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json new file mode 100644 index 000000000000..8dfe1f39aa0f --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json @@ -0,0 +1,217 @@ +{ + "type": "File", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 2 + } + }, + "errors": [ + "SyntaxError: Await cannot be used as name inside an async function (2:2)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 2 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 53, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 2 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 0, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start": 12, + "end": 52, + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "ExpressionStatement", + "start": 16, + "end": 27, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 13 + } + }, + "expression": { + "type": "AwaitExpression", + "start": 16, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 12 + } + }, + "argument": { + "type": "NullLiteral", + "start": 22, + "end": 26, + "loc": { + "start": { + "line": 2, + "column": 8 + }, + "end": { + "line": 2, + "column": 12 + } + } + } + } + }, + { + "type": "ExpressionStatement", + "start": 30, + "end": 50, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 22 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 30, + "end": 49, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 21 + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start": 36, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 11 + } + }, + "params": [ + { + "type": "TSTypeParameter", + "start": 37, + "end": 38, + "loc": { + "start": { + "line": 3, + "column": 9 + }, + "end": { + "line": 3, + "column": 10 + } + }, + "name": "T" + } + ] + }, + "params": [], + "id": null, + "generator": false, + "async": true, + "body": { + "type": "NullLiteral", + "start": 45, + "end": 49, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 21 + } + } + } + } + } + ], + "directives": [] + } + } + } + ], + "directives": [] + } +} \ No newline at end of file From 74416c3a6313d8e318616a4c7c88c63e54bd1e4b Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Sun, 2 Feb 2020 16:02:49 -0500 Subject: [PATCH 2/3] fix: reset awaitPos before parsing generic arrow fn --- .../babel-parser/src/plugins/typescript/index.js | 12 ++++++++++++ .../async-generic-after-await/output.json | 3 --- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index c8c7d60ce3f4..fba703845b30 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1528,6 +1528,14 @@ export default (superClass: Class): Class => if (!this.isRelational("<")) { return undefined; } + + const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; + const oldYieldPos = this.state.yieldPos; + const oldAwaitPos = this.state.awaitPos; + this.state.maybeInArrowParameters = false; + this.state.yieldPos = -1; + this.state.awaitPos = -1; + const res: ?N.ArrowFunctionExpression = this.tsTryParseAndCatch(() => { const node: N.ArrowFunctionExpression = this.startNodeAt( startPos, @@ -1541,6 +1549,10 @@ export default (superClass: Class): Class => return node; }); + this.state.maybeInArrowParameters = oldMaybeInArrowParameters; + this.state.yieldPos = oldYieldPos; + this.state.awaitPos = oldAwaitPos; + if (!res) { return undefined; } diff --git a/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json index 8dfe1f39aa0f..d3320bb41535 100644 --- a/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json +++ b/packages/babel-parser/test/fixtures/typescript/arrow-function/async-generic-after-await/output.json @@ -12,9 +12,6 @@ "column": 2 } }, - "errors": [ - "SyntaxError: Await cannot be used as name inside an async function (2:2)" - ], "program": { "type": "Program", "start": 0, From 869206b35babf3c08e4e66d7b0ae8f85077aef8e Mon Sep 17 00:00:00 2001 From: Liam Doran Date: Tue, 4 Feb 2020 09:14:33 -0500 Subject: [PATCH 3/3] fix: maybeInArrowParameters reset to true --- packages/babel-parser/src/plugins/typescript/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index fba703845b30..9824c5970f20 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -1532,7 +1532,7 @@ export default (superClass: Class): Class => const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; const oldYieldPos = this.state.yieldPos; const oldAwaitPos = this.state.awaitPos; - this.state.maybeInArrowParameters = false; + this.state.maybeInArrowParameters = true; this.state.yieldPos = -1; this.state.awaitPos = -1;