From e6a6ed33f9840e54fd1136f4d3cf7a9eb78cec91 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Wed, 31 Oct 2018 16:36:45 -0700 Subject: [PATCH] fix: Do not allow TypeCastExpressions w/o parens inside of SequenceExpressions --- packages/babel-parser/src/plugins/flow.js | 17 ++++++++++++++++- .../flow/typecasts/fail-without-parens/input.js | 1 + .../typecasts/fail-without-parens/options.json | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/options.json diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 6382c1f6f3c2..3c0a588a18f3 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -2494,9 +2494,24 @@ export default (superClass: Class): Class => } parseParenAndDistinguishExpression(canBeArrow: boolean): N.Expression { - return super.parseParenAndDistinguishExpression( + const list = super.parseParenAndDistinguishExpression( canBeArrow && this.state.noArrowAt.indexOf(this.state.start) === -1, ); + + // Ensure that TypeCastExpressions without parens are not allowed inside SequenceExpressions + // e.g. (A, B: T) + if (list.type === "SequenceExpression") { + const firstTypeCast = list.expressions.find( + ({ type, extra }) => + type === "TypeCastExpression" && + (!extra || extra.parenthesized !== true), + ); + if (firstTypeCast) { + this.unexpected(firstTypeCast.typeAnnotation.start); + } + } + + return list; } parseSubscripts( diff --git a/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/input.js b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/input.js new file mode 100644 index 000000000000..767b0cba53b8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/input.js @@ -0,0 +1 @@ +(A, B: T) diff --git a/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/options.json b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/options.json new file mode 100644 index 000000000000..d904a92181b9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/typecasts/fail-without-parens/options.json @@ -0,0 +1,8 @@ +{ + "sourceType": "module", + "plugins": [ + "jsx", + "flow" + ], + "throws": "Unexpected token (1:5)" +} \ No newline at end of file