eslint-utils

raw JSON →
3.0.0 verified Sat Apr 25 auth: no javascript

Utility functions and classes for developing ESLint custom rules, stable version 3.0.0, released occasionally with major version bumps. Provides static evaluation on AST nodes, reference tracking for modules/globals, and helper functions like getStaticValue(), ReferenceTracker, and PatternMatcher. Differentiators: comprehensive AST utility for ESLint plugin authors, well-documented, and actively maintained by a seasoned contributor. Current version drops support for older Node.js and adds exports field.

error Cannot find module 'eslint-utils/ast-utils'
cause ESM-only package with exports field restricts subpath imports since v3.0.0.
fix
Import from root: import { getStaticValue } from 'eslint-utils'
error Error [ERR_REQUIRE_ESM]: require() of ES Module /path/eslint-utils/index.mjs not supported.
cause Package is ESM-only since v3.0.0; require() fails.
fix
Use import syntax or dynamic import(). If using CommonJS project, consider an older version.
error Cannot read property 'getStaticValue' of undefined
cause Misunderstood that getStaticValue is exported as default or internal property.
fix
Use named import: import { getStaticValue } from 'eslint-utils'
breaking Direct access to internal files (e.g., 'eslint-utils/ast-utils') is disallowed in v3.0.0 due to the 'exports' field.
fix Use only the package root imports as documented.
breaking Node.js versions <10.0.0, 11.0.0-13.0.0 are no longer supported in v3.0.0.
fix Upgrade Node.js to ^10.0.0 || ^12.0.0 || >=14.0.0.
breaking getFunctionNameWithKind(node) result changed in some cases in v3.0.0.
fix Review rule logic relying on that function's output.
deprecated ReferenceTracker#iterateGlobalReferences() behavior changed in v2.0.0 to recognize globalThis by default.
fix If using globalThis tracking, adjust expectations.
gotcha getStringIfConstant() fails on BigInt and RegExp literals on runtimes without BigInt support (<v1.4.1).
fix Update to v1.4.1 or later.
npm install eslint-utils
yarn add eslint-utils
pnpm add eslint-utils

Shows usage of getStaticValue and ReferenceTracker in an ESLint rule to detect member expressions with static value 'click'.

import { getStaticValue, ReferenceTracker } from 'eslint-utils';

export default {
  create(context) {
    const tracker = new ReferenceTracker(context.getScope());
    return {
      MemberExpression(node) {
        const value = getStaticValue(node, context.getScope());
        if (value && value.value === 'click') {
          context.report({ node, message: 'Avoid using "click" as property.' });
        }
      }
    };
  }
};