Skip to content

Commit

Permalink
address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Jan 4, 2020
1 parent 19e99ae commit e740cb1
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion packages/babel-parser/src/parser/base.js
Expand Up @@ -11,7 +11,7 @@ export default class BaseParser {
options: Options;
inModule: boolean;
scope: ScopeHandler<*>;
param: ProductionParameterHandler;
prodParam: ProductionParameterHandler;
plugins: PluginsMap;
filename: ?string;
sawUnambiguousESM: boolean = false;
Expand Down
30 changes: 15 additions & 15 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -42,7 +42,7 @@ import {
} from "../util/scopeflags";
import {
PARAM_AWAIT,
PARAM_,
PARAM,
functionFlags,
} from "../util/production-parameter";

Expand Down Expand Up @@ -101,12 +101,12 @@ export default class ExpressionParser extends LValParser {

// Convenience method to parse an Expression only
getExpression(): N.Expression {
let paramFlags = PARAM_;
let paramFlags = PARAM;
if (this.hasPlugin("topLevelAwait") && this.inModule) {
paramFlags |= PARAM_AWAIT;
}
this.scope.enter(SCOPE_PROGRAM);
this.param.enter(paramFlags);
this.prodParam.enter(paramFlags);
this.nextToken();
const expr = this.parseExpression();
if (!this.match(tt.eof)) {
Expand Down Expand Up @@ -168,7 +168,7 @@ export default class ExpressionParser extends LValParser {
const startPos = this.state.start;
const startLoc = this.state.startLoc;
if (this.isContextual("yield")) {
if (this.param.hasYield) {
if (this.prodParam.hasYield) {
let left = this.parseYield(noIn);
if (afterLeftParse) {
left = afterLeftParse.call(this, left, startPos, startLoc);
Expand Down Expand Up @@ -357,7 +357,7 @@ export default class ExpressionParser extends LValParser {
if (
this.match(tt.name) &&
this.state.value === "await" &&
this.param.hasAwait
this.prodParam.hasAwait
) {
throw this.raise(
this.state.start,
Expand Down Expand Up @@ -1169,7 +1169,7 @@ export default class ExpressionParser extends LValParser {
this.next();
meta = this.createIdentifier(meta, "function");

if (this.param.hasYield && this.eat(tt.dot)) {
if (this.prodParam.hasYield && this.eat(tt.dot)) {
return this.parseMetaProperty(node, meta, "sent");
}
return this.parseFunction(node);
Expand Down Expand Up @@ -1849,10 +1849,10 @@ export default class ExpressionParser extends LValParser {
(inClassScope ? SCOPE_CLASS : 0) |
(allowDirectSuper ? SCOPE_DIRECT_SUPER : 0),
);
this.param.enter(functionFlags(isAsync, node.generator));
this.prodParam.enter(functionFlags(isAsync, node.generator));
this.parseFunctionParams((node: any), allowModifiers);
this.parseFunctionBodyAndFinish(node, type, true);
this.param.exit();
this.prodParam.exit();
this.scope.exit();

this.state.yieldPos = oldYieldPos;
Expand All @@ -1871,7 +1871,7 @@ export default class ExpressionParser extends LValParser {
trailingCommaPos: ?number,
): N.ArrowFunctionExpression {
this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
this.param.enter(functionFlags(isAsync, false));
this.prodParam.enter(functionFlags(isAsync, false));
this.initFunction(node, isAsync);

const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
Expand All @@ -1884,7 +1884,7 @@ export default class ExpressionParser extends LValParser {
if (params) this.setArrowFunctionParameters(node, params, trailingCommaPos);
this.parseFunctionBody(node, true);

this.param.exit();
this.prodParam.exit();
this.scope.exit();
this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.yieldPos = oldYieldPos;
Expand Down Expand Up @@ -2158,7 +2158,7 @@ export default class ExpressionParser extends LValParser {
checkKeywords: boolean,
isBinding: boolean,
): void {
if (this.param.hasYield && word === "yield") {
if (this.prodParam.hasYield && word === "yield") {
this.raise(
startLoc,
"Can not use 'yield' as identifier inside a generator",
Expand All @@ -2167,7 +2167,7 @@ export default class ExpressionParser extends LValParser {
}

if (word === "await") {
if (this.param.hasAwait) {
if (this.prodParam.hasAwait) {
this.raise(
startLoc,
"Can not use 'await' as identifier inside an async function",
Expand Down Expand Up @@ -2205,7 +2205,7 @@ export default class ExpressionParser extends LValParser {
: isStrictReservedWord;

if (reservedTest(word, this.inModule)) {
if (!this.param.hasAwait && word === "await") {
if (!this.prodParam.hasAwait && word === "await") {
this.raise(
startLoc,
"Can not use keyword 'await' outside an async function",
Expand All @@ -2217,10 +2217,10 @@ export default class ExpressionParser extends LValParser {
}

isAwaitAllowed(): boolean {
if (this.scope.inFunction) return this.param.hasAwait;
if (this.scope.inFunction) return this.prodParam.hasAwait;
if (this.options.allowAwaitOutsideFunction) return true;
if (this.hasPlugin("topLevelAwait")) {
return this.inModule && this.param.hasAwait;
return this.inModule && this.prodParam.hasAwait;
}
return false;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-parser/src/parser/index.js
Expand Up @@ -9,7 +9,7 @@ import { SCOPE_PROGRAM } from "../util/scopeflags";
import ScopeHandler from "../util/scope";
import ProductionParameterHandler, {
PARAM_AWAIT,
PARAM_,
PARAM,
} from "../util/production-parameter";

export type PluginsMap = Map<string, { [string]: any }>;
Expand All @@ -29,7 +29,7 @@ export default class Parser extends StatementParser {
this.options = options;
this.inModule = this.options.sourceType === "module";
this.scope = new ScopeHandler(this.raise.bind(this), this.inModule);
this.param = new ProductionParameterHandler();
this.prodParam = new ProductionParameterHandler();
this.plugins = pluginsMap(this.options.plugins);
this.filename = options.sourceFilename;
}
Expand All @@ -40,12 +40,12 @@ export default class Parser extends StatementParser {
}

parse(): File {
let paramFlags = PARAM_;
let paramFlags = PARAM;
if (this.hasPlugin("topLevelAwait") && this.inModule) {
paramFlags |= PARAM_AWAIT;
}
this.scope.enter(SCOPE_PROGRAM);
this.param.enter(paramFlags);
this.prodParam.enter(paramFlags);
const file = this.startNode();
const program = this.startNode();
this.nextToken();
Expand Down
14 changes: 7 additions & 7 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -22,7 +22,7 @@ import {
SCOPE_SUPER,
type BindingTypes,
} from "../util/scopeflags";
import { PARAM_, functionFlags } from "../util/production-parameter";
import { PARAM, functionFlags } from "../util/production-parameter";

const loopLabel = { kind: "loop" },
switchLabel = { kind: "switch" };
Expand Down Expand Up @@ -1055,7 +1055,7 @@ export default class StatementParser extends ExpressionParser {
this.state.yieldPos = -1;
this.state.awaitPos = -1;
this.scope.enter(SCOPE_FUNCTION);
this.param.enter(functionFlags(isAsync, node.generator));
this.prodParam.enter(functionFlags(isAsync, node.generator));

if (!isStatement) {
node.id = this.parseFunctionId();
Expand All @@ -1074,7 +1074,7 @@ export default class StatementParser extends ExpressionParser {
);
});

this.param.exit();
this.prodParam.exit();
this.scope.exit();

if (isStatement && !isHangingStatement) {
Expand Down Expand Up @@ -1577,11 +1577,11 @@ export default class StatementParser extends ExpressionParser {
): N.ClassPrivateProperty {
this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
// [In] production parameter is tracked in parseMaybeAssign
this.param.enter(PARAM_);
this.prodParam.enter(PARAM);

node.value = this.eat(tt.eq) ? this.parseMaybeAssign() : null;
this.semicolon();
this.param.exit();
this.prodParam.exit();

this.scope.exit();

Expand All @@ -1595,7 +1595,7 @@ export default class StatementParser extends ExpressionParser {

this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
// [In] production parameter is tracked in parseMaybeAssign
this.param.enter(PARAM_);
this.prodParam.enter(PARAM);

if (this.match(tt.eq)) {
this.expectPlugin("classProperties");
Expand All @@ -1606,7 +1606,7 @@ export default class StatementParser extends ExpressionParser {
}
this.semicolon();

this.param.exit();
this.prodParam.exit();
this.scope.exit();

return this.finishNode(node, "ClassProperty");
Expand Down
14 changes: 7 additions & 7 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -25,7 +25,7 @@ import {
} from "../../util/scopeflags";
import TypeScriptScopeHandler from "./scope";
import * as charCodes from "charcodes";
import { PARAM_ } from "../../util/production-parameter";
import { PARAM } from "../../util/production-parameter";

type TsModifier =
| "readonly"
Expand Down Expand Up @@ -1265,9 +1265,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.body = inner;
} else {
this.scope.enter(SCOPE_TS_MODULE);
this.param.enter(PARAM_);
this.prodParam.enter(PARAM);
node.body = this.tsParseModuleBlock();
this.param.exit();
this.prodParam.exit();
this.scope.exit();
}
return this.finishNode(node, "TSModuleDeclaration");
Expand All @@ -1286,9 +1286,9 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
if (this.match(tt.braceL)) {
this.scope.enter(SCOPE_TS_MODULE);
this.param.enter(PARAM_);
this.prodParam.enter(PARAM);
node.body = this.tsParseModuleBlock();
this.param.exit();
this.prodParam.exit();
this.scope.exit();
} else {
this.semicolon();
Expand Down Expand Up @@ -1443,13 +1443,13 @@ export default (superClass: Class<Parser>): Class<Parser> =>
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
if (this.match(tt.braceL)) {
this.scope.enter(SCOPE_TS_MODULE);
this.param.enter(PARAM_);
this.prodParam.enter(PARAM);
const mod: N.TsModuleDeclaration = node;
mod.global = true;
mod.id = expr;
mod.body = this.tsParseModuleBlock();
this.scope.exit();
this.param.exit();
this.prodParam.exit();
return this.finishNode(mod, "TSModuleDeclaration");
}
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/context.js
Expand Up @@ -60,7 +60,7 @@ tt.name.updateContext = function(prevType) {
if (prevType !== tt.dot) {
if (
(this.state.value === "of" && !this.state.exprAllowed) ||
(this.state.value === "yield" && this.param.hasYield)
(this.state.value === "yield" && this.prodParam.hasYield)
) {
allowed = true;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-parser/src/util/production-parameter.js
@@ -1,7 +1,7 @@
// @flow
export const PARAM_ = 0b000, // Initial Parameter flags
PARAM_YIELD = 0b001, // track [Await] production parameter
PARAM_AWAIT = 0b010; // track [Yield] production parameter
export const PARAM = 0b00, // Initial Parameter flags
PARAM_YIELD = 0b01, // track [Await] production parameter
PARAM_AWAIT = 0b10; // track [Yield] production parameter

// ProductionParameterHandler is a stack fashioned production parameter tracker
// https://tc39.es/ecma262/#sec-grammar-notation
Expand All @@ -25,7 +25,7 @@ export const PARAM_ = 0b000, // Initial Parameter flags
// 6. parse function body
// 7. exit current stack

export type ParamKind = typeof PARAM_ | typeof PARAM_AWAIT | typeof PARAM_YIELD;
export type ParamKind = typeof PARAM | typeof PARAM_AWAIT | typeof PARAM_YIELD;

export default class ProductionParameterHandler {
stacks: Array<ParamKind> = [];
Expand Down
18 changes: 9 additions & 9 deletions packages/babel-parser/src/util/scopeflags.js
Expand Up @@ -2,15 +2,15 @@

// Each scope gets a bitset that may contain these flags
// prettier-ignore
export const SCOPE_OTHER = 0b0000000000,
SCOPE_PROGRAM = 0b0000000001,
SCOPE_FUNCTION = 0b0000000010,
SCOPE_ARROW = 0b0000010000,
SCOPE_SIMPLE_CATCH = 0b0000100000,
SCOPE_SUPER = 0b0001000000,
SCOPE_DIRECT_SUPER = 0b0010000000,
SCOPE_CLASS = 0b0100000000,
SCOPE_TS_MODULE = 0b1000000000,
export const SCOPE_OTHER = 0b00000000,
SCOPE_PROGRAM = 0b00000001,
SCOPE_FUNCTION = 0b00000010,
SCOPE_ARROW = 0b00000100,
SCOPE_SIMPLE_CATCH = 0b00001000,
SCOPE_SUPER = 0b00010000,
SCOPE_DIRECT_SUPER = 0b00100000,
SCOPE_CLASS = 0b01000000,
SCOPE_TS_MODULE = 0b10000000,
SCOPE_VAR = SCOPE_PROGRAM | SCOPE_FUNCTION | SCOPE_TS_MODULE;

export type ScopeFlags =
Expand Down

0 comments on commit e740cb1

Please sign in to comment.