[flow] Allow type casts in array patterns inside arrow parameters (#9069)
This commit is contained in:
parent
d2971a1959
commit
856edbf95f
@ -566,7 +566,7 @@ export default class ExpressionParser extends LValParser {
|
||||
);
|
||||
this.state.yieldOrAwaitInPossibleArrowParameters = oldYOAIPAP;
|
||||
} else {
|
||||
this.toReferencedList(node.arguments);
|
||||
this.toReferencedListDeep(node.arguments);
|
||||
|
||||
// We keep the old value if it isn't null, for cases like
|
||||
// (x = async(yield)) => {}
|
||||
@ -891,7 +891,14 @@ export default class ExpressionParser extends LValParser {
|
||||
true,
|
||||
refShorthandDefaultPos,
|
||||
);
|
||||
this.toReferencedList(node.elements);
|
||||
if (!this.state.maybeInArrowParameters) {
|
||||
// This could be an array pattern:
|
||||
// ([a: string, b: string]) => {}
|
||||
// In this case, we don't have to call toReferencedList. We will
|
||||
// call it, if needed, when we are sure that it is a parenthesized
|
||||
// expression by calling toReferencedListDeep.
|
||||
this.toReferencedList(node.elements);
|
||||
}
|
||||
return this.finishNode(node, "ArrayExpression");
|
||||
|
||||
case tt.braceL:
|
||||
@ -1173,10 +1180,10 @@ export default class ExpressionParser extends LValParser {
|
||||
}
|
||||
if (refNeedsArrowPos.start) this.unexpected(refNeedsArrowPos.start);
|
||||
|
||||
this.toReferencedListDeep(exprList, /* isParenthesizedExpr */ true);
|
||||
if (exprList.length > 1) {
|
||||
val = this.startNodeAt(innerStartPos, innerStartLoc);
|
||||
val.expressions = exprList;
|
||||
this.toReferencedList(val.expressions);
|
||||
this.finishNodeAt(val, "SequenceExpression", innerEndPos, innerEndLoc);
|
||||
} else {
|
||||
val = exprList[0];
|
||||
|
||||
@ -177,11 +177,26 @@ export default class LValParser extends NodeUtils {
|
||||
|
||||
toReferencedList(
|
||||
exprList: $ReadOnlyArray<?Expression>,
|
||||
isInParens?: boolean, // eslint-disable-line no-unused-vars
|
||||
isParenthesizedExpr?: boolean, // eslint-disable-line no-unused-vars
|
||||
): $ReadOnlyArray<?Expression> {
|
||||
return exprList;
|
||||
}
|
||||
|
||||
toReferencedListDeep(
|
||||
exprList: $ReadOnlyArray<?Expression>,
|
||||
isParenthesizedExpr?: boolean,
|
||||
): $ReadOnlyArray<?Expression> {
|
||||
this.toReferencedList(exprList, isParenthesizedExpr);
|
||||
|
||||
for (const expr of exprList) {
|
||||
if (expr && expr.type === "ArrayExpression") {
|
||||
this.toReferencedListDeep(expr.elements);
|
||||
}
|
||||
}
|
||||
|
||||
return exprList;
|
||||
}
|
||||
|
||||
// Parses spread element.
|
||||
|
||||
parseSpread<T: RestElement | SpreadElement>(
|
||||
|
||||
@ -1977,7 +1977,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
// type casts that we've found that are illegal in this context
|
||||
toReferencedList(
|
||||
exprList: $ReadOnlyArray<?N.Expression>,
|
||||
isInParens?: boolean,
|
||||
isParenthesizedExpr?: boolean,
|
||||
): $ReadOnlyArray<?N.Expression> {
|
||||
for (let i = 0; i < exprList.length; i++) {
|
||||
const expr = exprList[i];
|
||||
@ -1985,7 +1985,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
expr &&
|
||||
expr.type === "TypeCastExpression" &&
|
||||
(!expr.extra || !expr.extra.parenthesized) &&
|
||||
(exprList.length > 1 || !isInParens)
|
||||
(exprList.length > 1 || !isParenthesizedExpr)
|
||||
) {
|
||||
this.raise(
|
||||
expr.typeAnnotation.start,
|
||||
|
||||
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-1/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-1/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
[a: string];
|
||||
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-1/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-1/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "The type cast expression is expected to be wrapped with parenthesis (1:2)"
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-2/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-2/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
([a: string]);
|
||||
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-2/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-2/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "The type cast expression is expected to be wrapped with parenthesis (1:3)"
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-3/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-3/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
([a, [b: string]]);
|
||||
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-3/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-3/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "The type cast expression is expected to be wrapped with parenthesis (1:7)"
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-4/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-4/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
async ([a: string]);
|
||||
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-4/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-4/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "The type cast expression is expected to be wrapped with parenthesis (1:9)"
|
||||
}
|
||||
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-5/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-5/input.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
async ([a, [b: string]]);
|
||||
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-5/options.json
vendored
Normal file
3
packages/babel-parser/test/fixtures/flow/typecasts/fails-in-array-expression-5/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"throws": "The type cast expression is expected to be wrapped with parenthesis (1:13)"
|
||||
}
|
||||
14
packages/babel-parser/test/fixtures/flow/typecasts/works-in-array-pattern/input.js
vendored
Normal file
14
packages/babel-parser/test/fixtures/flow/typecasts/works-in-array-pattern/input.js
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
([a: string]) => {};
|
||||
([a, [b: string]]) => {};
|
||||
([a: string] = []) => {};
|
||||
({ x: [a: string] }) => {};
|
||||
|
||||
async ([a: string]) => {};
|
||||
async ([a, [b: string]]) => {};
|
||||
async ([a: string] = []) => {};
|
||||
async ({ x: [a: string] }) => {};
|
||||
|
||||
let [a: string] = c;
|
||||
let [a, [b: string]] = c;
|
||||
let [a: string] = c;
|
||||
let { x: [a: string] } = c;
|
||||
1739
packages/babel-parser/test/fixtures/flow/typecasts/works-in-array-pattern/output.json
vendored
Normal file
1739
packages/babel-parser/test/fixtures/flow/typecasts/works-in-array-pattern/output.json
vendored
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user