Skip to content

Commit

Permalink
Introduce initializeScopes
Browse files Browse the repository at this point in the history
  • Loading branch information
sosukesuzuki committed Feb 12, 2021
1 parent 0feadb0 commit f651952
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
26 changes: 2 additions & 24 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -54,7 +54,6 @@ import {
newAsyncArrowScope,
newExpressionScope,
} from "../util/expression-scope";
import ClassScopeHandler from "../util/class-scope";
import { Errors } from "./error";

/*::
Expand Down Expand Up @@ -2657,33 +2656,12 @@ export default class ExpressionParser extends LValParser {
this.expectPlugin("moduleBlocks");
const node = this.startNode<N.ModuleExpression>();
this.next(); // eat "module"
const oldLabels = this.state.labels;
this.state.labels = [];
const oldExportedIdentifiers = this.state.exportedIdentifiers;
this.state.exportedIdentifiers = [];
this.expect(tt.braceL);
this.scope.enter(SCOPE_PROGRAM);
const oldUndefinedExports = this.scope.undefinedExports;
this.scope.undefinedExports = new Map();
let paramFlags = PARAM;
if (this.hasPlugin("topLevelAwait")) {
paramFlags |= PARAM_AWAIT;
}
this.prodParam.enter(paramFlags);
const oldInModule = this.inModule;
this.inModule = true;
const oldClassScope = this.classScope;
this.classScope = new ClassScopeHandler(this.raise.bind(this));
const revertScopes = this.initializeScopes();
const program = this.startNode<N.Program>();
node.body = this.parseProgram(program, tt.braceR, "module");
this.scope.exit();
this.scope.undefinedExports = oldUndefinedExports;
this.prodParam.exit();
this.inModule = oldInModule;
this.eat(tt.braceR);
this.state.labels = oldLabels;
this.state.exportedIdentifiers = oldExportedIdentifiers;
this.classScope = oldClassScope;
revertScopes();
return this.finishNode<N.ModuleExpression>(node, "ModuleExpression");
}
}
31 changes: 31 additions & 0 deletions packages/babel-parser/src/parser/util.js
Expand Up @@ -6,6 +6,9 @@ import State from "../tokenizer/state";
import type { Node } from "../types";
import { lineBreak } from "../util/whitespace";
import { isIdentifierChar } from "../util/identifier";
import ClassScopeHandler from "../util/class-scope";
import { SCOPE_PROGRAM } from "../util/scopeflags";
import { PARAM_AWAIT, PARAM } from "../util/production-parameter";
import { Errors } from "./error";

type TryParse<Node, Error, Thrown, Aborted, FailState> = {
Expand Down Expand Up @@ -304,6 +307,34 @@ export default class UtilParser extends Tokenizer {
isObjectMethod(node: Node): boolean {
return node.type === "ObjectMethod";
}

initializeScopes() {
const oldLabels = this.state.labels;
this.state.labels = [];
const oldExportedIdentifiers = this.state.exportedIdentifiers;
this.state.exportedIdentifiers = [];
this.scope.enter(SCOPE_PROGRAM);
const oldUndefinedExports = this.scope.undefinedExports;
this.scope.undefinedExports = new Map();
let paramFlags = PARAM;
if (this.hasPlugin("topLevelAwait")) {
paramFlags |= PARAM_AWAIT;
}
this.prodParam.enter(paramFlags);
const oldInModule = this.inModule;
this.inModule = true;
const oldClassScope = this.classScope;
this.classScope = new ClassScopeHandler(this.raise.bind(this));
return () => {
this.scope.exit();
this.scope.undefinedExports = oldUndefinedExports;
this.prodParam.exit();
this.inModule = oldInModule;
this.state.labels = oldLabels;
this.state.exportedIdentifiers = oldExportedIdentifiers;
this.classScope = oldClassScope;
};
}
}

/**
Expand Down

0 comments on commit f651952

Please sign in to comment.