Do not allow member expressions to start async arrows (#10332)

* Do not allow member expressions to start async arrows

* Boolean -> boolean
This commit is contained in:
Nicolò Ribaudo 2019-10-01 11:40:42 +02:00 committed by GitHub
parent 94fcabc4e3
commit 80d99b4d4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 210 additions and 24 deletions

View File

@ -578,21 +578,16 @@ export default class ExpressionParser extends LValParser {
startLoc: Position, startLoc: Position,
noCalls?: ?boolean, noCalls?: ?boolean,
): N.Expression { ): N.Expression {
const maybeAsyncArrow = this.atPossibleAsync(base);
const state = { const state = {
optionalChainMember: false, optionalChainMember: false,
maybeAsyncArrow: this.atPossibleAsync(base),
stop: false, stop: false,
}; };
do { do {
base = this.parseSubscript( base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
base,
startPos, // After parsing a subscript, this isn't "async" for sure.
startLoc, state.maybeAsyncArrow = false;
noCalls,
state,
maybeAsyncArrow,
);
} while (!state.stop); } while (!state.stop);
return base; return base;
} }
@ -607,7 +602,6 @@ export default class ExpressionParser extends LValParser {
startLoc: Position, startLoc: Position,
noCalls: ?boolean, noCalls: ?boolean,
state: N.ParseSubscriptState, state: N.ParseSubscriptState,
maybeAsyncArrow: boolean,
): N.Expression { ): N.Expression {
if (!noCalls && this.eat(tt.doubleColon)) { if (!noCalls && this.eat(tt.doubleColon)) {
const node = this.startNodeAt(startPos, startLoc); const node = this.startNodeAt(startPos, startLoc);
@ -695,7 +689,7 @@ export default class ExpressionParser extends LValParser {
node.arguments = this.parseCallExpressionArguments( node.arguments = this.parseCallExpressionArguments(
tt.parenR, tt.parenR,
maybeAsyncArrow, state.maybeAsyncArrow,
base.type === "Import", base.type === "Import",
base.type !== "Super", base.type !== "Super",
); );
@ -705,7 +699,7 @@ export default class ExpressionParser extends LValParser {
this.finishOptionalCallExpression(node); this.finishOptionalCallExpression(node);
} }
if (maybeAsyncArrow && this.shouldParseAsyncArrow()) { if (state.maybeAsyncArrow && this.shouldParseAsyncArrow()) {
state.stop = true; state.stop = true;
this.checkCommaAfterRestFromSpread(); this.checkCommaAfterRestFromSpread();

View File

@ -2634,7 +2634,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc: Position, startLoc: Position,
noCalls: ?boolean, noCalls: ?boolean,
subscriptState: N.ParseSubscriptState, subscriptState: N.ParseSubscriptState,
maybeAsyncArrow: boolean,
): N.Expression { ): N.Expression {
if (this.match(tt.questionDot) && this.isLookaheadRelational("<")) { if (this.match(tt.questionDot) && this.isLookaheadRelational("<")) {
this.expectPlugin("optionalChaining"); this.expectPlugin("optionalChaining");
@ -2687,7 +2686,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc, startLoc,
noCalls, noCalls,
subscriptState, subscriptState,
maybeAsyncArrow,
); );
} }

View File

@ -1568,7 +1568,6 @@ export default (superClass: Class<Parser>): Class<Parser> =>
startLoc: Position, startLoc: Position,
noCalls: ?boolean, noCalls: ?boolean,
state: N.ParseSubscriptState, state: N.ParseSubscriptState,
maybeAsyncArrow: boolean,
): N.Expression { ): N.Expression {
if (!this.hasPrecedingLineBreak() && this.match(tt.bang)) { if (!this.hasPrecedingLineBreak() && this.match(tt.bang)) {
this.state.exprAllowed = false; this.state.exprAllowed = false;
@ -1631,14 +1630,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (result) return result; if (result) return result;
} }
return super.parseSubscript( return super.parseSubscript(base, startPos, startLoc, noCalls, state);
base,
startPos,
startLoc,
noCalls,
state,
maybeAsyncArrow,
);
} }
parseNewArguments(node: N.NewExpression): void { parseNewArguments(node: N.NewExpression): void {

View File

@ -1447,5 +1447,6 @@ export type Placeholder<N: PlaceholderTypes> = NodeBase & {
export type ParseSubscriptState = { export type ParseSubscriptState = {
optionalChainMember: boolean, optionalChainMember: boolean,
maybeAsyncArrow: boolean,
stop: boolean, stop: boolean,
}; };

View File

@ -0,0 +1,3 @@
// @flow
true ? async.waterfall() : null;

View File

@ -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
}
}
}
]
}