123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- "use strict";
- const Scope = require("./scope");
- const assert = require("assert");
- const GlobalScope = Scope.GlobalScope;
- const CatchScope = Scope.CatchScope;
- const WithScope = Scope.WithScope;
- const ModuleScope = Scope.ModuleScope;
- const ClassScope = Scope.ClassScope;
- const SwitchScope = Scope.SwitchScope;
- const FunctionScope = Scope.FunctionScope;
- const ForScope = Scope.ForScope;
- const FunctionExpressionNameScope = Scope.FunctionExpressionNameScope;
- const BlockScope = Scope.BlockScope;
- class ScopeManager {
- constructor(options) {
- this.scopes = [];
- this.globalScope = null;
- this.__nodeToScope = new WeakMap();
- this.__currentScope = null;
- this.__options = options;
- this.__declaredVariables = new WeakMap();
- }
- __useDirective() {
- return this.__options.directive;
- }
- __isOptimistic() {
- return this.__options.optimistic;
- }
- __ignoreEval() {
- return this.__options.ignoreEval;
- }
- __isNodejsScope() {
- return this.__options.nodejsScope;
- }
- isModule() {
- return this.__options.sourceType === "module";
- }
- isImpliedStrict() {
- return this.__options.impliedStrict;
- }
- isStrictModeSupported() {
- return this.__options.ecmaVersion >= 5;
- }
-
- __get(node) {
- return this.__nodeToScope.get(node);
- }
-
- getDeclaredVariables(node) {
- return this.__declaredVariables.get(node) || [];
- }
-
- acquire(node, inner) {
-
- function predicate(testScope) {
- if (testScope.type === "function" && testScope.functionExpressionScope) {
- return false;
- }
- return true;
- }
- const scopes = this.__get(node);
- if (!scopes || scopes.length === 0) {
- return null;
- }
-
-
- if (scopes.length === 1) {
- return scopes[0];
- }
- if (inner) {
- for (let i = scopes.length - 1; i >= 0; --i) {
- const scope = scopes[i];
- if (predicate(scope)) {
- return scope;
- }
- }
- } else {
- for (let i = 0, iz = scopes.length; i < iz; ++i) {
- const scope = scopes[i];
- if (predicate(scope)) {
- return scope;
- }
- }
- }
- return null;
- }
-
- acquireAll(node) {
- return this.__get(node);
- }
-
- release(node, inner) {
- const scopes = this.__get(node);
- if (scopes && scopes.length) {
- const scope = scopes[0].upper;
- if (!scope) {
- return null;
- }
- return this.acquire(scope.block, inner);
- }
- return null;
- }
- attach() { }
- detach() { }
- __nestScope(scope) {
- if (scope instanceof GlobalScope) {
- assert(this.__currentScope === null);
- this.globalScope = scope;
- }
- this.__currentScope = scope;
- return scope;
- }
- __nestGlobalScope(node) {
- return this.__nestScope(new GlobalScope(this, node));
- }
- __nestBlockScope(node) {
- return this.__nestScope(new BlockScope(this, this.__currentScope, node));
- }
- __nestFunctionScope(node, isMethodDefinition) {
- return this.__nestScope(new FunctionScope(this, this.__currentScope, node, isMethodDefinition));
- }
- __nestForScope(node) {
- return this.__nestScope(new ForScope(this, this.__currentScope, node));
- }
- __nestCatchScope(node) {
- return this.__nestScope(new CatchScope(this, this.__currentScope, node));
- }
- __nestWithScope(node) {
- return this.__nestScope(new WithScope(this, this.__currentScope, node));
- }
- __nestClassScope(node) {
- return this.__nestScope(new ClassScope(this, this.__currentScope, node));
- }
- __nestSwitchScope(node) {
- return this.__nestScope(new SwitchScope(this, this.__currentScope, node));
- }
- __nestModuleScope(node) {
- return this.__nestScope(new ModuleScope(this, this.__currentScope, node));
- }
- __nestFunctionExpressionNameScope(node) {
- return this.__nestScope(new FunctionExpressionNameScope(this, this.__currentScope, node));
- }
- __isES6() {
- return this.__options.ecmaVersion >= 6;
- }
- }
- module.exports = ScopeManager;
|