Registry / testing / eslint-ast-utils

eslint-ast-utils

JSON →
library1.1.0jsnpmunverified

eslint-ast-utils is a utility library designed to simplify common Abstract Syntax Tree (AST) manipulations specifically for ESLint rule development. It provides helper functions to analyze AST nodes, such as identifying CommonJS `require()` calls, extracting their sources, checking for variable references within a node's scope, and safely getting property names from `MemberExpression` nodes. The current stable version is 1.1.0, released in 2017. The project appears to be in maintenance mode, with infrequent updates since its last major release. Its primary differentiation lies in providing pre-built checks for patterns frequently encountered when writing custom ESLint rules, abstracting some of the complexities of direct ESTree traversal.

npm install eslint-ast-utils
INSTALL
IMPORT
SIG · ESLINT-AST-UTILS
E
eslint-ast-utils
testingjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

astUtils
const astUtils = require('eslint-ast-utils');
import astUtils from 'eslint-ast-utils';
This library is primarily CommonJS. While some bundlers or Node.js versions with specific configurations might allow `import astUtils from 'eslint-ast-utils';`, direct usage in Node.js ESM projects typically requires a CommonJS wrapper or dynamic `import()` for reliable interoperability.
isStaticRequire
const { isStaticRequire } = require('eslint-ast-utils');
import { isStaticRequire } from 'eslint-ast-utils';
The library exports an object containing utility functions as its `module.exports`. Therefore, individual functions like `isStaticRequire` can be destructured directly from the `require()` call in CommonJS. However, using a named import syntax like `import { isStaticRequire }` in an ESM context will fail because `isStaticRequire` is not a top-level named export.
getPropertyName
const astUtils = require('eslint-ast-utils'); // ... later ... astUtils.getPropertyName(node);
import { getPropertyName } from 'eslint-ast-utils';
Similar to other utilities, `getPropertyName` is a property of the object exported by the module. It must be accessed via the imported object (`astUtils.getPropertyName`) or by destructuring the object in CommonJS. Direct named ESM imports are not supported.

This quickstart demonstrates parsing a JavaScript code snippet into an AST using 'espree' and then traversing the AST to identify static `require` calls and extract property names from `MemberExpression` nodes, showcasing typical use cases for `eslint-ast-utils` in an ESLint rule context.

const astUtils = require('eslint-ast-utils'); const espree = require('espree'); // A common AST parser for examples const code = ` // Example code to parse const myVar = require('lodash'); function processData(input) { if (astUtils.containsIdentifier('input', input)) { // Self-referential check example const result = input.value; console.log(result); } return myVar.isString(input); } require('./another-module'); `; const ast = espree.parse(code, { ecmaVersion: 2018, loc: true, range: true }); // A basic visitor pattern to find nodes of interest function traverse(node, visitor) { if (!node || typeof node !== 'object') return; visitor(node); for (const key in node) { if (node[key] && typeof node[key] === 'object') { if (Array.isArray(node[key])) { node[key].forEach(item => traverse(item, visitor)); } else { traverse(node[key], visitor); } } } } traverse(ast, (node) => { if (node.type === 'CallExpression' && astUtils.isStaticRequire(node)) { console.log(`Found static require: '${astUtils.getRequireSource(node)}' at line ${node.loc.start.line}`); } if (node.type === 'MemberExpression') { const propertyName = astUtils.getPropertyName(node); if (propertyName !== undefined) { console.log(`Found member expression property: '${propertyName}' at line ${node.loc.start.line}`); } } }); // Note: To run this example, install espree: npm install espree
Debug
Known issues
gotchaThis library is written exclusively in CommonJS (CJS). Direct usage in modern Node.js environments configured for ECMAScript Modules (ESM) (e.g., with `"type": "module"` in `package.json` or a `.mjs` file) may require specific interop patterns or bundler configurations, as standard named ESM imports will not work without a transpilation step.
fix
For ESM projects, use dynamic `import()` or a CommonJS wrapper. Example: `const astUtils = await import('eslint-ast-utils');` or create a `.cjs` file to `require()` and then re-export.
affects: >=1.0.0
gotchaThe package has not seen significant updates since its 1.1.0 release in 2017. While core AST structures for JavaScript remain stable, newer ECMAScript features (e.g., optional chaining, nullish coalescing, private class fields) or changes in ESLint's own AST representations (ESTree) might not be fully supported or handled by these utilities.
fix
Thoroughly test its behavior with ASTs generated from modern JavaScript syntax. Consider alternatives or custom AST traversal logic for full support of the latest ECMAScript features or direct ESLint-provided utilities.
affects: >=1.0.0
gotchaThe `containsIdentifier` function includes nuanced scoping logic to avoid false positives for shadowed variables, but its exact behavior might require careful testing in complex scenarios. Incorrect assumptions about its behavior can lead to faulty ESLint rules that misidentify unused variables or incorrect references.
fix
Refer to the documentation for `containsIdentifier` and thoroughly test scenarios with nested scopes and shadowed variables to ensure it behaves as expected for your specific rule logic. Consider using ESLint's `ScopeManager` for more robust scope analysis if needed.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: require is not defined
Attempting to use `require()` in a Node.js ESM module (e.g., a file with `"type": "module"` in `package.json` or a `.mjs` file). ESM modules do not have a global `require` function by default.
fix
In an ESM context, use a dynamic `import()`: `const astUtilsModule = await import('eslint-ast-utils'); const astUtils = astUtilsModule.default || astUtilsModule;`. Alternatively, if possible, switch the file to CommonJS (`.cjs` extension or remove `"type": "module"` from `package.json`).
TypeError: (0, _eslintAstUtils.isStaticRequire) is not a function
This typically occurs when trying to use named ESM imports like `import { isStaticRequire } from 'eslint-ast-utils';` on a CommonJS module that exports an object as its `module.exports`. CommonJS exports are treated as a single default export in ESM.
fix
Import the entire module as a default object and access its properties: `import astUtils from 'eslint-ast-utils'; astUtils.isStaticRequire(node);`. For TypeScript, you might need `import astUtils = require('eslint-ast-utils');` or `import * as astUtils from 'eslint-ast-utils';` with `esModuleInterop: true`.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
eslint-ast-utils — npm install eslint-ast-utils · libregistry