Skip to content

⬅️ Back to Table of Contents

📄 GlobalScope

📊 Analysis Summary

Metric Count
🔧 Functions 3
🧱 Classes 1
📦 Imports 12

📚 Table of Contents

🛠️ File Location:

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

📦 Imports

Name Source
TSESTree @typescript-eslint/types
AST_NODE_TYPES @typescript-eslint/types
Reference ../referencer/Reference
ScopeManager ../ScopeManager
ImplicitLibVariableOptions ../variable
Variable ../variable
Scope ./Scope
assert ../assert
ImplicitGlobalVariableDefinition ../definition/ImplicitGlobalVariableDefinition
ImplicitLibVariable ../variable
ScopeBase ./ScopeBase
ScopeType ./ScopeType

Functions

GlobalScope.addVariables(names: string[]): void

Parameters:

  • names string[]

Returns: void

Calls:

  • this.defineVariable
  • this.implicit.set.delete
  • nameSet.has
  • this.set.get
  • assert (from ../assert)
  • variable.references.push
  • this.through.filter
  • this.implicit.variables.filter
  • this.implicit.leftToBeResolved.filter
Code
public addVariables(names: string[]): void {
    for (const name of names) {
      this.defineVariable(name, this.set, this.variables, null, null);

      this.implicit.set.delete(name);
    }

    const nameSet = new Set(names);
    for (const reference of this.through) {
      if (nameSet.has(reference.identifier.name)) {
        const variable = this.set.get(reference.identifier.name);
        assert(
          variable,
          `Expected variable with name "${reference.identifier.name}" to be specified.`,
        );

        reference.resolved = variable;
        variable.references.push(reference);
      }
    }

    this.through = this.through.filter(
      reference => !nameSet.has(reference.identifier.name),
    );
    this.implicit.variables = this.implicit.variables.filter(
      variable => !nameSet.has(variable.name),
    );
    this.implicit.leftToBeResolved = this.implicit.leftToBeResolved.filter(
      reference => !nameSet.has(reference.identifier.name),
    );
  }

GlobalScope.close(scopeManager: ScopeManager): Scope | null

Parameters:

  • scopeManager ScopeManager

Returns: Scope | null

Calls:

  • assert (from ../assert)
  • this.set.has
  • this.defineVariable
  • super.close

Internal Comments:

// create an implicit global variable from assignment expression (x2)

Code
public override close(scopeManager: ScopeManager): Scope | null {
    assert(this.leftToResolve);

    for (const ref of this.leftToResolve) {
      if (ref.maybeImplicitGlobal && !this.set.has(ref.identifier.name)) {
        // create an implicit global variable from assignment expression
        const info = ref.maybeImplicitGlobal;
        const node = info.pattern;
        if (node.type === AST_NODE_TYPES.Identifier) {
          this.defineVariable(
            node.name,
            this.implicit.set,
            this.implicit.variables,
            node,
            new ImplicitGlobalVariableDefinition(info.pattern, info.node),
          );
        }
      }
    }

    this.implicit.leftToBeResolved = this.leftToResolve;
    super.close(scopeManager);
    this.implicit.leftToBeResolved = [...this.through];

    return null;
  }

GlobalScope.defineImplicitVariable(name: string, options: ImplicitLibVariableOptions): void

Parameters:

  • name string
  • options ImplicitLibVariableOptions

Returns: void

Calls:

  • this.defineVariable
Code
public defineImplicitVariable(
    name: string,
    options: ImplicitLibVariableOptions,
  ): void {
    this.defineVariable(
      new ImplicitLibVariable(this, name, options),
      this.set,
      this.variables,
      null,
      null,
    );
  }

Classes

GlobalScope

Extends: `ScopeBase< ScopeType.global, TSESTree.Program, /* * The global scope has no parent. / null

`

Class Code
export class GlobalScope extends ScopeBase<
  ScopeType.global,
  TSESTree.Program,
  /**
   * The global scope has no parent.
   */
  null
> {
  // note this is accessed in used in the legacy eslint-scope tests, so it can't be true private
  private readonly implicit: {
    readonly set: Map<string, Variable>;
    variables: Variable[];
    /**
     * List of {@link Reference}s that are left to be resolved (i.e. which
     * need to be linked to the variable they refer to).
     */
    leftToBeResolved: Reference[];
  };

  constructor(scopeManager: ScopeManager, block: GlobalScope['block']) {
    super(scopeManager, ScopeType.global, null, block, false);
    this.implicit = {
      leftToBeResolved: [],
      set: new Map<string, Variable>(),
      variables: [],
    };
  }

  public addVariables(names: string[]): void {
    for (const name of names) {
      this.defineVariable(name, this.set, this.variables, null, null);

      this.implicit.set.delete(name);
    }

    const nameSet = new Set(names);
    for (const reference of this.through) {
      if (nameSet.has(reference.identifier.name)) {
        const variable = this.set.get(reference.identifier.name);
        assert(
          variable,
          `Expected variable with name "${reference.identifier.name}" to be specified.`,
        );

        reference.resolved = variable;
        variable.references.push(reference);
      }
    }

    this.through = this.through.filter(
      reference => !nameSet.has(reference.identifier.name),
    );
    this.implicit.variables = this.implicit.variables.filter(
      variable => !nameSet.has(variable.name),
    );
    this.implicit.leftToBeResolved = this.implicit.leftToBeResolved.filter(
      reference => !nameSet.has(reference.identifier.name),
    );
  }

  public override close(scopeManager: ScopeManager): Scope | null {
    assert(this.leftToResolve);

    for (const ref of this.leftToResolve) {
      if (ref.maybeImplicitGlobal && !this.set.has(ref.identifier.name)) {
        // create an implicit global variable from assignment expression
        const info = ref.maybeImplicitGlobal;
        const node = info.pattern;
        if (node.type === AST_NODE_TYPES.Identifier) {
          this.defineVariable(
            node.name,
            this.implicit.set,
            this.implicit.variables,
            node,
            new ImplicitGlobalVariableDefinition(info.pattern, info.node),
          );
        }
      }
    }

    this.implicit.leftToBeResolved = this.leftToResolve;
    super.close(scopeManager);
    this.implicit.leftToBeResolved = [...this.through];

    return null;
  }

  public defineImplicitVariable(
    name: string,
    options: ImplicitLibVariableOptions,
  ): void {
    this.defineVariable(
      new ImplicitLibVariable(this, name, options),
      this.set,
      this.variables,
      null,
      null,
    );
  }
}

Methods (3) — full entries under Functions

Method Signature
addVariables (names: string[]): void
close (scopeManager: ScopeManager): Scope \| null
defineImplicitVariable (name: string, options: ImplicitLibVariableOptions): void

Generated by Syntax Scribe