Skip to content

⬅️ Back to Table of Contents

📄 ScopeManager

📊 Analysis Summary

Metric Count
🔧 Functions 29
🧱 Classes 1
📦 Imports 24
📐 Interfaces 1

📚 Table of Contents

🛠️ File Location:

📂 packages/scope-manager/src/ScopeManager.ts

📦 Imports

Name Source
SourceType @typescript-eslint/types
TSESTree @typescript-eslint/types
Scope ./scope
Variable ./variable
assert ./assert
BlockScope ./scope
CatchScope ./scope
ClassScope ./scope
ConditionalTypeScope ./scope
ForScope ./scope
FunctionExpressionNameScope ./scope
FunctionScope ./scope
FunctionTypeScope ./scope
GlobalScope ./scope
MappedTypeScope ./scope
ModuleScope ./scope
ScopeType ./scope
SwitchScope ./scope
TSEnumScope ./scope
TSModuleScope ./scope
TypeScope ./scope
WithScope ./scope
ClassFieldInitializerScope ./scope/ClassFieldInitializerScope
ClassStaticBlockScope ./scope/ClassStaticBlockScope

Functions

ScopeManager.isES6(): boolean

Returns: boolean

Code
public isES6(): boolean {
    return true;
  }

ScopeManager.isGlobalReturn(): boolean

Returns: boolean

Code
public isGlobalReturn(): boolean {
    return this.#options.globalReturn === true;
  }

ScopeManager.isImpliedStrict(): boolean

Returns: boolean

Code
public isImpliedStrict(): boolean {
    return this.#options.impliedStrict === true;
  }

ScopeManager.isModule(): boolean

Returns: boolean

Code
public isModule(): boolean {
    return this.#options.sourceType === 'module';
  }

ScopeManager.isStrictModeSupported(): boolean

Returns: boolean

Code
public isStrictModeSupported(): boolean {
    return true;
  }

ScopeManager.getDeclaredVariables(node: TSESTree.Node): Variable[]

Get the variables that a given AST node defines. The gotten variables' def[].node/def[].parent property is the node. If the node does not define any variable, this returns an empty array.

Parameters:

  • node any: An AST node to get their variables.
Raw JSDoc
/**
   * Get the variables that a given AST node defines. The gotten variables' `def[].node`/`def[].parent` property is the node.
   * If the node does not define any variable, this returns an empty array.
   * @param node An AST node to get their variables.
   */

Calls:

  • this.declaredVariables.get
Code
public getDeclaredVariables(node: TSESTree.Node): Variable[] {
    return this.declaredVariables.get(node) ?? [];
  }

ScopeManager.acquire(node: TSESTree.Node, inner: boolean): Scope | null

Get the scope of a given AST node. The gotten scope's block property is the node. This method never returns function-expression-name scope. If the node does not have their scope, this returns null.

Parameters:

  • node any: An AST node to get their scope.
  • inner any: If the node has multiple scopes, this returns the outermost scope normally. If inner is true then this returns the innermost scope.
Raw JSDoc
/**
   * Get the scope of a given AST node. The gotten scope's `block` property is the node.
   * This method never returns `function-expression-name` scope. If the node does not have their scope, this returns `null`.
   *
   * @param node An AST node to get their scope.
   * @param inner If the node has multiple scopes, this returns the outermost scope normally.
   *                If `inner` is `true` then this returns the innermost scope.
   */

Calls:

  • this.nodeToScope.get
  • predicate
  • scopes.find

Internal Comments:

// Heuristic selection from all scopes.
// If you would like to get all scopes, please use ScopeManager#acquireAll.

Code
public acquire(node: TSESTree.Node, inner = false): Scope | null {
    function predicate(testScope: Scope): boolean {
      if (
        testScope.type === ScopeType.function &&
        testScope.functionExpressionScope
      ) {
        return false;
      }
      return true;
    }

    const scopes = this.nodeToScope.get(node);

    if (!scopes || scopes.length === 0) {
      return null;
    }

    // Heuristic selection from all scopes.
    // If you would like to get all scopes, please use ScopeManager#acquireAll.
    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;
        }
      }
      return null;
    }
    return scopes.find(predicate) ?? null;
  }

ScopeManager.addGlobals(names: string[]): void

Adds dynamically created globals to the global scope and resolve their references. This method is called by ESLint.

Parameters:

  • names any: Names of the globals to create
Raw JSDoc
/**
   * Adds dynamically created globals to the global scope and resolve their references.
   * This method is called by ESLint.
   * @param names Names of the globals to create
   */

Calls:

  • this.globalScope?.addVariables
Code
public addGlobals(names: string[]): void {
    this.globalScope?.addVariables(names);
  }

ScopeManager.nestBlockScope(node: BlockScope['block']): BlockScope

Parameters:

  • node BlockScope['block']

Returns: BlockScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestBlockScope(node: BlockScope['block']): BlockScope {
    assert(this.currentScope);
    return this.nestScope(new BlockScope(this, this.currentScope, node));
  }

