Skip to content

Commit

Permalink
Refactor yield await classification (#12230)
Browse files Browse the repository at this point in the history
* fix: incomplete ParamKind declaration

* refactor: add expression scope handler

* test: update test262 allowlist

* chore: cleanup

* fix: push expression scope for function body

* fix: push new expression scope for initializer and static block

* test: add more test cases

* fix flow error

* refactor: remove unecessary expression scope

* fix: parameter initializer error should not cross expression scope boundary

* chore: cleanup outdated comments

* fix: do not record async arrow error on ParameterDeclaration

* Update packages/babel-parser/src/util/expression-scope.js

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>

* polish: clear ancestry declaration error on validate

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
JLHwung and nicolo-ribaudo committed Oct 26, 2020
1 parent faaebfe commit 2782a54
Show file tree
Hide file tree
Showing 63 changed files with 1,532 additions and 199 deletions.
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Options } from "../options";
import type State from "../tokenizer/state";
import type { PluginsMap } from "./index";
import type ScopeHandler from "../util/scope";
import type ExpressionScopeHandler from "../util/expression-scope";
import type ClassScopeHandler from "../util/class-scope";
import type ProductionParameterHandler from "../util/production-parameter";

Expand All @@ -14,6 +15,7 @@ export default class BaseParser {
scope: ScopeHandler<*>;
classScope: ClassScopeHandler;
prodParam: ProductionParameterHandler;
expressionScope: ExpressionScopeHandler;
plugins: PluginsMap;
filename: ?string;
sawUnambiguousESM: boolean = false;
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-parser/src/parser/error-message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// @flow
/* eslint sort-keys: "error" */

/**
* @module parser/error-message
*/

// The Errors key follows https://cs.chromium.org/chromium/src/v8/src/common/message-template.h unless it does not exist
export const ErrorMessages = Object.freeze({
AccessorIsGenerator: "A %0ter cannot be a generator",
Expand Down
143 changes: 37 additions & 106 deletions packages/babel-parser/src/parser/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ import {
PARAM,
functionFlags,
} from "../util/production-parameter";
import {
newArrowHeadScope,
newAsyncArrowScope,
newExpressionScope,
} from "../util/expression-scope.js";
import { Errors } from "./error";

export default class ExpressionParser extends LValParser {
Expand Down Expand Up @@ -581,15 +586,10 @@ export default class ExpressionParser extends LValParser {
stop: false,
};
do {
const oldMaybeInAsyncArrowHead = this.state.maybeInAsyncArrowHead;
if (state.maybeAsyncArrow) {
this.state.maybeInAsyncArrowHead = true;
}
base = this.parseSubscript(base, startPos, startLoc, noCalls, state);

// After parsing a subscript, this isn't "async" for sure.
state.maybeAsyncArrow = false;
this.state.maybeInAsyncArrowHead = oldMaybeInAsyncArrowHead;
} while (!state.stop);
return base;
}
Expand Down Expand Up @@ -714,16 +714,15 @@ export default class ExpressionParser extends LValParser {
optional: boolean,
): N.Expression {
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.maybeInArrowParameters = true;
this.state.yieldPos = -1;
this.state.awaitPos = -1;

this.next(); // eat `(`

let node = this.startNodeAt(startPos, startLoc);
node.callee = base;
if (state.maybeAsyncArrow) {
this.expressionScope.enter(newAsyncArrowScope());
}

if (state.optionalChainMember) {
node.optional = optional;
Expand All @@ -743,47 +742,17 @@ export default class ExpressionParser extends LValParser {

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

this.expressionScope.validateAsPattern();
this.expressionScope.exit();
node = this.parseAsyncArrowFromCallExpression(
this.startNodeAt(startPos, startLoc),
node,
);
this.checkYieldAwaitInDefaultParams();
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
} else {
this.toReferencedArguments(node);

// We keep the old value if it isn't null, for cases like
// (x = async(yield)) => {}
//
// Hi developer of the future :) If you are implementing generator
// arrow functions, please read the note below about "await" and
// verify if the same logic is needed for yield.
if (oldYieldPos !== -1) this.state.yieldPos = oldYieldPos;

// Await is trickier than yield. When parsing a possible arrow function
// (e.g. something starting with `async(`) we don't know if its possible
// parameters will actually be inside an async arrow function or if it is
// a normal call expression.
// If it ended up being a call expression, if we are in a context where
// await expression are disallowed (and thus "await" is an identifier)
// we must be careful not to leak this.state.awaitPos to an even outer
// context, where "await" could not be an identifier.
// For example, this code is valid because "await" isn't directly inside
// an async function:
//
// async function a() {
// function b(param = async (await)) {
// }
// }
//
if (
(!this.isAwaitAllowed() && !oldMaybeInArrowParameters) ||
oldAwaitPos !== -1
) {
this.state.awaitPos = oldAwaitPos;
if (state.maybeAsyncArrow) {
this.expressionScope.exit();
}
this.toReferencedArguments(node);
}

this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
Expand Down Expand Up @@ -1206,24 +1175,15 @@ export default class ExpressionParser extends LValParser {
// async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] [no LineTerminator here] => AsyncConciseBody[?In]
parseAsyncArrowUnaryFunction(id: N.Expression): N.ArrowFunctionExpression {
const node = this.startNodeAtNode(id);
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldMaybeInAsyncArrowHead = this.state.maybeInAsyncArrowHead;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.maybeInArrowParameters = true;
this.state.maybeInAsyncArrowHead = true;
this.state.yieldPos = -1;
this.state.awaitPos = -1;
// We don't need to push a new ParameterDeclarationScope here since we are sure
// 1) it is an async arrow, 2) no biding pattern is allowed in params
this.prodParam.enter(functionFlags(true, this.prodParam.hasYield));
const params = [this.parseIdentifier()];
this.prodParam.exit();
if (this.hasPrecedingLineBreak()) {
this.raise(this.state.pos, Errors.LineTerminatorBeforeArrow);
}
this.expect(tt.arrow);
this.checkYieldAwaitInDefaultParams();
this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.maybeInAsyncArrowHead = oldMaybeInAsyncArrowHead;
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
// let foo = async bar => {};
this.parseArrowExpression(node, params, true);
return node;
Expand Down Expand Up @@ -1393,14 +1353,11 @@ export default class ExpressionParser extends LValParser {

let val;
this.next(); // eat `(`
this.expressionScope.enter(newArrowHeadScope());

const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
const oldInFSharpPipelineDirectBody = this.state.inFSharpPipelineDirectBody;
this.state.maybeInArrowParameters = true;
this.state.yieldPos = -1;
this.state.awaitPos = -1;
this.state.inFSharpPipelineDirectBody = false;

const innerStartPos = this.state.start;
Expand Down Expand Up @@ -1462,12 +1419,8 @@ export default class ExpressionParser extends LValParser {
this.shouldParseArrow() &&
(arrowNode = this.parseArrow(arrowNode))
) {
if (!this.isAwaitAllowed() && !this.state.maybeInAsyncArrowHead) {
this.state.awaitPos = oldAwaitPos;
}
this.checkYieldAwaitInDefaultParams();
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
this.expressionScope.validateAsPattern();
this.expressionScope.exit();
for (const param of exprList) {
if (param.extra && param.extra.parenthesized) {
this.unexpected(param.extra.parenStart);
Expand All @@ -1477,11 +1430,7 @@ export default class ExpressionParser extends LValParser {
this.parseArrowExpression(arrowNode, exprList, false);
return arrowNode;
}

// We keep the old value if it isn't null, for cases like
// (x = (yield)) => {}
if (oldYieldPos !== -1) this.state.yieldPos = oldYieldPos;
if (oldAwaitPos !== -1) this.state.awaitPos = oldAwaitPos;
this.expressionScope.exit();

if (!exprList.length) {
this.unexpected(this.state.lastTokStart);
Expand Down Expand Up @@ -2016,11 +1965,6 @@ export default class ExpressionParser extends LValParser {
type: string,
inClassScope: boolean = false,
): T {
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.yieldPos = -1;
this.state.awaitPos = -1;

this.initFunction(node, isAsync);
node.generator = !!isGenerator;
const allowModifiers = isConstructor; // For TypeScript parameter properties
Expand All @@ -2036,9 +1980,6 @@ export default class ExpressionParser extends LValParser {
this.prodParam.exit();
this.scope.exit();

this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;

return node;
}

Expand Down Expand Up @@ -2089,23 +2030,17 @@ export default class ExpressionParser extends LValParser {
this.prodParam.enter(flags);
this.initFunction(node, isAsync);
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;

if (params) {
this.state.maybeInArrowParameters = true;
this.setArrowFunctionParameters(node, params, trailingCommaPos);
}
this.state.maybeInArrowParameters = false;
this.state.yieldPos = -1;
this.state.awaitPos = -1;
this.parseFunctionBody(node, true);

this.prodParam.exit();
this.scope.exit();
this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;

return this.finishNode(node, "ArrowFunctionExpression");
}
Expand Down Expand Up @@ -2135,8 +2070,7 @@ export default class ExpressionParser extends LValParser {
isMethod?: boolean = false,
): void {
const isExpression = allowExpression && !this.match(tt.braceL);
const oldInParameters = this.state.inParameters;
this.state.inParameters = false;
this.expressionScope.enter(newExpressionScope());

if (isExpression) {
// https://tc39.es/ecma262/#prod-ExpressionBody
Expand Down Expand Up @@ -2196,10 +2130,9 @@ export default class ExpressionParser extends LValParser {
},
);
this.prodParam.exit();
this.expressionScope.exit();
this.state.labels = oldLabels;
}

this.state.inParameters = oldInParameters;
}

isSimpleParamList(
Expand Down Expand Up @@ -2381,12 +2314,11 @@ export default class ExpressionParser extends LValParser {
if (this.prodParam.hasAwait) {
this.raise(startLoc, Errors.AwaitBindingIdentifier);
return;
}
if (
this.state.awaitPos === -1 &&
(this.state.maybeInAsyncArrowHead || this.isAwaitAllowed())
) {
this.state.awaitPos = this.state.start;
} else {
this.expressionScope.recordAsyncArrowParametersError(
startLoc,
Errors.AwaitBindingIdentifier,
);
}
}

Expand Down Expand Up @@ -2434,11 +2366,11 @@ export default class ExpressionParser extends LValParser {

this.next();

if (this.state.inParameters) {
this.raise(node.start, Errors.AwaitExpressionFormalParameter);
} else if (this.state.awaitPos === -1) {
this.state.awaitPos = node.start;
}
this.expressionScope.recordParameterInitializerError(
node.start,
Errors.AwaitExpressionFormalParameter,
);

if (this.eat(tt.star)) {
this.raise(node.start, Errors.ObsoleteAwaitStar);
}
Expand Down Expand Up @@ -2478,11 +2410,10 @@ export default class ExpressionParser extends LValParser {
parseYield(): N.YieldExpression {
const node = this.startNode();

if (this.state.inParameters) {
this.raise(node.start, Errors.YieldInParameter);
} else if (this.state.yieldPos === -1) {
this.state.yieldPos = node.start;
}
this.expressionScope.recordParameterInitializerError(
node.start,
Errors.YieldInParameter,
);

this.next();
if (
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-parser/src/parser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import StatementParser from "./statement";
import { SCOPE_PROGRAM } from "../util/scopeflags";
import ScopeHandler from "../util/scope";
import ClassScopeHandler from "../util/class-scope";
import ExpressionScopeHandler from "../util/expression-scope";
import ProductionParameterHandler, {
PARAM_AWAIT,
PARAM,
Expand All @@ -34,6 +35,7 @@ export default class Parser extends StatementParser {
this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
this.prodParam = new ProductionParameterHandler();
this.classScope = new ClassScopeHandler(this.raise.bind(this));
this.expressionScope = new ExpressionScopeHandler(this.raise.bind(this));
this.plugins = pluginsMap(this.options.plugins);
this.filename = options.sourceFilename;
}
Expand Down

0 comments on commit 2782a54

Please sign in to comment.