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