Simplify await and yield tracking in params (#9405)

This commit is contained in:
Daniel Tschinder 2019-02-04 13:01:17 -08:00 committed by Nicolò Ribaudo
parent fe71154626
commit 344d35bbe9
20 changed files with 90 additions and 82 deletions

View File

@ -597,9 +597,11 @@ export default class ExpressionParser extends LValParser {
return this.finishNode(node, "MemberExpression"); return this.finishNode(node, "MemberExpression");
} else if (!noCalls && this.match(tt.parenL)) { } else if (!noCalls && this.match(tt.parenL)) {
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldYOAIPAP = this.state.yieldOrAwaitInPossibleArrowParameters; const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.maybeInArrowParameters = true; this.state.maybeInArrowParameters = true;
this.state.yieldOrAwaitInPossibleArrowParameters = null; this.state.yieldPos = 0;
this.state.awaitPos = 0;
const possibleAsync = this.atPossibleAsync(base); const possibleAsync = this.atPossibleAsync(base);
this.next(); this.next();
@ -630,14 +632,16 @@ export default class ExpressionParser extends LValParser {
this.startNodeAt(startPos, startLoc), this.startNodeAt(startPos, startLoc),
node, node,
); );
this.state.yieldOrAwaitInPossibleArrowParameters = oldYOAIPAP; this.checkYieldAwaitInDefaultParams();
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
} else { } else {
this.toReferencedListDeep(node.arguments); this.toReferencedListDeep(node.arguments);
// We keep the old value if it isn't null, for cases like // We keep the old value if it isn't null, for cases like
// (x = async(yield)) => {} // (x = async(yield)) => {}
this.state.yieldOrAwaitInPossibleArrowParameters = this.state.yieldPos = oldYieldPos || this.state.yieldPos;
this.state.yieldOrAwaitInPossibleArrowParameters || oldYOAIPAP; this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
} }
this.state.maybeInArrowParameters = oldMaybeInArrowParameters; this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
@ -873,25 +877,19 @@ export default class ExpressionParser extends LValParser {
this.match(tt.name) && this.match(tt.name) &&
!this.canInsertSemicolon() !this.canInsertSemicolon()
) { ) {
const oldYOAIPAP = this.state.yieldOrAwaitInPossibleArrowParameters;
const oldInAsync = this.state.inAsync; const oldInAsync = this.state.inAsync;
this.state.yieldOrAwaitInPossibleArrowParameters = null;
this.state.inAsync = true; this.state.inAsync = true;
const params = [this.parseIdentifier()]; const params = [this.parseIdentifier()];
this.expect(tt.arrow); this.expect(tt.arrow);
// let foo = async bar => {}; // let foo = async bar => {};
this.parseArrowExpression(node, params, true); this.parseArrowExpression(node, params, true);
this.state.yieldOrAwaitInPossibleArrowParameters = oldYOAIPAP;
this.state.inAsync = oldInAsync; this.state.inAsync = oldInAsync;
return node; return node;
} }
if (canBeArrow && this.match(tt.arrow) && !this.canInsertSemicolon()) { if (canBeArrow && this.match(tt.arrow) && !this.canInsertSemicolon()) {
this.next(); this.next();
const oldYOAIPAP = this.state.yieldOrAwaitInPossibleArrowParameters; this.parseArrowExpression(node, [id], false);
this.state.yieldOrAwaitInPossibleArrowParameters = null;
this.parseArrowExpression(node, [id]);
this.state.yieldOrAwaitInPossibleArrowParameters = oldYOAIPAP;
return node; return node;
} }
@ -1172,9 +1170,11 @@ export default class ExpressionParser extends LValParser {
this.expect(tt.parenL); this.expect(tt.parenL);
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldYOAIPAP = this.state.yieldOrAwaitInPossibleArrowParameters; const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.maybeInArrowParameters = true; this.state.maybeInArrowParameters = true;
this.state.yieldOrAwaitInPossibleArrowParameters = null; this.state.yieldPos = 0;
this.state.awaitPos = 0;
const innerStartPos = this.state.start; const innerStartPos = this.state.start;
const innerStartLoc = this.state.startLoc; const innerStartLoc = this.state.startLoc;
@ -1235,21 +1235,23 @@ export default class ExpressionParser extends LValParser {
this.shouldParseArrow() && this.shouldParseArrow() &&
(arrowNode = this.parseArrow(arrowNode)) (arrowNode = this.parseArrow(arrowNode))
) { ) {
this.checkYieldAwaitInDefaultParams();
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
for (const param of exprList) { for (const param of exprList) {
if (param.extra && param.extra.parenthesized) { if (param.extra && param.extra.parenthesized) {
this.unexpected(param.extra.parenStart); this.unexpected(param.extra.parenStart);
} }
} }
this.parseArrowExpression(arrowNode, exprList); this.parseArrowExpression(arrowNode, exprList, false);
this.state.yieldOrAwaitInPossibleArrowParameters = oldYOAIPAP;
return arrowNode; return arrowNode;
} }
// We keep the old value if it isn't null, for cases like // We keep the old value if it isn't null, for cases like
// (x = (yield)) => {} // (x = (yield)) => {}
this.state.yieldOrAwaitInPossibleArrowParameters = this.state.yieldPos = oldYieldPos || this.state.yieldPos;
this.state.yieldOrAwaitInPossibleArrowParameters || oldYOAIPAP; this.state.awaitPos = oldAwaitPos || this.state.awaitPos;
if (!exprList.length) { if (!exprList.length) {
this.unexpected(this.state.lastTokStart); this.unexpected(this.state.lastTokStart);
@ -1717,21 +1719,28 @@ export default class ExpressionParser extends LValParser {
const oldInMethod = this.state.inMethod; const oldInMethod = this.state.inMethod;
const oldInAsync = this.state.inAsync; const oldInAsync = this.state.inAsync;
const oldInGenerator = this.state.inGenerator; const oldInGenerator = this.state.inGenerator;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.inFunction = true; this.state.inFunction = true;
this.state.inMethod = node.kind || true; this.state.inMethod = node.kind || true;
this.state.inAsync = isAsync; this.state.inAsync = isAsync;
this.state.inGenerator = isGenerator; this.state.inGenerator = isGenerator;
this.state.yieldPos = 0;
this.state.awaitPos = 0;
this.initFunction(node, isAsync); this.initFunction(node, isAsync);
node.generator = !!isGenerator; node.generator = !!isGenerator;
const allowModifiers = isConstructor; // For TypeScript parameter properties const allowModifiers = isConstructor; // For TypeScript parameter properties
this.parseFunctionParams((node: any), allowModifiers); this.parseFunctionParams((node: any), allowModifiers);
this.checkYieldAwaitInDefaultParams();
this.parseFunctionBodyAndFinish(node, type); this.parseFunctionBodyAndFinish(node, type);
this.state.inFunction = oldInFunc; this.state.inFunction = oldInFunc;
this.state.inMethod = oldInMethod; this.state.inMethod = oldInMethod;
this.state.inAsync = oldInAsync; this.state.inAsync = oldInAsync;
this.state.inGenerator = oldInGenerator; this.state.inGenerator = oldInGenerator;
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
return node; return node;
} }
@ -1741,44 +1750,33 @@ export default class ExpressionParser extends LValParser {
// assignable list. // assignable list.
parseArrowExpression( parseArrowExpression(
node: N.ArrowFunctionExpression, node: N.ArrowFunctionExpression,
params?: ?(N.Expression[]), params: ?(N.Expression[]),
isAsync?: boolean = false, isAsync: boolean,
): N.ArrowFunctionExpression { ): N.ArrowFunctionExpression {
// if we got there, it's no more "yield in possible arrow parameters"; this.initFunction(node, isAsync);
// it's just "yield in arrow parameters"
const yOAIPAP = this.state.yieldOrAwaitInPossibleArrowParameters;
if (yOAIPAP) {
if (yOAIPAP.type === "YieldExpression") {
this.raise(
yOAIPAP.start,
"yield is not allowed in the parameters of an arrow function" +
" inside a generator",
);
} else {
this.raise(
yOAIPAP.start,
"await is not allowed in the parameters of an arrow function" +
" inside an async function",
);
}
}
const oldInFunc = this.state.inFunction; const oldInFunc = this.state.inFunction;
this.state.inFunction = true;
this.initFunction(node, isAsync);
if (params) this.setArrowFunctionParameters(node, params);
const oldInAsync = this.state.inAsync; const oldInAsync = this.state.inAsync;
const oldInGenerator = this.state.inGenerator; const oldInGenerator = this.state.inGenerator;
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.inFunction = true;
this.state.inAsync = isAsync; this.state.inAsync = isAsync;
this.state.inGenerator = false; this.state.inGenerator = false;
this.state.maybeInArrowParameters = false; this.state.maybeInArrowParameters = false;
this.state.yieldPos = 0;
this.state.awaitPos = 0;
if (params) this.setArrowFunctionParameters(node, params);
this.parseFunctionBody(node, true); this.parseFunctionBody(node, true);
this.state.inAsync = oldInAsync; this.state.inAsync = oldInAsync;
this.state.inGenerator = oldInGenerator; this.state.inGenerator = oldInGenerator;
this.state.inFunction = oldInFunc; this.state.inFunction = oldInFunc;
this.state.maybeInArrowParameters = oldMaybeInArrowParameters; this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
return this.finishNode(node, "ArrowFunctionExpression"); return this.finishNode(node, "ArrowFunctionExpression");
} }
@ -2034,16 +2032,10 @@ export default class ExpressionParser extends LValParser {
// Parses await expression inside async function. // Parses await expression inside async function.
parseAwait(): N.AwaitExpression { parseAwait(): N.AwaitExpression {
const node = this.startNode(); if (!this.state.awaitPos) {
this.state.awaitPos = this.state.start;
if (
this.state.maybeInArrowParameters &&
// We only set yieldOrAwaitInPossibleArrowParameters if we haven't already
// found a possible invalid AwaitExpression.
!this.state.yieldOrAwaitInPossibleArrowParameters
) {
this.state.yieldOrAwaitInPossibleArrowParameters = node;
} }
const node = this.startNode();
this.next(); this.next();
@ -2067,19 +2059,14 @@ export default class ExpressionParser extends LValParser {
// Parses yield expression inside generator. // Parses yield expression inside generator.
parseYield(noIn?: ?boolean): N.YieldExpression { parseYield(noIn?: ?boolean): N.YieldExpression {
if (!this.state.yieldPos) {
this.state.yieldPos = this.state.start;
}
const node = this.startNode(); const node = this.startNode();
if (this.state.inParameters) { if (this.state.inParameters) {
this.raise(node.start, "yield is not allowed in generator parameters"); this.raise(node.start, "yield is not allowed in generator parameters");
} }
if (
this.state.maybeInArrowParameters &&
// We only set yieldOrAwaitInPossibleArrowParameters if we haven't already
// found a possible invalid YieldExpression.
!this.state.yieldOrAwaitInPossibleArrowParameters
) {
this.state.yieldOrAwaitInPossibleArrowParameters = node;
}
this.next(); this.next();
if ( if (

View File

@ -969,9 +969,13 @@ export default class StatementParser extends ExpressionParser {
const oldInAsync = this.state.inAsync; const oldInAsync = this.state.inAsync;
const oldInGenerator = this.state.inGenerator; const oldInGenerator = this.state.inGenerator;
const oldInClassProperty = this.state.inClassProperty; const oldInClassProperty = this.state.inClassProperty;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.inFunction = true; this.state.inFunction = true;
this.state.inMethod = false; this.state.inMethod = false;
this.state.inClassProperty = false; this.state.inClassProperty = false;
this.state.yieldPos = 0;
this.state.awaitPos = 0;
this.initFunction(node, isAsync); this.initFunction(node, isAsync);
@ -1021,6 +1025,8 @@ export default class StatementParser extends ExpressionParser {
this.state.inAsync = oldInAsync; this.state.inAsync = oldInAsync;
this.state.inGenerator = oldInGenerator; this.state.inGenerator = oldInGenerator;
this.state.inClassProperty = oldInClassProperty; this.state.inClassProperty = oldInClassProperty;
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
return node; return node;
} }
@ -1037,6 +1043,7 @@ export default class StatementParser extends ExpressionParser {
); );
this.state.inParameters = oldInParameters; this.state.inParameters = oldInParameters;
this.checkYieldAwaitInDefaultParams();
} }
// Parse a class declaration or literal (depending on the // Parse a class declaration or literal (depending on the

View File

@ -147,4 +147,22 @@ export default class UtilParser extends Tokenizer {
); );
} }
} }
checkYieldAwaitInDefaultParams() {
if (
this.state.yieldPos &&
(!this.state.awaitPos || this.state.yieldPos < this.state.awaitPos)
) {
this.raise(
this.state.yieldPos,
"Yield cannot be used as name inside a generator function",
);
}
if (this.state.awaitPos) {
this.raise(
this.state.awaitPos,
"Await cannot be used as name inside an async function",
);
}
}
} }