ScopeManager.nestCatchScope(node: CatchScope['block']): CatchScope

Parameters:

  • node CatchScope['block']

Returns: CatchScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestCatchScope(node: CatchScope['block']): CatchScope {
    assert(this.currentScope);
    return this.nestScope(new CatchScope(this, this.currentScope, node));
  }

ScopeManager.nestClassFieldInitializerScope(node: ClassFieldInitializerScope['block']): ClassFieldInitializerScope

Parameters:

  • node ClassFieldInitializerScope['block']

Returns: ClassFieldInitializerScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestClassFieldInitializerScope(
    node: ClassFieldInitializerScope['block'],
  ): ClassFieldInitializerScope {
    assert(this.currentScope);
    return this.nestScope(
      new ClassFieldInitializerScope(this, this.currentScope, node),
    );
  }

ScopeManager.nestClassScope(node: ClassScope['block']): ClassScope

Parameters:

  • node ClassScope['block']

Returns: ClassScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestClassScope(node: ClassScope['block']): ClassScope {
    assert(this.currentScope);
    return this.nestScope(new ClassScope(this, this.currentScope, node));
  }

ScopeManager.nestClassStaticBlockScope(node: ClassStaticBlockScope['block']): ClassStaticBlockScope

Parameters:

  • node ClassStaticBlockScope['block']

Returns: ClassStaticBlockScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestClassStaticBlockScope(
    node: ClassStaticBlockScope['block'],
  ): ClassStaticBlockScope {
    assert(this.currentScope);
    return this.nestScope(
      new ClassStaticBlockScope(this, this.currentScope, node),
    );
  }

ScopeManager.nestConditionalTypeScope(node: ConditionalTypeScope['block']): ConditionalTypeScope

Parameters:

  • node ConditionalTypeScope['block']

Returns: ConditionalTypeScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestConditionalTypeScope(
    node: ConditionalTypeScope['block'],
  ): ConditionalTypeScope {
    assert(this.currentScope);
    return this.nestScope(
      new ConditionalTypeScope(this, this.currentScope, node),
    );
  }

ScopeManager.nestForScope(node: ForScope['block']): ForScope

Parameters:

  • node ForScope['block']

Returns: ForScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestForScope(node: ForScope['block']): ForScope {
    assert(this.currentScope);
    return this.nestScope(new ForScope(this, this.currentScope, node));
  }

ScopeManager.nestFunctionExpressionNameScope(node: FunctionExpressionNameScope['block']): FunctionExpressionNameScope

Parameters:

  • node FunctionExpressionNameScope['block']

Returns: FunctionExpressionNameScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestFunctionExpressionNameScope(
    node: FunctionExpressionNameScope['block'],
  ): FunctionExpressionNameScope {
    assert(this.currentScope);
    return this.nestScope(
      new FunctionExpressionNameScope(this, this.currentScope, node),
    );
  }

ScopeManager.nestFunctionScope(node: FunctionScope['block'], isMethodDefinition: boolean): FunctionScope

Parameters:

  • node FunctionScope['block']
  • isMethodDefinition boolean

Returns: FunctionScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestFunctionScope(
    node: FunctionScope['block'],
    isMethodDefinition: boolean,
  ): FunctionScope {
    assert(this.currentScope);
    return this.nestScope(
      new FunctionScope(this, this.currentScope, node, isMethodDefinition),
    );
  }

ScopeManager.nestFunctionTypeScope(node: FunctionTypeScope['block']): FunctionTypeScope

Parameters:

  • node FunctionTypeScope['block']

Returns: FunctionTypeScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestFunctionTypeScope(
    node: FunctionTypeScope['block'],
  ): FunctionTypeScope {
    assert(this.currentScope);
    return this.nestScope(new FunctionTypeScope(this, this.currentScope, node));
  }

ScopeManager.nestGlobalScope(node: GlobalScope['block']): GlobalScope

Parameters:

  • node GlobalScope['block']

Returns: GlobalScope

Calls:

  • this.nestScope
Code
public nestGlobalScope(node: GlobalScope['block']): GlobalScope {
    return this.nestScope(new GlobalScope(this, node));
  }

ScopeManager.nestMappedTypeScope(node: MappedTypeScope['block']): MappedTypeScope

Parameters:

  • node MappedTypeScope['block']

Returns: MappedTypeScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestMappedTypeScope(node: MappedTypeScope['block']): MappedTypeScope {
    assert(this.currentScope);
    return this.nestScope(new MappedTypeScope(this, this.currentScope, node));
  }

ScopeManager.nestModuleScope(node: ModuleScope['block']): ModuleScope

Parameters:

  • node ModuleScope['block']

Returns: ModuleScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestModuleScope(node: ModuleScope['block']): ModuleScope {
    assert(this.currentScope);
    return this.nestScope(new ModuleScope(this, this.currentScope, node));
  }

