Helper visitor to only visit nodes in the current 'this' context
npx @tessl/cli install tessl/npm-babel--helper-environment-visitor@7.24.0@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.
npm install @babel/helper-environment-visitorimport visitor, { requeueComputedKeyAndDecorators } from "@babel/helper-environment-visitor";For CommonJS:
const visitor = require("@babel/helper-environment-visitor").default;
const { requeueComputedKeyAndDecorators } = require("@babel/helper-environment-visitor");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);
}
});The helper is built around the concept of lexical environment boundaries in JavaScript:
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:
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 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;// Re-exported types from @babel/traverse and @babel/types
import type { NodePath, Visitor } from "@babel/traverse";
import type * as t from "@babel/types";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);
}
});// 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.