Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow member expressions to start async arrows #10332

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 7 additions & 13 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -545,21 +545,16 @@ export default class ExpressionParser extends LValParser {
startLoc: Position,
noCalls?: ?boolean,
): N.Expression {
const maybeAsyncArrow = this.atPossibleAsync(base);

const state = {
optionalChainMember: false,
maybeAsyncArrow: this.atPossibleAsync(base),
stop: false,
};
do {
base = this.parseSubscript(
base,
startPos,
startLoc,
noCalls,
state,
maybeAsyncArrow,
);
base = this.parseSubscript(base, startPos, startLoc, noCalls, state);

// After parsing a subscript, this isn't "async" for sure.
state.maybeAsyncArrow = false;
} while (!state.stop);
return base;
}
Expand All @@ -574,7 +569,6 @@ export default class ExpressionParser extends LValParser {
startLoc: Position,
noCalls: ?boolean,
state: N.ParseSubscriptState,
maybeAsyncArrow: boolean,
): N.Expression {
if (!noCalls && this.eat(tt.doubleColon)) {
const node = this.startNodeAt(startPos, startLoc);
Expand Down Expand Up @@ -656,7 +650,7 @@ export default class ExpressionParser extends LValParser {

node.arguments = this.parseCallExpressionArguments(
tt.parenR,
maybeAsyncArrow,
state.maybeAsyncArrow,
base.type === "Import",
base.type !== "Super",
);
Expand All @@ -666,7 +660,7 @@ export default class ExpressionParser extends LValParser {
this.finishOptionalCallExpression(node);
}

if (maybeAsyncArrow && this.shouldParseAsyncArrow()) {
if (state.maybeAsyncArrow && this.shouldParseAsyncArrow()) {
state.stop = true;

this.checkCommaAfterRestFromSpread();
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2634,7 +2634,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc: Position,
noCalls: ?boolean,
subscriptState: N.ParseSubscriptState,
maybeAsyncArrow: boolean,
): N.Expression {
if (this.match(tt.questionDot) && this.isLookaheadRelational("<")) {
this.expectPlugin("optionalChaining");
Expand Down Expand Up @@ -2687,7 +2686,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc,
noCalls,
subscriptState,
maybeAsyncArrow,
);
}

Expand Down
10 changes: 1 addition & 9 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1564,7 +1564,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc: Position,
noCalls: ?boolean,
state: N.ParseSubscriptState,
maybeAsyncArrow: boolean,
): N.Expression {
if (!this.hasPrecedingLineBreak() && this.match(tt.bang)) {
this.state.exprAllowed = false;
Expand Down Expand Up @@ -1627,14 +1626,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (result) return result;
}

return super.parseSubscript(
base,
startPos,
startLoc,
noCalls,
state,
maybeAsyncArrow,
);
return super.parseSubscript(base, startPos, startLoc, noCalls, state);
}

parseNewArguments(node: N.NewExpression): void {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/types.js
Expand Up @@ -1447,5 +1447,6 @@ export type Placeholder<N: PlaceholderTypes> = NodeBase & {

export type ParseSubscriptState = {
optionalChainMember: boolean,
maybeAsyncArrow: boolean,
stop: boolean,
};
@@ -0,0 +1,3 @@
// @flow

true ? async.waterfall() : null;
@@ -0,0 +1,198 @@
{
"type": "File",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 32
}
},
"program": {
"type": "Program",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 32
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 10,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 32
}
},
"expression": {
"type": "ConditionalExpression",
"start": 10,
"end": 41,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 31
}
},
"test": {
"type": "BooleanLiteral",
"start": 10,
"end": 14,
"loc": {
"start": {
"line": 3,
"column": 0
},
"end": {
"line": 3,
"column": 4
}
},
"value": true
},
"consequent": {
"type": "CallExpression",
"start": 17,
"end": 34,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 24
}
},
"callee": {
"type": "MemberExpression",
"start": 17,
"end": 32,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 22
}
},
"object": {
"type": "Identifier",
"start": 17,
"end": 22,
"loc": {
"start": {
"line": 3,
"column": 7
},
"end": {
"line": 3,
"column": 12
},
"identifierName": "async"
},
"name": "async"
},
"property": {
"type": "Identifier",
"start": 23,
"end": 32,
"loc": {
"start": {
"line": 3,
"column": 13
},
"end": {
"line": 3,
"column": 22
},
"identifierName": "waterfall"
},
"name": "waterfall"
},
"computed": false
},
"arguments": []
},
"alternate": {
"type": "NullLiteral",
"start": 37,
"end": 41,
"loc": {
"start": {
"line": 3,
"column": 27
},
"end": {
"line": 3,
"column": 31
}
}
}
},
"leadingComments": [
{
"type": "CommentLine",
"value": " @flow",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
}
]
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " @flow",
"start": 0,
"end": 8,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
}
]
}