From f13f4adcbb73bba964ba98bdc9541d4c73f0b2b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Thu, 28 Feb 2019 23:58:27 +0100 Subject: [PATCH] [TS] Disallow type casts in arrow parameters (#9612) --- packages/babel-parser/src/plugins/typescript.js | 14 ++++++++++++-- .../cast/arrow-async-parameter-as/input.js | 1 + .../cast/arrow-async-parameter-as/options.json | 3 +++ .../cast/arrow-async-parameter-assertion/input.js | 1 + .../arrow-async-parameter-assertion/options.json | 3 +++ .../typescript/cast/arrow-parameter-as/input.js | 1 + .../cast/arrow-parameter-as/options.json | 3 +++ .../cast/arrow-parameter-assertion/input.js | 1 + .../cast/arrow-parameter-assertion/options.json | 3 +++ 9 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/options.json create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/input.js create mode 100644 packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/options.json diff --git a/packages/babel-parser/src/plugins/typescript.js b/packages/babel-parser/src/plugins/typescript.js index 7bc3623d78..7d1f81d1b8 100644 --- a/packages/babel-parser/src/plugins/typescript.js +++ b/packages/babel-parser/src/plugins/typescript.js @@ -2268,8 +2268,18 @@ export default (superClass: Class): Class => ): $ReadOnlyArray { for (let i = 0; i < exprList.length; i++) { const expr = exprList[i]; - if (expr && expr.type === "TSTypeCastExpression") { - exprList[i] = this.typeCastToParameter(expr); + if (!expr) continue; + switch (expr.type) { + case "TSTypeCastExpression": + exprList[i] = this.typeCastToParameter(expr); + break; + case "TSAsExpression": + case "TSTypeAssertion": + this.raise( + expr.start, + "Unexpected type cast in parameter position.", + ); + break; } } return super.toAssignableList(exprList, isBinding, contextDescription); diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/input.js b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/input.js new file mode 100644 index 0000000000..746078a226 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/input.js @@ -0,0 +1 @@ +async (a as T) => {}; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/options.json b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/options.json new file mode 100644 index 0000000000..69d9150249 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-as/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected type cast in parameter position. (1:7)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/input.js b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/input.js new file mode 100644 index 0000000000..5e3f1481d6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/input.js @@ -0,0 +1 @@ +async ( a) => {}; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/options.json b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/options.json new file mode 100644 index 0000000000..69d9150249 --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-async-parameter-assertion/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected type cast in parameter position. (1:7)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/input.js b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/input.js new file mode 100644 index 0000000000..dc2282c6bd --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/input.js @@ -0,0 +1 @@ +(a as T) => {}; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/options.json b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/options.json new file mode 100644 index 0000000000..b5bdba868c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-as/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected type cast in parameter position. (1:1)" +} diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/input.js b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/input.js new file mode 100644 index 0000000000..39dae7d0cf --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/input.js @@ -0,0 +1 @@ +( a) => {}; \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/options.json b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/options.json new file mode 100644 index 0000000000..b5bdba868c --- /dev/null +++ b/packages/babel-parser/test/fixtures/typescript/cast/arrow-parameter-assertion/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Unexpected type cast in parameter position. (1:1)" +}