Skip to content

Commit e9bea31

Browse files
Revert "Check if param is assignable when parsing arrow return type (#11992)"
This reverts commit 136e630.
1 parent af8e0fa commit e9bea31

9 files changed

Lines changed: 47 additions & 411 deletions

File tree

packages/babel-parser/src/parser/expression.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,7 +1453,7 @@ export default class ExpressionParser extends LValParser {
14531453
if (
14541454
canBeArrow &&
14551455
this.shouldParseArrow() &&
1456-
(arrowNode = this.parseArrow(arrowNode, exprList))
1456+
(arrowNode = this.parseArrow(arrowNode))
14571457
) {
14581458
if (!this.isAwaitAllowed() && !this.state.maybeInAsyncArrowHead) {
14591459
this.state.awaitPos = oldAwaitPos;
@@ -1509,10 +1509,7 @@ export default class ExpressionParser extends LValParser {
15091509
return !this.canInsertSemicolon();
15101510
}
15111511

1512-
parseArrow(
1513-
node: N.ArrowFunctionExpression,
1514-
exprList: N.Node[], // eslint-disable-line no-unused-vars
1515-
): ?N.ArrowFunctionExpression {
1512+
parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression {
15161513
if (this.eat(tt.arrow)) {
15171514
return node;
15181515
}

packages/babel-parser/src/parser/lval.js

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,10 @@ export default class LValParser extends NodeUtils {
5151
+parseDecorator: () => Decorator;
5252
*/
5353

54-
/**
55-
* Check if a node can be converted to a binding identifier or binding pattern.
56-
* https://tc39.es/ecma262/#prod-BindingIdentifier
57-
* https://tc39.es/ecma262/#prod-BindingPattern
58-
* Note that although a mebmer expression can serve as a LHS in the init of for loop,
59-
* i.e. `for (a.b of []);`, it is not a binding pattern
60-
*
61-
* @param {Node} node
62-
* @returns {boolean}
63-
* @memberof LValParser
64-
*/
65-
isAssignable(node: Node): boolean {
66-
switch (node.type) {
67-
case "Identifier":
68-
case "ObjectPattern":
69-
case "ArrayPattern":
70-
case "AssignmentPattern":
71-
return true;
72-
73-
case "ObjectExpression": {
74-
const last = node.properties.length - 1;
75-
return node.properties.every((prop, i) => {
76-
return (
77-
prop.type !== "ObjectMethod" &&
78-
(i === last || prop.type === "SpreadElement") &&
79-
this.isAssignable(prop)
80-
);
81-
});
82-
}
83-
84-
case "ObjectProperty":
85-
return this.isAssignable(node.value);
86-
87-
case "SpreadElement":
88-
return this.isAssignable(node.argument);
89-
90-
case "ArrayExpression":
91-
return node.elements.every(element => this.isAssignable(element));
92-
93-
case "AssignmentExpression":
94-
return node.operator === "=";
95-
96-
case "ParenthesizedExpression":
97-
return this.isAssignable(node.expression);
98-
99-
default:
100-
return false;
101-
}
102-
}
103-
10454
// Convert existing expression atom to assignable pattern
10555
// if possible.
106-
// When this one is updated, please check if `isAssignable` also needs to be updated.
56+
// NOTE: There is a corresponding "isAssignable" method in flow.js.
57+
// When this one is updated, please check if also that one needs to be updated.
10758

10859
toAssignable(node: Node): Node {
10960
let parenthesized = undefined;

packages/babel-parser/src/plugins/flow.js

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
19431943
}
19441944

19451945
return partition(arrows, node =>
1946-
node.params.every(param => this.isAssignable(param)),
1946+
node.params.every(param => this.isAssignable(param, true)),
19471947
);
19481948
}
19491949

@@ -2146,12 +2146,47 @@ export default (superClass: Class<Parser>): Class<Parser> =>
21462146
}
21472147
}
21482148

2149-
isAssignable(node: N.Node): boolean {
2149+
isAssignable(node: N.Node, isBinding?: boolean): boolean {
21502150
switch (node.type) {
2151+
case "Identifier":
2152+
case "ObjectPattern":
2153+
case "ArrayPattern":
2154+
case "AssignmentPattern":
2155+
return true;
2156+
2157+
case "ObjectExpression": {
2158+
const last = node.properties.length - 1;
2159+
return node.properties.every((prop, i) => {
2160+
return (
2161+
prop.type !== "ObjectMethod" &&
2162+
(i === last || prop.type === "SpreadElement") &&
2163+
this.isAssignable(prop)
2164+
);
2165+
});
2166+
}
2167+
2168+
case "ObjectProperty":
2169+
return this.isAssignable(node.value);
2170+
2171+
case "SpreadElement":
2172+
return this.isAssignable(node.argument);
2173+
2174+
case "ArrayExpression":
2175+
return node.elements.every(element => this.isAssignable(element));
2176+
2177+
case "AssignmentExpression":
2178+
return node.operator === "=";
2179+
2180+
case "ParenthesizedExpression":
21512181
case "TypeCastExpression":
21522182
return this.isAssignable(node.expression);
2183+
2184+
case "MemberExpression":
2185+
case "OptionalMemberExpression":
2186+
return !isBinding;
2187+
21532188
default:
2154-
return super.isAssignable(node);
2189+
return false;
21552190
}
21562191
}
21572192

@@ -2737,10 +2772,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
27372772
}
27382773

27392774
// handle return types for arrow functions
2740-
parseArrow(
2741-
node: N.ArrowFunctionExpression,
2742-
exprList: N.Node[],
2743-
): ?N.ArrowFunctionExpression {
2775+
parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression {
27442776
if (this.match(tt.colon)) {
27452777
const result = this.tryParse(() => {
27462778
const oldNoAnonFunctionType = this.state.noAnonFunctionType;
@@ -2774,7 +2806,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
27742806
: null;
27752807
}
27762808

2777-
return super.parseArrow(node, exprList);
2809+
return super.parseArrow(node);
27782810
}
27792811

27802812
shouldParseArrow(): boolean {
@@ -2945,8 +2977,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
29452977
): ?N.ArrowFunctionExpression {
29462978
const node = this.startNodeAt(startPos, startLoc);
29472979
this.parseFunctionParams(node);
2948-
// set exprList to `[]` as the parameters has been validated in `parseFunctionParams`
2949-
if (!this.parseArrow(node, [])) return;
2980+
if (!this.parseArrow(node)) return;
29502981
return this.parseArrowExpression(
29512982
node,
29522983
/* params */ undefined,

packages/babel-parser/src/plugins/typescript/index.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,10 +2540,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
25402540
}
25412541
}
25422542

2543-
parseArrow(
2544-
node: N.ArrowFunctionExpression,
2545-
exprList,
2546-
): ?N.ArrowFunctionExpression {
2543+
parseArrow(node: N.ArrowFunctionExpression): ?N.ArrowFunctionExpression {
25472544
if (this.match(tt.colon)) {
25482545
// This is different from how the TS parser does it.
25492546
// 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> =>
25532550
tt.colon,
25542551
);
25552552
if (this.canInsertSemicolon() || !this.match(tt.arrow)) abort();
2556-
// check if the exprList is assignable because `: TSType` can be part of conditional expression
2557-
// i.e. we can only know `: v` is not a return type by checking that `sum(v)` can not be a pattern.
2558-
// 0 ? v => (sum(v)) : v => 0
2559-
if (exprList.some(param => !this.isAssignable(param))) abort();
25602553
return returnType;
25612554
});
25622555

@@ -2568,7 +2561,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
25682561
}
25692562
}
25702563

2571-
return super.parseArrow(node, exprList);
2564+
return super.parseArrow(node);
25722565
}
25732566

25742567
// Allow type annotations inside of a parameter list.
@@ -2587,18 +2580,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
25872580
return param;
25882581
}
25892582

2590-
isAssignable(node: N.Node): boolean {
2591-
switch (node.type) {
2592-
case "TSAsExpression":
2593-
case "TSNonNullExpression":
2594-
case "TSTypeAssertion":
2595-
case "TSTypeCastExpression":
2596-
return this.isAssignable(node.expression);
2597-
default:
2598-
return super.isAssignable(node);
2599-
}
2600-
}
2601-
26022583
toAssignable(node: N.Node): N.Node {
26032584
switch (node.type) {
26042585
case "TSTypeCastExpression":

packages/babel-parser/test/fixtures/typescript/arrow-function/ambiguous-ternary-arrow-return-type-typescript-issue-16241/input.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/babel-parser/test/fixtures/typescript/arrow-function/ambiguous-ternary-arrow-return-type-typescript-issue-16241/options.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/babel-parser/test/fixtures/typescript/arrow-function/return-type-like-conditional/input.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

packages/babel-parser/test/fixtures/typescript/arrow-function/return-type-like-conditional/options.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)