Skip to content

Commit

Permalink
chore: Use enum in @babel/parser (#16066)
Browse files Browse the repository at this point in the history
chore
  • Loading branch information
liuxingbaoyu committed Nov 5, 2023
1 parent 3352f4d commit dab8e95
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
23 changes: 10 additions & 13 deletions packages/babel-parser/src/parser/expression.ts
Expand Up @@ -49,12 +49,7 @@ import {
import * as charCodes from "charcodes";
import { ScopeFlag, BindingFlag } from "../util/scopeflags.ts";
import { ExpressionErrors } from "./util.ts";
import {
PARAM_AWAIT,
PARAM_IN,
PARAM_RETURN,
functionFlags,
} from "../util/production-parameter.ts";
import { ParamKind, functionFlags } from "../util/production-parameter.ts";
import {
newArrowHeadScope,
newAsyncArrowScope,
Expand Down Expand Up @@ -1543,7 +1538,7 @@ export default abstract class ExpressionParser extends LValParser {
if (isAsync) {
// AsyncDoExpression :
// async [no LineTerminator here] do Block[~Yield, +Await, ~Return]
this.prodParam.enter(PARAM_AWAIT);
this.prodParam.enter(ParamKind.PARAM_AWAIT);
node.body = this.parseBlock();
this.prodParam.exit();
} else {
Expand Down Expand Up @@ -2520,7 +2515,7 @@ export default abstract class ExpressionParser extends LValParser {
// [lookahead ≠ {] ExpressionBody[?In, ~Await]
// { FunctionBody[~Yield, ~Await] }
if (!this.match(tt.braceL) && this.prodParam.hasIn) {
flags |= PARAM_IN;
flags |= ParamKind.PARAM_IN;
}
this.prodParam.enter(flags);
this.initFunction(node, isAsync);
Expand Down Expand Up @@ -2585,7 +2580,9 @@ export default abstract class ExpressionParser extends LValParser {

// FunctionBody[Yield, Await]:
// StatementList[?Yield, ?Await, +Return] opt
this.prodParam.enter(this.prodParam.currentFlags() | PARAM_RETURN);
this.prodParam.enter(
this.prodParam.currentFlags() | ParamKind.PARAM_RETURN,
);
node.body = this.parseBlock(
true,
false,
Expand Down Expand Up @@ -3130,9 +3127,9 @@ export default abstract class ExpressionParser extends LValParser {

allowInAnd<T>(callback: () => T): T {
const flags = this.prodParam.currentFlags();
const prodParamToSet = PARAM_IN & ~flags;
const prodParamToSet = ParamKind.PARAM_IN & ~flags;
if (prodParamToSet) {
this.prodParam.enter(flags | PARAM_IN);
this.prodParam.enter(flags | ParamKind.PARAM_IN);
try {
return callback();
} finally {
Expand All @@ -3144,9 +3141,9 @@ export default abstract class ExpressionParser extends LValParser {

disallowInAnd<T>(callback: () => T): T {
const flags = this.prodParam.currentFlags();
const prodParamToClear = PARAM_IN & flags;
const prodParamToClear = ParamKind.PARAM_IN & flags;
if (prodParamToClear) {
this.prodParam.enter(flags & ~PARAM_IN);
this.prodParam.enter(flags & ~ParamKind.PARAM_IN);
try {
return callback();
} finally {
Expand Down
6 changes: 3 additions & 3 deletions packages/babel-parser/src/parser/statement.ts
Expand Up @@ -19,7 +19,7 @@ import {
BindingFlag,
} from "../util/scopeflags.ts";
import { ExpressionErrors } from "./util.ts";
import { PARAM, functionFlags } from "../util/production-parameter.ts";
import { ParamKind, functionFlags } from "../util/production-parameter.ts";
import {
newExpressionScope,
newParameterDeclarationScope,
Expand Down Expand Up @@ -2109,7 +2109,7 @@ export default abstract class StatementParser extends ExpressionParser {
this.state.labels = [];
// ClassStaticBlockStatementList:
// StatementList[~Yield, ~Await, ~Return] opt
this.prodParam.enter(PARAM);
this.prodParam.enter(ParamKind.PARAM);
const body: N.Node[] = (member.body = []);
this.parseBlockOrModuleBlockBody(body, undefined, false, tt.braceR);
this.prodParam.exit();
Expand Down Expand Up @@ -2289,7 +2289,7 @@ export default abstract class StatementParser extends ExpressionParser {
): void {
this.scope.enter(ScopeFlag.CLASS | ScopeFlag.SUPER);
this.expressionScope.enter(newExpressionScope());
this.prodParam.enter(PARAM);
this.prodParam.enter(ParamKind.PARAM);
node.value = this.eat(tt.eq) ? this.parseMaybeAssignAllowIn() : null;
this.expressionScope.exit();
this.prodParam.exit();
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-parser/src/parser/util.ts
Expand Up @@ -17,8 +17,7 @@ import ClassScopeHandler from "../util/class-scope.ts";
import ExpressionScopeHandler from "../util/expression-scope.ts";
import { ScopeFlag } from "../util/scopeflags.ts";
import ProductionParameterHandler, {
PARAM_AWAIT,
PARAM,
ParamKind,
} from "../util/production-parameter.ts";
import {
Errors,
Expand Down Expand Up @@ -347,9 +346,9 @@ export default abstract class UtilParser extends Tokenizer {
}

enterInitialScopes() {
let paramFlags = PARAM;
let paramFlags = ParamKind.PARAM;
if (this.inModule) {
paramFlags |= PARAM_AWAIT;
paramFlags |= ParamKind.PARAM_AWAIT;
}
this.scope.enter(ScopeFlag.PROGRAM);
this.prodParam.enter(paramFlags);
Expand Down
8 changes: 4 additions & 4 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -26,7 +26,7 @@ import TypeScriptScopeHandler from "./scope.ts";
import * as charCodes from "charcodes";
import type { ExpressionErrors } from "../../parser/util.ts";
import type { ParseStatementFlag } from "../../parser/statement.ts";
import { PARAM } from "../../util/production-parameter.ts";
import { ParamKind } from "../../util/production-parameter.ts";
import { Errors, ParseErrorEnum } from "../../parse-error.ts";
import { cloneIdentifier, type Undone } from "../../parser/node.ts";
import type { Pattern } from "../../types.ts";
Expand Down Expand Up @@ -1910,7 +1910,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
node.body = inner;
} else {
this.scope.enter(ScopeFlag.TS_MODULE);
this.prodParam.enter(PARAM);
this.prodParam.enter(ParamKind.PARAM);
node.body = this.tsParseModuleBlock();
this.prodParam.exit();
this.scope.exit();
Expand All @@ -1931,7 +1931,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
}
if (this.match(tt.braceL)) {
this.scope.enter(ScopeFlag.TS_MODULE);
this.prodParam.enter(PARAM);
this.prodParam.enter(ParamKind.PARAM);
node.body = this.tsParseModuleBlock();
this.prodParam.exit();
this.scope.exit();
Expand Down Expand Up @@ -2125,7 +2125,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
// Would like to use tsParseAmbientExternalModuleDeclaration here, but already ran past "global".
if (this.match(tt.braceL)) {
this.scope.enter(ScopeFlag.TS_MODULE);
this.prodParam.enter(PARAM);
this.prodParam.enter(ParamKind.PARAM);
const mod = node;
mod.global = true;
mod.id = expr;
Expand Down
42 changes: 23 additions & 19 deletions packages/babel-parser/src/util/production-parameter.ts
@@ -1,13 +1,3 @@
export const // Initial Parameter flags
PARAM = 0b0000,
// track [Yield] production parameter
PARAM_YIELD = 0b0001,
// track [Await] production parameter
PARAM_AWAIT = 0b0010,
// track [Return] production parameter
PARAM_RETURN = 0b0100,
PARAM_IN = 0b1000; // track [In] production parameter

// ProductionParameterHandler is a stack fashioned production parameter tracker
// https://tc39.es/ecma262/#sec-grammar-notation
// The tracked parameters are defined above.
Expand All @@ -29,7 +19,18 @@ export const // Initial Parameter flags
// 6. parse function body
// 7. exit current stack

export type ParamKind = number;
export const enum ParamKind {
// Initial Parameter flags
PARAM = 0b0000,
// track [Yield] production parameter
PARAM_YIELD = 0b0001,
// track [Await] production parameter
PARAM_AWAIT = 0b0010,
// track [Return] production parameter
PARAM_RETURN = 0b0100,
// track [In] production parameter
PARAM_IN = 0b1000,
}

// todo(flow->ts) - check if more granular type can be used,
// type below is not good because things like PARAM_AWAIT|PARAM_YIELD are not included
Expand All @@ -41,39 +42,42 @@ export type ParamKind = number;
// | typeof PARAM_YIELD;

export default class ProductionParameterHandler {
stacks: Array<number> = [];
enter(flags: number) {
stacks: Array<ParamKind> = [];
enter(flags: ParamKind) {
this.stacks.push(flags);
}

exit() {
this.stacks.pop();
}

currentFlags(): number {
currentFlags(): ParamKind {
return this.stacks[this.stacks.length - 1];
}

get hasAwait(): boolean {
return (this.currentFlags() & PARAM_AWAIT) > 0;
return (this.currentFlags() & ParamKind.PARAM_AWAIT) > 0;
}

get hasYield(): boolean {
return (this.currentFlags() & PARAM_YIELD) > 0;
return (this.currentFlags() & ParamKind.PARAM_YIELD) > 0;
}

get hasReturn(): boolean {
return (this.currentFlags() & PARAM_RETURN) > 0;
return (this.currentFlags() & ParamKind.PARAM_RETURN) > 0;
}

get hasIn(): boolean {
return (this.currentFlags() & PARAM_IN) > 0;
return (this.currentFlags() & ParamKind.PARAM_IN) > 0;
}
}

export function functionFlags(
isAsync: boolean,
isGenerator: boolean,
): ParamKind {
return (isAsync ? PARAM_AWAIT : 0) | (isGenerator ? PARAM_YIELD : 0);
return (
(isAsync ? ParamKind.PARAM_AWAIT : 0) |
(isGenerator ? ParamKind.PARAM_YIELD : 0)
);
}

0 comments on commit dab8e95

Please sign in to comment.