ScopeManager.nestSwitchScope(node: SwitchScope['block']): SwitchScope

Parameters:

  • node SwitchScope['block']

Returns: SwitchScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestSwitchScope(node: SwitchScope['block']): SwitchScope {
    assert(this.currentScope);
    return this.nestScope(new SwitchScope(this, this.currentScope, node));
  }

ScopeManager.nestTSEnumScope(node: TSEnumScope['block']): TSEnumScope

Parameters:

  • node TSEnumScope['block']

Returns: TSEnumScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestTSEnumScope(node: TSEnumScope['block']): TSEnumScope {
    assert(this.currentScope);
    return this.nestScope(new TSEnumScope(this, this.currentScope, node));
  }

ScopeManager.nestTSModuleScope(node: TSModuleScope['block']): TSModuleScope

Parameters:

  • node TSModuleScope['block']

Returns: TSModuleScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestTSModuleScope(node: TSModuleScope['block']): TSModuleScope {
    assert(this.currentScope);
    return this.nestScope(new TSModuleScope(this, this.currentScope, node));
  }

ScopeManager.nestTypeScope(node: TypeScope['block']): TypeScope

Parameters:

  • node TypeScope['block']

Returns: TypeScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestTypeScope(node: TypeScope['block']): TypeScope {
    assert(this.currentScope);
    return this.nestScope(new TypeScope(this, this.currentScope, node));
  }

ScopeManager.nestWithScope(node: WithScope['block']): WithScope

Parameters:

  • node WithScope['block']

Returns: WithScope

Calls:

  • assert (from ./assert)
  • this.nestScope
Code
public nestWithScope(node: WithScope['block']): WithScope {
    assert(this.currentScope);
    return this.nestScope(new WithScope(this, this.currentScope, node));
  }

ScopeManager.nestScope(scope: T): T

Parameters:

  • scope T

Returns: T

Code
protected nestScope<T extends Scope>(scope: T): T;

Internal helpers

Declared inside another function in this file.

recurse(scope: Scope): void

Parameters:

  • scope Scope

Returns: void

Calls:

  • scope.variables.forEach
  • variables.add
  • scope.childScopes.forEach
Code
function recurse(scope: Scope): void {
      scope.variables.forEach(v => variables.add(v));
      scope.childScopes.forEach(recurse);
    }

predicate(testScope: Scope): boolean

Parameters:

  • testScope Scope

Returns: boolean

Code
function predicate(testScope: Scope): boolean {
      if (
        testScope.type === ScopeType.function &&
        testScope.functionExpressionScope
      ) {
        return false;
      }
      return true;
    }

Classes

ScopeManager

Methods (28) — full entries under Functions

Method Signature
isES6 (): boolean
isGlobalReturn (): boolean
isImpliedStrict (): boolean
isModule (): boolean
isStrictModeSupported (): boolean
getDeclaredVariables (node: TSESTree.Node): Variable[]
acquire (node: TSESTree.Node, inner: boolean): Scope \| null
addGlobals (names: string[]): void
nestBlockScope (node: BlockScope['block']): BlockScope
nestCatchScope (node: CatchScope['block']): CatchScope
nestClassFieldInitializerScope (node: ClassFieldInitializerScope['block']): ClassFieldInitializerScope
nestClassScope (node: ClassScope['block']): ClassScope
nestClassStaticBlockScope (node: ClassStaticBlockScope['block']): ClassStaticBlockScope
nestConditionalTypeScope (node: ConditionalTypeScope['block']): ConditionalTypeScope
nestForScope (node: ForScope['block']): ForScope
nestFunctionExpressionNameScope (node: FunctionExpressionNameScope['block']): FunctionExpressionNameScope
nestFunctionScope (node: FunctionScope['block'], isMethodDefinition: boolean): FunctionScope
nestFunctionTypeScope (node: FunctionTypeScope['block']): FunctionTypeScope
nestGlobalScope (node: GlobalScope['block']): GlobalScope
nestMappedTypeScope (node: MappedTypeScope['block']): MappedTypeScope
nestModuleScope (node: ModuleScope['block']): ModuleScope
nestSwitchScope (node: SwitchScope['block']): SwitchScope
nestTSEnumScope (node: TSEnumScope['block']): TSEnumScope
nestTSModuleScope (node: TSModuleScope['block']): TSModuleScope
nestTypeScope (node: TypeScope['block']): TypeScope
nestWithScope (node: WithScope['block']): WithScope
nestScope (scope: T): T
nestScope (scope: Scope): Scope

Interfaces

ScopeManagerOptions

Interface Code
interface ScopeManagerOptions {
  globalReturn?: boolean;
  impliedStrict?: boolean;
  sourceType?: SourceType;
}

Properties

Name Type Optional Description
globalReturn boolean not shown
impliedStrict boolean not shown
sourceType SourceType not shown

Generated by Syntax Scribe