Skip to content

⬅️ Back to Table of Contents

📄 VisitorBase.ts

📊 Analysis Summary

Metric Count
🔧 Functions 4
🧱 Classes 1
📦 Imports 4
📊 Variables & Constants 4
🔄 Re-exports 1
📐 Interfaces 1
📑 Type Aliases 1

📚 Table of Contents

🛠️ File Location:

📂 packages/scope-manager/src/referencer/VisitorBase.ts

📦 Imports

Name Source
AST_NODE_TYPES @typescript-eslint/types
TSESTree @typescript-eslint/types
VisitorKeys @typescript-eslint/visitor-keys
visitorKeys @typescript-eslint/visitor-keys

Variables & Constants

Name Type Kind Value Exported
exclude Set<string> const new Set([...excludeArr, 'parent'] as string[])
children any const this.#childVisitorKeys[node.type] ?? Object.keys(node)
child unknown const node[key as keyof TSESTree.Node] as unknown
visitor (node: TSESTree.Node) => void const (this as NodeVisitor)[node.type]

Re-exports

Type Source Exported Names
named @typescript-eslint/visitor-keys VisitorKeys

Functions

isObject(obj: unknown): obj is Record<string, unknown>

Code
function isObject(obj: unknown): obj is Record<string, unknown> {
  return typeof obj === 'object' && obj != null;
}
  • Parameters:
  • obj: unknown
  • Return Type: obj is Record<string, unknown>

isNode(node: unknown): node is TSESTree.Node

Code
function isNode(node: unknown): node is TSESTree.Node {
  return isObject(node) && typeof node.type === 'string';
}
  • Parameters:
  • node: unknown
  • Return Type: node is TSESTree.Node
  • Calls:
  • isObject

VisitorBase.visitChildren(node: T | null | undefined, excludeArr: (keyof T)[]): void

Code
visitChildren<T extends TSESTree.Node>(
    node: T | null | undefined,
    excludeArr: (keyof T)[] = [],
  ): void {
    if (node?.type == null) {
      return;
    }

    const exclude = new Set([...excludeArr, 'parent'] as string[]);
    const children = this.#childVisitorKeys[node.type] ?? Object.keys(node);
    for (const key of children) {
      if (exclude.has(key)) {
        continue;
      }

      const child = node[key as keyof TSESTree.Node] as unknown;
      if (!child) {
        continue;
      }

      if (Array.isArray(child)) {
        for (const subChild of child) {
          if (isNode(subChild)) {
            this.visit(subChild);
          }
        }
      } else if (isNode(child)) {
        this.visit(child);
      }
    }
  }
  • JSDoc:

    /**
       * Default method for visiting children.
       * @param node the node whose children should be visited
       * @param excludeArr a list of keys to not visit
       */
    

  • Parameters:

  • node: T | null | undefined
  • excludeArr: (keyof T)[]
  • Return Type: void
  • Calls:
  • Object.keys
  • exclude.has
  • Array.isArray
  • isNode
  • this.visit

VisitorBase.visit(node: TSESTree.Node | null | undefined): void

Code
visit(node: TSESTree.Node | null | undefined): void {
    if (node?.type == null) {
      return;
    }

    const visitor = (this as NodeVisitor)[node.type];
    if (visitor) {
      visitor.call(this, node);
      if (!this.#visitChildrenEvenIfSelectorExists) {
        return;
      }
    }

    this.visitChildren(node);
  }
  • JSDoc:

    /**
       * Dispatching node.
       */
    

  • Parameters:

  • node: TSESTree.Node | null | undefined
  • Return Type: void
  • Calls:
  • visitor.call
  • this.visitChildren

Classes

VisitorBase

Class Code
export abstract class VisitorBase {
  readonly #childVisitorKeys: VisitorKeys;
  readonly #visitChildrenEvenIfSelectorExists: boolean;
  constructor(options: VisitorOptions) {
    this.#childVisitorKeys = options.childVisitorKeys ?? visitorKeys;
    this.#visitChildrenEvenIfSelectorExists =
      options.visitChildrenEvenIfSelectorExists ?? false;
  }

  /**
   * Default method for visiting children.
   * @param node the node whose children should be visited
   * @param excludeArr a list of keys to not visit
   */
  visitChildren<T extends TSESTree.Node>(
    node: T | null | undefined,
    excludeArr: (keyof T)[] = [],
  ): void {
    if (node?.type == null) {
      return;
    }

    const exclude = new Set([...excludeArr, 'parent'] as string[]);
    const children = this.#childVisitorKeys[node.type] ?? Object.keys(node);
    for (const key of children) {
      if (exclude.has(key)) {
        continue;
      }

      const child = node[key as keyof TSESTree.Node] as unknown;
      if (!child) {
        continue;
      }

      if (Array.isArray(child)) {
        for (const subChild of child) {
          if (isNode(subChild)) {
            this.visit(subChild);
          }
        }
      } else if (isNode(child)) {
        this.visit(child);
      }
    }
  }

  /**
   * Dispatching node.
   */
  visit(node: TSESTree.Node | null | undefined): void {
    if (node?.type == null) {
      return;
    }

    const visitor = (this as NodeVisitor)[node.type];
    if (visitor) {
      visitor.call(this, node);
      if (!this.#visitChildrenEvenIfSelectorExists) {
        return;
      }
    }

    this.visitChildren(node);
  }
}

Methods

visitChildren(node: T | null | undefined, excludeArr: (keyof T)[]): void
Code
visitChildren<T extends TSESTree.Node>(
    node: T | null | undefined,
    excludeArr: (keyof T)[] = [],
  ): void {
    if (node?.type == null) {
      return;
    }

    const exclude = new Set([...excludeArr, 'parent'] as string[]);
    const children = this.#childVisitorKeys[node.type] ?? Object.keys(node);
    for (const key of children) {
      if (exclude.has(key)) {
        continue;
      }

      const child = node[key as keyof TSESTree.Node] as unknown;
      if (!child) {
        continue;
      }

      if (Array.isArray(child)) {
        for (const subChild of child) {
          if (isNode(subChild)) {
            this.visit(subChild);
          }
        }
      } else if (isNode(child)) {
        this.visit(child);
      }
    }
  }
visit(node: TSESTree.Node | null | undefined): void
Code
visit(node: TSESTree.Node | null | undefined): void {
    if (node?.type == null) {
      return;
    }

    const visitor = (this as NodeVisitor)[node.type];
    if (visitor) {
      visitor.call(this, node);
      if (!this.#visitChildrenEvenIfSelectorExists) {
        return;
      }
    }

    this.visitChildren(node);
  }

Interfaces

VisitorOptions

Interface Code
export interface VisitorOptions {
  childVisitorKeys?: VisitorKeys | null;
  visitChildrenEvenIfSelectorExists?: boolean;
}

Properties

Name Type Optional Description
childVisitorKeys VisitorKeys | null
visitChildrenEvenIfSelectorExists boolean

Type Aliases

NodeVisitor

type NodeVisitor = Partial<
  Record<AST_NODE_TYPES, (node: TSESTree.Node) => void>
>;