Revert "Fix: check if param is assignable when parsing arrow return type annotation" (#12198)
This reverts commit 84987a00e6.
This commit is contained in:
@@ -1453,7 +1453,7 @@ export default class ExpressionParser extends LValParser {
|
||||
if (
|
||||
canBeArrow &&
|
||||
this.shouldParseArrow() &&
|
||||
(arrowNode = this.parseArrow(arrowNode, exprList))
|
||||
(arrowNode = this.parseArrow(arrowNode))
|
||||
) {
|
||||
if (!this.isAwaitAllowed() && !this.state.maybeInAsyncArrowHead) {
|
||||
this.state.awaitPos = oldAwaitPos;
|
||||
@@ -1509,10 +1509,7 @@ export default class ExpressionParser extends LValParser {
|
||||
return !this.canInsertSemicolon();
|
||||
}
|
||||
|
||||
parseArrow(
|
||||
node: N.ArrowFunctionExpression,
|
||||
exprList: N.Node[], // eslint-disable-line no-unused-vars
|
||||
): ?N.ArrowFunctionExpression {
|
||||
parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression {
|
||||
if (this.eat(tt.arrow)) {
|
||||
return node;
|
||||
}
|
||||
|
||||
@@ -51,60 +51,10 @@ export default class LValParser extends NodeUtils {
|
||||
+parseDecorator: () => Decorator;
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if a node can be converted to a binding identifier or binding pattern.
|
||||
* https://tc39.es/ecma262/#prod-BindingIdentifier
|
||||
* https://tc39.es/ecma262/#prod-BindingPattern
|
||||
* Note that although a mebmer expression can serve as a LHS in the init of for loop,
|
||||
* i.e. `for (a.b of []);`, it is not a binding pattern
|
||||
*
|
||||
* @param {Node} node
|
||||
* @returns {boolean}
|
||||
* @memberof LValParser
|
||||
*/
|
||||
isAssignable(node: Node): boolean {
|
||||
switch (node.type) {
|
||||
case "Identifier":
|
||||
case "ObjectPattern":
|
||||
case "ArrayPattern":
|
||||
case "AssignmentPattern":
|
||||
case "RestElement":
|
||||
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":
|
||||
return this.isAssignable(node.expression);
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Convert existing expression atom to assignable pattern
|
||||
// if possible.
|
||||
// When this one is updated, please check if `isAssignable` also needs to be updated.
|
||||
// NOTE: There is a corresponding "isAssignable" method in flow.js.
|
||||
// When this one is updated, please check if also that one needs to be updated.
|
||||
|
||||
toAssignable(node: Node): Node {
|
||||
let parenthesized = undefined;
|
||||
|
||||
@@ -1943,7 +1943,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
|
||||
return partition(arrows, node =>
|
||||
node.params.every(param => this.isAssignable(param)),
|
||||
node.params.every(param => this.isAssignable(param, true)),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2146,12 +2146,47 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
isAssignable(node: N.Node): boolean {
|
||||
isAssignable(node: N.Node, isBinding?: boolean): 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 super.isAssignable(node);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2737,10 +2772,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
|
||||
// handle return types for arrow functions
|
||||
parseArrow(
|
||||
node: N.ArrowFunctionExpression,
|
||||
exprList: N.Node[],
|
||||
): ?N.ArrowFunctionExpression {
|
||||
parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression {
|
||||
if (this.match(tt.colon)) {
|
||||
const result = this.tryParse(() => {
|
||||
const oldNoAnonFunctionType = this.state.noAnonFunctionType;
|
||||
@@ -2774,7 +2806,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
: null;
|
||||
}
|
||||
|
||||
return super.parseArrow(node, exprList);
|
||||
return super.parseArrow(node);
|
||||
}
|
||||
|
||||
shouldParseArrow(): boolean {
|
||||
@@ -2945,8 +2977,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
): ?N.ArrowFunctionExpression {
|
||||
const node = this.startNodeAt(startPos, startLoc);
|
||||
this.parseFunctionParams(node);
|
||||
// set exprList to `[]` as the parameters has been validated in `parseFunctionParams`
|
||||
if (!this.parseArrow(node, [])) return;
|
||||
if (!this.parseArrow(node)) return;
|
||||
return this.parseArrowExpression(
|
||||
node,
|
||||
/* params */ undefined,
|
||||
|
||||
@@ -2540,10 +2540,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
parseArrow(
|
||||
node: N.ArrowFunctionExpression,
|
||||
exprList,
|
||||
): ?N.ArrowFunctionExpression {
|
||||
parseArrow(node: N.ArrowFunctionExpression): ?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.
|
||||
@@ -2553,10 +2550,6 @@ 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;
|
||||
});
|
||||
|
||||
@@ -2568,7 +2561,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
}
|
||||
|
||||
return super.parseArrow(node, exprList);
|
||||
return super.parseArrow(node);
|
||||
}
|
||||
|
||||
// Allow type annotations inside of a parameter list.
|
||||
@@ -2587,18 +2580,6 @@ 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":
|
||||
|
||||
Reference in New Issue
Block a user