From 48d66eb6482a6bc029a884f113e78cbfe8183e2e Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Mon, 18 Mar 2019 22:18:47 -0700 Subject: [PATCH] Correctly parse TS TypeAssertions around arrow functions (#9699) --- .../babel-parser/src/plugins/typescript.js | 5 +- .../arrow-in-parens-with-parens-node/input.js | 1 + .../options.json | 4 + .../output.json | 217 ++++++++++++++++++ .../typescript/cast/arrow-in-parens/input.js | 1 + .../cast/arrow-in-parens/output.json | 206 +++++++++++++++++ 6 files changed, 433 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/output.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/output.json diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index dfe6695616..07c560707d 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -2121,7 +2121,10 @@ export default (superClass: Class): Class => // This is similar to TypeScript's `tryParseParenthesizedArrowFunctionExpression`. typeParameters = this.tsParseTypeParameters(); arrowExpression = super.parseMaybeAssign(...args); - if (arrowExpression.type !== "ArrowFunctionExpression") { + if ( + arrowExpression.type !== "ArrowFunctionExpression" || + (arrowExpression.extra && arrowExpression.extra.parenthesized) + ) { this.unexpected(); // Go to the catch block (needs a SyntaxError). } } catch (err) { diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/input.js b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/input.js new file mode 100644 index 0000000000..f9f6449772 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/input.js @@ -0,0 +1 @@ +var asserted1 = ((n) => { return n; }); diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/options.json b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/options.json new file mode 100644 index 0000000000..9dc8f2c9c9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/options.json @@ -0,0 +1,4 @@ +{ + "createParenthesizedExpressions": true, + "plugins": ["typescript"] +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/output.json b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/output.json new file mode 100644 index 0000000000..29e413f17d --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens-with-parens-node/output.json @@ -0,0 +1,217 @@ +{ + "type": "File", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "asserted1" + }, + "name": "asserted1" + }, + "init": { + "type": "TSTypeAssertion", + "start": 16, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + "expression": { + "type": "ParenthesizedExpression", + "start": 21, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 22, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "n" + }, + "name": "n" + } + ], + "body": { + "type": "BlockStatement", + "start": 29, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 31, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "argument": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 39 + }, + "identifierName": "n" + }, + "name": "n" + } + } + ], + "directives": [] + } + } + } + } + } + ], + "kind": "var" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/input.js b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/input.js new file mode 100644 index 0000000000..f9f6449772 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/input.js @@ -0,0 +1 @@ +var asserted1 = ((n) => { return n; }); diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/output.json b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/output.json new file mode 100644 index 0000000000..08bffbefa0 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-in-parens/output.json @@ -0,0 +1,206 @@ +{ + "type": "File", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 44, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 4, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "id": { + "type": "Identifier", + "start": 4, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "asserted1" + }, + "name": "asserted1" + }, + "init": { + "type": "TSTypeAssertion", + "start": 16, + "end": 43, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 43 + } + }, + "typeAnnotation": { + "type": "TSAnyKeyword", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + "expression": { + "type": "ArrowFunctionExpression", + "start": 22, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "n" + }, + "name": "n" + } + ], + "body": { + "type": "BlockStatement", + "start": 29, + "end": 42, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 42 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 31, + "end": 40, + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "argument": { + "type": "Identifier", + "start": 38, + "end": 39, + "loc": { + "start": { + "line": 1, + "column": 38 + }, + "end": { + "line": 1, + "column": 39 + }, + "identifierName": "n" + }, + "name": "n" + } + } + ], + "directives": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 21 + } + } + } + } + ], + "kind": "var" + } + ], + "directives": [] + } +} \ No newline at end of file