The `ast-test` library provides a utility for conditionally evaluating individual nodes within an Abstract Syntax Tree (AST) against a set of user-defined rules. It operates on ASTs conforming to the ESTree specification, typically generated by parsers such as `esprima` or `@babel/parser`. Its core functionality revolves around a `test` function that evaluates a given AST node against a rule object. This object maps AST node types (e.g., `AssignmentExpression`, `Expression`) to predicate functions, which return `true` if the node matches the condition and `false` otherwise. Currently at version 1.1.1, the package had its last release in 2017. Given its age and complete lack of recent updates or maintenance, its release cadence is dormant, indicating it is an abandoned project. It offers a simple, rule-based approach for selective AST node inspection, which can be useful in niche scenarios for static analysis, linting, or identifying specific code patterns without full AST traversal utilities.
npm install ast-testVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to define a set of rules and apply the `ast-test` function to various ESTree AST nodes to identify specific code patterns, such as variable assignments or particular literal values.
Consider using more actively maintained AST traversal and testing libraries like `estree-walker`, `@babel/traverse`, or custom visitor patterns for robust AST analysis in modern JavaScript environments.
When using in an ES module context, you must `require` the package within a CommonJS compatible wrapper or use a bundler that can transpile CommonJS modules for ESM consumption. For Node.js, ensure your file is a CommonJS module or use `createRequire`.
Add a `declare module 'ast-test';` declaration or create a more specific `ast-test.d.ts` file in your project to provide type information for the `test` function.
Evaluate the source code thoroughly before integrating, and be prepared to fork or replace the library if stability or maintenance becomes a critical concern.
For Node.js, rename your file to `.cjs` or change your `package.json` `"type"` field to `"commonjs"`. Alternatively, use `import { createRequire } from 'module'; const require = createRequire(import.meta.url); const test = require('ast-test');` in an ES module.Implement robust checks within your rule functions to ensure properties exist before attempting to access them. For example, `if (node.left && node.left.type === 'Identifier' && node.left.name === 'foo')` instead of just `node.left.name === 'foo'`.