From 86245a83a2e58fc0bbbe28b9bf9a4b77a53ea5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 30 Dec 2019 16:10:19 -0500 Subject: [PATCH] TSTypeCastExpression should not be inside call parameters (#10939) --- .../src/plugins/typescript/index.js | 6 +- .../cast/parameter-typecast/input.ts | 1 + .../cast/parameter-typecast/output.json | 166 ++++++++++++++++++ 3 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/input.ts create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/output.json diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index 2b442620a507..0c740b2c9bdd 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -2568,11 +2568,7 @@ export default (superClass: Class): Class => ): $ReadOnlyArray { for (let i = 0; i < exprList.length; i++) { const expr = exprList[i]; - if ( - expr && - expr._exprListItem && - expr.type === "TsTypeCastExpression" - ) { + if (expr && expr.type === "TSTypeCastExpression") { this.raise(expr.start, "Did not expect a type annotation here."); } } diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/input.ts b/packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/input.ts new file mode 100644 index 000000000000..c494cc00bc39 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/input.ts @@ -0,0 +1 @@ +func(a: T); diff --git a/packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/output.json b/packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/output.json new file mode 100644 index 000000000000..d815e81603d1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/parameter-typecast/output.json @@ -0,0 +1,166 @@ +{ + "type": "File", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "errors": [ + "SyntaxError: Did not expect a type annotation here. (1:5)" + ], + "program": { + "type": "Program", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "sourceType": "module", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 11 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "callee": { + "type": "Identifier", + "start": 0, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "func" + }, + "name": "func" + }, + "arguments": [ + { + "type": "TSTypeCastExpression", + "start": 5, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "expression": { + "type": "Identifier", + "start": 5, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + }, + "identifierName": "a" + }, + "name": "a" + }, + "typeAnnotation": { + "type": "TSTypeAnnotation", + "start": 6, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "typeAnnotation": { + "type": "TSTypeReference", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "typeName": { + "type": "Identifier", + "start": 8, + "end": 9, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 9 + }, + "identifierName": "T" + }, + "name": "T" + } + } + } + } + ] + } + } + ], + "directives": [] + } +} \ No newline at end of file