ts: Check if param is assignable when parsing arrow return type (#13581)
This commit is contained in:
@@ -330,6 +330,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return (node: any);
|
||||
}
|
||||
|
||||
isAssignable(node: N.Node, isBinding?: boolean): boolean {
|
||||
if (node != null && this.isObjectProperty(node)) {
|
||||
return this.isAssignable(node.value, isBinding);
|
||||
}
|
||||
return super.isAssignable(node, isBinding);
|
||||
}
|
||||
|
||||
toAssignable(node: N.Node, isLHS: boolean = false): N.Node {
|
||||
if (node != null && this.isObjectProperty(node)) {
|
||||
this.toAssignable(node.value, isLHS);
|
||||
|
||||
@@ -2257,46 +2257,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
}
|
||||
|
||||
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 false;
|
||||
if (node.type === "TypeCastExpression") {
|
||||
return this.isAssignable(node.expression, isBinding);
|
||||
} else {
|
||||
return super.isAssignable(node, isBinding);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2989,8 +2953,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return super.parseArrow(node);
|
||||
}
|
||||
|
||||
shouldParseArrow(): boolean {
|
||||
return this.match(tt.colon) || super.shouldParseArrow();
|
||||
shouldParseArrow(params: Array<N.Node>): boolean {
|
||||
return this.match(tt.colon) || super.shouldParseArrow(params);
|
||||
}
|
||||
|
||||
setArrowFunctionParameters(
|
||||
|
||||
@@ -2877,6 +2877,17 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return param;
|
||||
}
|
||||
|
||||
isAssignable(node: N.Node, isBinding?: boolean): boolean {
|
||||
switch (node.type) {
|
||||
case "TSTypeCastExpression":
|
||||
return this.isAssignable(node.expression, isBinding);
|
||||
case "TSParameterProperty":
|
||||
return true;
|
||||
default:
|
||||
return super.isAssignable(node, isBinding);
|
||||
}
|
||||
}
|
||||
|
||||
toAssignable(node: N.Node, isLHS: boolean = false): N.Node {
|
||||
switch (node.type) {
|
||||
case "TSTypeCastExpression":
|
||||
@@ -3071,8 +3082,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
||||
return node.expression;
|
||||
}
|
||||
|
||||
shouldParseArrow() {
|
||||
return this.match(tt.colon) || super.shouldParseArrow();
|
||||
shouldParseArrow(params: Array<N.Node>) {
|
||||
if (this.match(tt.colon)) {
|
||||
return params.every(expr => this.isAssignable(expr, true));
|
||||
}
|
||||
return super.shouldParseArrow(params);
|
||||
}
|
||||
|
||||
shouldParseAsyncArrow(): boolean {
|
||||
|
||||
Reference in New Issue
Block a user