or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-babel--helper-environment-visitor

Helper visitor to only visit nodes in the current 'this' context

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/helper-environment-visitor@7.24.x

To install, run

npx @tessl/cli install tessl/npm-babel--helper-environment-visitor@7.24.0

index.mddocs/

@babel/helper-environment-visitor

@babel/helper-environment-visitor is a specialized visitor utility for Babel's AST traversal system. It provides a visitor object that intelligently navigates JavaScript/TypeScript code while respecting lexical scope boundaries, particularly the 'this' context. The visitor skips function boundaries (except arrow functions which inherit context) and handles computed keys and decorators appropriately.

Package Information

  • Package Name: @babel/helper-environment-visitor
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @babel/helper-environment-visitor

Core Imports

import visitor, { requeueComputedKeyAndDecorators } from "@babel/helper-environment-visitor";

For CommonJS:

const visitor = require("@babel/helper-environment-visitor").default;
const { requeueComputedKeyAndDecorators } = require("@babel/helper-environment-visitor");

Basic Usage

import traverse from "@babel/traverse";
import visitor from "@babel/helper-environment-visitor";
import * as t from "@babel/types";

// Use the environment visitor to traverse only current context
traverse(ast, visitor);

// Use with additional visitor methods
traverse(ast, {
  ...visitor,
  VariableDeclarator(path) {
    // This will only visit variable declarators in the current environment
    console.log(path.node.id.name);
  }
});

Architecture

The helper is built around the concept of lexical environment boundaries in JavaScript:

  • Environment Visitor: Core visitor object that skips function boundaries while preserving arrow function context
  • Function Boundary Handling: Distinguishes between regular functions (which create new contexts) and arrow functions (which inherit context)
  • Computed Key Management: Ensures computed object/class keys are still processed even when their containing functions are skipped
  • Decorator Support: Maintains traversal of decorators on methods and properties

Capabilities

Environment Visitor

The main visitor object that handles traversal within the current lexical environment.

/**
 * Main environment visitor that skips function boundaries while preserving arrow function context
 */
const visitor: Visitor;

interface Visitor {
  FunctionParent(path: NodePath): void;
  Property(path: NodePath): void;
}

The visitor object contains handlers for:

  • FunctionParent: Skips all function-like nodes except arrow functions, requeues computed keys/decorators for methods
  • Property: Allows object properties to continue, skips other property types and requeues computed keys/decorators

Requeue Computed Keys and Decorators

Utility function to requeue computed property keys and decorators for continued traversal.

/**
 * Requeues computed keys and decorators for traversal in method/property nodes
 * @param path - NodePath for Method or Property nodes
 */
function requeueComputedKeyAndDecorators(
  path: NodePath<t.Method | t.Property>
): void;

Usage Example:

import { requeueComputedKeyAndDecorators } from "@babel/helper-environment-visitor";
import * as t from "@babel/types";

// In a custom visitor
const customVisitor = {
  Method(path) {
    if (shouldSkipMethod(path)) {
      path.skip();
      // Ensure computed keys and decorators are still processed
      requeueComputedKeyAndDecorators(path);
    }
  }
};

Legacy Skip Utility (Conditional Export)

Legacy utility function available only in specific build conditions.

/**
 * Legacy function that skips traversal but requeues computed keys
 * @param path - NodePath for Method or ClassProperty nodes
 * Note: Only available in non-Babel 8, non-ESM, non-standalone builds
 */
function skipAllButComputedKey?(
  path: NodePath<t.Method | t.ClassProperty>
): void;

Types

// Re-exported types from @babel/traverse and @babel/types
import type { NodePath, Visitor } from "@babel/traverse";
import type * as t from "@babel/types";

Advanced Usage

Combining with Custom Visitors

import traverse from "@babel/traverse";
import visitor from "@babel/helper-environment-visitor";
import * as t from "@babel/types";

// Combine environment visitor with custom logic
traverse(ast, {
  ...visitor,
  Identifier(path) {
    // Only processes identifiers in current environment
    if (path.node.name === "this") {
      console.log("Found 'this' reference in current context");
    }
  },
  ThisExpression(path) {
    // Only processes 'this' expressions in current environment
    console.log("'this' expression at", path.node.loc);
  }
});

Traversal with NoScope Option

// Environment visitor can be used with noScope for performance
traverse(ast, visitor, undefined, undefined, undefined, { noScope: true });

Important: When using { noScope: true }, path.scope will be undefined. The environment visitor is designed to work correctly in this mode and avoids using path.scope internally.