Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use const enum in babel-parser #15793

Merged
merged 7 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 12 additions & 16 deletions packages/babel-parser/src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,7 @@ import {
createPositionWithColumnOffset,
} from "../util/location";
import * as charCodes from "charcodes";
import {
BIND_OUTSIDE,
BIND_VAR,
SCOPE_ARROW,
SCOPE_CLASS,
SCOPE_DIRECT_SUPER,
SCOPE_FUNCTION,
SCOPE_SUPER,
} from "../util/scopeflags";
import { ScopeFlag, BindingFlag } from "../util/scopeflags";
import { ExpressionErrors } from "./util";
import {
PARAM_AWAIT,
Expand Down Expand Up @@ -2430,10 +2422,10 @@ export default abstract class ExpressionParser extends LValParser {
this.initFunction(node, isAsync);
node.generator = isGenerator;
this.scope.enter(
SCOPE_FUNCTION |
SCOPE_SUPER |
(inClassScope ? SCOPE_CLASS : 0) |
(allowDirectSuper ? SCOPE_DIRECT_SUPER : 0),
ScopeFlag.FUNCTION |
ScopeFlag.SUPER |
(inClassScope ? ScopeFlag.CLASS : 0) |
(allowDirectSuper ? ScopeFlag.DIRECT_SUPER : 0),
);
this.prodParam.enter(functionFlags(isAsync, node.generator));
this.parseFunctionParams(node, isConstructor);
Expand Down Expand Up @@ -2485,7 +2477,7 @@ export default abstract class ExpressionParser extends LValParser {
isAsync: boolean,
trailingCommaLoc?: Position | null,
): N.ArrowFunctionExpression {
this.scope.enter(SCOPE_FUNCTION | SCOPE_ARROW);
this.scope.enter(ScopeFlag.FUNCTION | ScopeFlag.ARROW);
let flags = functionFlags(isAsync, false);
// ConciseBody[In] :
// [lookahead ≠ {] ExpressionBody[?In, ~Await]
Expand Down Expand Up @@ -2591,7 +2583,11 @@ export default abstract class ExpressionParser extends LValParser {

// Ensure the function name isn't a forbidden identifier in strict mode, e.g. 'eval'
if (this.state.strict && node.id) {
this.checkIdentifier(node.id, BIND_OUTSIDE, strictModeChanged);
this.checkIdentifier(
node.id,
BindingFlag.TYPE_OUTSIDE,
strictModeChanged,
);
}
},
);
Expand Down Expand Up @@ -2632,7 +2628,7 @@ export default abstract class ExpressionParser extends LValParser {
for (const param of node.params) {
this.checkLVal(param, {
in: formalParameters,
binding: BIND_VAR,
binding: BindingFlag.TYPE_VAR,
checkClashes,
strictModeChanged,
});
Expand Down
22 changes: 10 additions & 12 deletions packages/babel-parser/src/parser/lval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ import {
isStrictBindReservedWord,
} from "../util/identifier";
import { NodeUtils, type Undone } from "./node";
import {
type BindingTypes,
BIND_NONE,
BIND_FLAGS_NO_LET_IN_LEXICAL,
} from "../util/scopeflags";
import { type BindingTypes, BindingFlag } from "../util/scopeflags";
import type { ExpressionErrors } from "./util";
import { Errors, type LValAncestor } from "../parse-error";
import type Parser from "./index";
Expand Down Expand Up @@ -566,7 +562,7 @@ export default abstract class LValParser extends NodeUtils {
* if the check fails.
* @param options.binding
* The desired binding type. If the given expression is an identifier
* and `binding` is not `BIND_NONE`, `checkLVal` will register binding
* and `binding` is not `BindingFlag.TYPE_NONE`, `checkLVal` will register binding
* to the parser scope See also `src/util/scopeflags.js`
* @param options.checkClashes
* An optional string set to check if an identifier name is included.
Expand All @@ -585,7 +581,7 @@ export default abstract class LValParser extends NodeUtils {
expression: Expression | ObjectMember | RestElement,
{
in: ancestor,
binding = BIND_NONE,
binding = BindingFlag.TYPE_NONE,
checkClashes = false,
strictModeChanged = false,
hasParenthesizedAncestor = false,
Expand All @@ -605,7 +601,7 @@ export default abstract class LValParser extends NodeUtils {
if (this.isObjectMethod(expression)) return;

if (type === "MemberExpression") {
if (binding !== BIND_NONE) {
if (binding !== BindingFlag.TYPE_NONE) {
this.raise(Errors.InvalidPropertyBindingPattern, { at: expression });
}
return;
Expand Down Expand Up @@ -641,7 +637,9 @@ export default abstract class LValParser extends NodeUtils {
if (validity === true) return;
if (validity === false) {
const ParseErrorClass =
binding === BIND_NONE ? Errors.InvalidLhs : Errors.InvalidLhsBinding;
binding === BindingFlag.TYPE_NONE
? Errors.InvalidLhs
: Errors.InvalidLhsBinding;

this.raise(ParseErrorClass, { at: expression, ancestor });
return;
Expand Down Expand Up @@ -682,7 +680,7 @@ export default abstract class LValParser extends NodeUtils {
? isStrictBindReservedWord(at.name, this.inModule)
: isStrictBindOnlyReservedWord(at.name))
) {
if (bindingType === BIND_NONE) {
if (bindingType === BindingFlag.TYPE_NONE) {
this.raise(Errors.StrictEvalArguments, { at, referenceName: at.name });
} else {
this.raise(Errors.StrictEvalArgumentsBinding, {
Expand All @@ -692,11 +690,11 @@ export default abstract class LValParser extends NodeUtils {
}
}

if (bindingType & BIND_FLAGS_NO_LET_IN_LEXICAL && at.name === "let") {
if (bindingType & BindingFlag.FLAG_NO_LET_IN_LEXICAL && at.name === "let") {
this.raise(Errors.LetInLexicalBinding, { at });
}

if (!(bindingType & BIND_NONE)) {
if (!(bindingType & BindingFlag.TYPE_NONE)) {
this.declareNameFromIdentifier(at, bindingType);
}
}
Expand Down
71 changes: 32 additions & 39 deletions packages/babel-parser/src/parser/statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,10 @@ import { Errors } from "../parse-error";
import { isIdentifierChar, isIdentifierStart } from "../util/identifier";
import * as charCodes from "charcodes";
import {
BIND_CLASS,
BIND_LEXICAL,
BIND_VAR,
BIND_FUNCTION,
SCOPE_CLASS,
SCOPE_FUNCTION,
SCOPE_OTHER,
SCOPE_SIMPLE_CATCH,
SCOPE_STATIC_BLOCK,
SCOPE_SUPER,
CLASS_ELEMENT_OTHER,
CLASS_ELEMENT_INSTANCE_GETTER,
CLASS_ELEMENT_INSTANCE_SETTER,
CLASS_ELEMENT_STATIC_GETTER,
CLASS_ELEMENT_STATIC_SETTER,
ScopeFlag,
ClassElementType,
type BindingTypes,
BIND_CATCH_PARAM,
BindingFlag,
} from "../util/scopeflags";
import { ExpressionErrors } from "./util";
import { PARAM, functionFlags } from "../util/production-parameter";
Expand Down Expand Up @@ -932,7 +919,7 @@ export default abstract class StatementParser extends ExpressionParser {
if (this.isAwaitAllowed() && this.eatContextual(tt._await)) {
awaitAt = this.state.lastTokStartLoc;
}
this.scope.enter(SCOPE_OTHER);
this.scope.enter(ScopeFlag.OTHER);
this.expect(tt.parenL);

if (this.match(tt.semi)) {
Expand Down Expand Up @@ -1091,7 +1078,7 @@ export default abstract class StatementParser extends ExpressionParser {
const cases: N.SwitchStatement["cases"] = (node.cases = []);
this.expect(tt.braceL);
this.state.labels.push(switchLabel);
this.scope.enter(SCOPE_OTHER);
this.scope.enter(ScopeFlag.OTHER);

// Statements under must be grouped (by label) in SwitchCase
// nodes. `cur` is used to keep the node that we are currently
Expand Down Expand Up @@ -1148,12 +1135,12 @@ export default abstract class StatementParser extends ExpressionParser {

this.scope.enter(
this.options.annexB && param.type === "Identifier"
? SCOPE_SIMPLE_CATCH
? ScopeFlag.SIMPLE_CATCH
: 0,
);
this.checkLVal(param, {
in: { type: "CatchClause" },
binding: BIND_CATCH_PARAM,
binding: BindingFlag.TYPE_CATCH_PARAM,
});

return param;
Expand All @@ -1177,7 +1164,7 @@ export default abstract class StatementParser extends ExpressionParser {
this.expect(tt.parenR);
} else {
clause.param = null;
this.scope.enter(SCOPE_OTHER);
this.scope.enter(ScopeFlag.OTHER);
}

// Parse the catch clause's body.
Expand Down Expand Up @@ -1344,7 +1331,7 @@ export default abstract class StatementParser extends ExpressionParser {
}
this.expect(tt.braceL);
if (createNewLexicalScope) {
this.scope.enter(SCOPE_OTHER);
this.scope.enter(ScopeFlag.OTHER);
}
this.parseBlockBody(
node,
Expand Down Expand Up @@ -1585,7 +1572,7 @@ export default abstract class StatementParser extends ExpressionParser {
const id = this.parseBindingAtom();
this.checkLVal(id, {
in: { type: "VariableDeclarator" },
binding: kind === "var" ? BIND_VAR : BIND_LEXICAL,
binding: kind === "var" ? BindingFlag.TYPE_VAR : BindingFlag.TYPE_LEXICAL,
});
decl.id = id;
}
Expand Down Expand Up @@ -1629,7 +1616,7 @@ export default abstract class StatementParser extends ExpressionParser {

const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
this.state.maybeInArrowParameters = false;
this.scope.enter(SCOPE_FUNCTION);
this.scope.enter(ScopeFlag.FUNCTION);
this.prodParam.enter(functionFlags(isAsync, node.generator));

if (!isDeclaration) {
Expand Down Expand Up @@ -1690,16 +1677,16 @@ export default abstract class StatementParser extends ExpressionParser {
if (!node.id) return;

// If it is a regular function declaration in sloppy mode, then it is
// subject to Annex B semantics (BIND_FUNCTION). Otherwise, the binding
// subject to Annex B semantics (BindingFlag.TYPE_FUNCTION). Otherwise, the binding
// mode depends on properties of the current scope (see
// treatFunctionsAsVar).
this.scope.declareName(
node.id.name,
!this.options.annexB || this.state.strict || node.generator || node.async
? this.scope.treatFunctionsAsVar
? BIND_VAR
: BIND_LEXICAL
: BIND_FUNCTION,
? BindingFlag.TYPE_VAR
: BindingFlag.TYPE_LEXICAL
: BindingFlag.TYPE_FUNCTION,
node.id.loc.start,
);
}
Expand Down Expand Up @@ -2114,7 +2101,9 @@ export default abstract class StatementParser extends ExpressionParser {
>,
) {
// Start a new lexical scope
this.scope.enter(SCOPE_CLASS | SCOPE_STATIC_BLOCK | SCOPE_SUPER);
this.scope.enter(
ScopeFlag.CLASS | ScopeFlag.STATIC_BLOCK | ScopeFlag.SUPER,
);
// Start a new scope with regard to loop labels
const oldLabels = this.state.labels;
this.state.labels = [];
Expand Down Expand Up @@ -2159,7 +2148,7 @@ export default abstract class StatementParser extends ExpressionParser {

this.classScope.declarePrivateName(
this.getPrivateNameSV(node.key),
CLASS_ELEMENT_OTHER,
ClassElementType.OTHER,
node.key.loc.start,
);
}
Expand Down Expand Up @@ -2187,7 +2176,7 @@ export default abstract class StatementParser extends ExpressionParser {
if (isPrivate) {
this.classScope.declarePrivateName(
this.getPrivateNameSV(node.key),
CLASS_ELEMENT_OTHER,
ClassElementType.OTHER,
node.key.loc.start,
);
}
Expand Down Expand Up @@ -2236,13 +2225,13 @@ export default abstract class StatementParser extends ExpressionParser {
const kind =
node.kind === "get"
? node.static
? CLASS_ELEMENT_STATIC_GETTER
: CLASS_ELEMENT_INSTANCE_GETTER
? ClassElementType.STATIC_GETTER
: ClassElementType.INSTANCE_GETTER
: node.kind === "set"
? node.static
? CLASS_ELEMENT_STATIC_SETTER
: CLASS_ELEMENT_INSTANCE_SETTER
: CLASS_ELEMENT_OTHER;
? ClassElementType.STATIC_SETTER
: ClassElementType.INSTANCE_SETTER
: ClassElementType.OTHER;
this.declareClassPrivateMethodInScope(node, kind);
}

Expand Down Expand Up @@ -2298,7 +2287,7 @@ export default abstract class StatementParser extends ExpressionParser {
N.ClassProperty | N.ClassPrivateProperty | N.ClassAccessorProperty
>,
): void {
this.scope.enter(SCOPE_CLASS | SCOPE_SUPER);
this.scope.enter(ScopeFlag.CLASS | ScopeFlag.SUPER);
this.expressionScope.enter(newExpressionScope());
this.prodParam.enter(PARAM);
node.value = this.eat(tt.eq) ? this.parseMaybeAssignAllowIn() : null;
Expand All @@ -2311,7 +2300,7 @@ export default abstract class StatementParser extends ExpressionParser {
node: Undone<N.Class>,
isStatement: boolean,
optionalId?: boolean | null,
bindingType: BindingTypes = BIND_CLASS,
bindingType: BindingTypes = BindingFlag.TYPE_CLASS,
): void {
if (tokenIsIdentifier(this.state.type)) {
node.id = this.parseIdentifier();
Expand Down Expand Up @@ -3133,7 +3122,11 @@ export default abstract class StatementParser extends ExpressionParser {
| N.ImportSpecifier
| N.ImportDefaultSpecifier
| N.ImportNamespaceSpecifier,
>(specifier: Undone<T>, type: T["type"], bindingType = BIND_LEXICAL) {
>(
specifier: Undone<T>,
type: T["type"],
bindingType: BindingTypes = BindingFlag.TYPE_LEXICAL,
) {
this.checkLVal(specifier.local, {
in: { type },
binding: bindingType,
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-parser/src/parser/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { lineBreak, skipWhiteSpaceToLineBreak } from "../util/whitespace";
import { isIdentifierChar } from "../util/identifier";
import ClassScopeHandler from "../util/class-scope";
import ExpressionScopeHandler from "../util/expression-scope";
import { SCOPE_PROGRAM } from "../util/scopeflags";
import { ScopeFlag } from "../util/scopeflags";
import ProductionParameterHandler, {
PARAM_AWAIT,
PARAM,
Expand Down Expand Up @@ -347,7 +347,7 @@ export default abstract class UtilParser extends Tokenizer {
if (this.inModule) {
paramFlags |= PARAM_AWAIT;
}
this.scope.enter(SCOPE_PROGRAM);
this.scope.enter(ScopeFlag.PROGRAM);
this.prodParam.enter(paramFlags);
}

Expand Down