enhance-visitors

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

A utility library (v1.0.0, last updated 2016) for merging ESLint visitor objects, enabling shared logic across multiple ESLint rules. It provides a `mergeVisitors` function that combines visitor objects so that all handlers for a given node type run in first-to-last order, with `:exit` handlers in reverse. Commonly used in ESLint plugin development to avoid duplicating common AST traversal logic (e.g., detecting a package import). Stable, but largely superseded by modern ESLint's native support for multiple visitors or `linter-utils`. The package has low maintenance and no known issues.

error TypeError: enhance.mergeVisitors is not a function
cause Using default import in ESM incorrectly.
fix
Use import { mergeVisitors } from 'enhance-visitors' or const enhance = require('enhance-visitors');
error ReferenceError: exports is not defined in ES module scope
cause Using `module.exports` inside an ESM file.
fix
Switch to CommonJS or use export default if the package supports it (it does not).
error Error: Cannot find module 'enhance-visitors'
cause Package not installed or missing from dependencies.
fix
Run npm install --save enhance-visitors.
gotcha Visitor order for `:exit` is reversed: last-to-first instead of first-to-last.
fix Be aware of the reverse exit order. If you need consistent ordering, consider using separate listeners or a different merging strategy.
deprecated Package is unmaintained since 2017; no updates for ESLint 6+.
fix Consider using ESLint's built-in rule composition or linter-utils for new projects.
gotcha The function is named `mergeVisitors` but often mistakenly called as `mergeVisitors([...])` without the array wrapper.
fix Always pass an array: `mergeVisitors([obj1, obj2])`. If you pass multiple arguments, behavior is undefined.
gotcha No TypeScript definitions; using with TypeScript requires `@ts-ignore` or custom types.
fix Manually declare module or use `any` type.
npm install enhance-visitors
yarn add enhance-visitors
pnpm add enhance-visitors

Demonstrates merging a shared visitor (import detection) with a rule-specific visitor, avoiding duplicate logic.

const enhance = require('enhance-visitors');

// Shared logic: detect import of 'unicorn'
function unicornSeeker(imports) {
  return {
    ImportDeclaration(node) {
      if (node.source.value === 'unicorn') {
        node.specifiers.forEach(spec => {
          if (spec.type === 'ImportDefaultSpecifier') {
            imports.unicorn = spec.local.name;
          }
        });
      }
    }
  };
}

// Rule that uses shared logic
module.exports = function(context) {
  const imports = {};

  return enhance.mergeVisitors([
    unicornSeeker(imports),
    {
      CallExpression(node) {
        if (
          node.callee.type === 'MemberExpression' &&
          node.callee.object.type === 'Identifier' &&
          node.callee.object.name === imports.unicorn
        ) {
          context.report({ node, message: 'Use unicorn with caution' });
        }
      }
    }
  ]);
};