eslint-traverse

raw JSON →
1.0.1 verified Sat Apr 25 auth: no javascript maintenance

ESLint utility (v1.0.1) for creating sub-traversals of AST nodes within ESLint plugin rules. It provides a fast, lightweight traversal with 'SKIP' and 'STOP' controls, and a Path object containing node, parent, parentKey, and parentPath for ancestor tracking. Unlike manual recursive walks, it integrates directly with ESLint's context and offers Babel-style path objects. Minimal maintenance; last release 2018.

error Cannot find module 'eslint-traverse'
cause Missing dependency in package.json.
fix
Run npm install --save-dev eslint-traverse.
error TypeError: traverse is not a function
cause Incorrect import (named import vs default import).
fix
Use import traverse from 'eslint-traverse' instead of import { traverse } from 'eslint-traverse'.
error traverse.SKIP is undefined
cause Using a namespace or named import instead of default import.
fix
Import default: import traverse from 'eslint-traverse' and access its properties.
gotcha traverse.SKIP and traverse.STOP are properties on the default export, not imported directly
fix Use traverse.SKIP and traverse.STOP after importing traverse as default.
gotcha The traversal includes the starting node itself; the visitor is called for the root node.
fix Check path.parent to skip root processing if needed.
deprecated Package has not been updated since 2018; may not support latest ESLint AST changes (e.g., experimental syntax).
fix Consider forking or using eslint-plugin-query if you need newer features.
npm install eslint-traverse
yarn add eslint-traverse
pnpm add eslint-traverse

Shows how to use traverse inside an ESLint rule, skipping child functions and stopping at 'x' identifier.

import traverse from 'eslint-traverse';

export default function(context) {
  return {
    FunctionDeclaration(node) {
      traverse(context, node, (path) => {
        if (path.node.type === 'FunctionDeclaration') {
          return traverse.SKIP;
        }
        if (path.node.type === 'Identifier' && path.node.name === 'x') {
          return traverse.STOP;
        }
        console.log(path.node.type);
      });
    }
  };
}