Check if param is assignable when parsing arrow return type (#11992)

This commit is contained in:
Huáng Jùnliàng
2020-10-14 14:09:48 -04:00
committed by GitHub
parent 4e66b8eb6b
commit 136e6301cb
9 changed files with 411 additions and 47 deletions

View File

@@ -1943,7 +1943,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
return partition(arrows, node =>
node.params.every(param => this.isAssignable(param, true)),
node.params.every(param => this.isAssignable(param)),
);
}
@@ -2147,47 +2147,12 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
isAssignable(node: N.Node, isBinding?: boolean): boolean {
isAssignable(node: N.Node): boolean {
switch (node.type) {
case "Identifier":
case "ObjectPattern":
case "ArrayPattern":
case "AssignmentPattern":
return true;
case "ObjectExpression": {
const last = node.properties.length - 1;
return node.properties.every((prop, i) => {
return (
prop.type !== "ObjectMethod" &&
(i === last || prop.type === "SpreadElement") &&
this.isAssignable(prop)
);
});
}
case "ObjectProperty":
return this.isAssignable(node.value);
case "SpreadElement":
return this.isAssignable(node.argument);
case "ArrayExpression":
return node.elements.every(element => this.isAssignable(element));
case "AssignmentExpression":
return node.operator === "=";
case "ParenthesizedExpression":
case "TypeCastExpression":
return this.isAssignable(node.expression);
case "MemberExpression":
case "OptionalMemberExpression":
return !isBinding;
default:
return false;
return super.isAssignable(node);
}
}
@@ -2773,7 +2738,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
// handle return types for arrow functions
parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression {
parseArrow(
node: N.ArrowFunctionExpression,
exprList: N.Node[],
): ?N.ArrowFunctionExpression {
if (this.match(tt.colon)) {
const result = this.tryParse(() => {
const oldNoAnonFunctionType = this.state.noAnonFunctionType;
@@ -2807,7 +2775,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
: null;
}
return super.parseArrow(node);
return super.parseArrow(node, exprList);
}
shouldParseArrow(): boolean {
@@ -2978,7 +2946,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): ?N.ArrowFunctionExpression {
const node = this.startNodeAt(startPos, startLoc);
this.parseFunctionParams(node);
if (!this.parseArrow(node)) return;
// set exprList to `[]` as the parameters has been validated in `parseFunctionParams`
if (!this.parseArrow(node, [])) return;
return this.parseArrowExpression(
node,
/* params */ undefined,

View File

@@ -2540,7 +2540,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression {
parseArrow(
node: N.ArrowFunctionExpression,
exprList,
): ?N.ArrowFunctionExpression {
if (this.match(tt.colon)) {
// This is different from how the TS parser does it.
// TS uses lookahead. The Babel Parser parses it as a parenthesized expression and converts.
@@ -2550,6 +2553,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
tt.colon,
);
if (this.canInsertSemicolon() || !this.match(tt.arrow)) abort();
// check if the exprList is assignable because `: TSType` can be part of conditional expression
// i.e. we can only know `: v` is not a return type by checking that `sum(v)` can not be a pattern.
// 0 ? v => (sum(v)) : v => 0
if (exprList.some(param => !this.isAssignable(param))) abort();
return returnType;
});
@@ -2561,7 +2568,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
}
return super.parseArrow(node);
return super.parseArrow(node, exprList);
}
// Allow type annotations inside of a parameter list.
@@ -2580,6 +2587,18 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return param;
}
isAssignable(node: N.Node): boolean {
switch (node.type) {
case "TSAsExpression":
case "TSNonNullExpression":
case "TSTypeAssertion":
case "TSTypeCastExpression":
return this.isAssignable(node.expression);
default:
return super.isAssignable(node);
}
}
toAssignable(node: N.Node): N.Node {
switch (node.type) {
case "TSTypeCastExpression":