View File

@ -102,13 +102,9 @@ export default class State {
// where @foo belongs to the outer class and @bar to the inner // where @foo belongs to the outer class and @bar to the inner
decoratorStack: Array<Array<N.Decorator>> = [[]]; decoratorStack: Array<Array<N.Decorator>> = [[]];
// The first yield or await expression inside parenthesized expressions // Positions to delayed-check that yield/await does not exist in default parameters.
// and arrow function parameters. It is used to disallow yield and await in yieldPos: number = 0;
// arrow function parameters. awaitPos: number = 0;
yieldOrAwaitInPossibleArrowParameters:
| N.YieldExpression
| N.AwaitExpression
| null = null;
// Token store. // Token store.
tokens: Array<Token | N.Comment> = []; tokens: Array<Token | N.Comment> = [];

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:7)" "throws": "Yield cannot be used as name inside a generator function (2:7)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:15)" "throws": "Yield cannot be used as name inside a generator function (2:15)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:7)" "throws": "Yield cannot be used as name inside a generator function (2:7)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:17)" "throws": "Yield cannot be used as name inside a generator function (2:17)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:8)" "throws": "Yield cannot be used as name inside a generator function (2:8)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:8)" "throws": "Yield cannot be used as name inside a generator function (2:8)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:3)" "throws": "Yield cannot be used as name inside a generator function (2:3)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:3)" "throws": "Yield cannot be used as name inside a generator function (2:3)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (2:9)" "throws": "Yield cannot be used as name inside a generator function (2:9)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "await is not allowed in the parameters of an arrow function inside an async function (2:23)" "throws": "Await cannot be used as name inside an async function (2:23)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "await is not allowed in the parameters of an arrow function inside an async function (2:7)" "throws": "Await cannot be used as name inside an async function (2:7)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "await is not allowed in the parameters of an arrow function inside an async function (2:13)" "throws": "Await cannot be used as name inside an async function (2:13)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (1:21)" "throws": "Yield cannot be used as name inside a generator function (1:21)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (1:16)" "throws": "Yield cannot be used as name inside a generator function (1:16)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (1:25)" "throws": "Yield cannot be used as name inside a generator function (1:25)"
} }

View File

@ -1,3 +1,3 @@
{ {
"throws": "yield is not allowed in the parameters of an arrow function inside a generator (1:21)" "throws": "Yield cannot be used as name inside a generator function (1:21)"
} }