{"id":15298,"library":"ast-test","title":"AST Node Conditional Tester","description":"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.","status":"abandoned","version":"1.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/dfcreative/ast-test","tags":["javascript","ast","test","esprima","estools"],"install":[{"cmd":"npm install ast-test","lang":"bash","label":"npm"},{"cmd":"yarn add ast-test","lang":"bash","label":"yarn"},{"cmd":"pnpm add ast-test","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used in examples for parsing JavaScript into an ESTree-compliant AST; a de facto peer dependency.","package":"esprima","optional":false}],"imports":[{"note":"This package is CommonJS-only and does not provide an ESM export. Attempting to import it with ES module syntax will result in a runtime error in an ES module environment.","wrong":"import test from 'ast-test';","symbol":"test","correct":"const test = require('ast-test');"}],"quickstart":{"code":"const parse = require('esprima').parse;\nconst test = require('ast-test');\n\n// Define a rule set to identify specific AST patterns.\n// This rule will match 'AssignmentExpression' nodes where 'foo' is assigned\n// and 'Literal' nodes with the value '1'.\nconst rules = {\n  AssignmentExpression: function (node) {\n    if (node.operator !== '=') return false;\n    // Ensure node.left is an Identifier and its name is 'foo'\n    return node.left && node.left.type === 'Identifier' && node.left.name === 'foo';\n  },\n  Expression: function (node) {\n    // A broader rule to catch Literal expressions with specific values.\n    // Note: 'Expression' is a supertype, so rules for specific subtypes like\n    // 'AssignmentExpression' will be checked first if defined.\n    return node.type === 'Literal' && node.value === 1;\n  }\n};\n\n// Test various AST nodes against the defined rules.\nconst ast1 = parse('foo = 1;').body[0]; // Variable assignment 'foo = 1;'\nconst ast2 = parse('var foo = 1;').body[0]; // Variable declaration 'var foo = 1;'\nconst ast3 = parse('bar = 2;').body[0]; // Variable assignment 'bar = 2;'\nconst ast4 = parse('1;').body[0].expression; // Literal expression '1;'\nconst ast5 = parse('2;').body[0].expression; // Literal expression '2;'\n\nconsole.log(\"Testing 'foo = 1;' (AssignmentExpression):\", test(ast1, rules)); // Expected: true\nconsole.log(\"Testing 'var foo = 1;' (VariableDeclaration):\", test(ast2, rules)); // Expected: false (not an AssignmentExpression)\nconsole.log(\"Testing 'bar = 2;' (AssignmentExpression, different LHS):\", test(ast3, rules)); // Expected: false\nconsole.log(\"Testing '1;' (Literal Expression):\", test(ast4, rules)); // Expected: true\nconsole.log(\"Testing '2;' (Literal Expression):\", test(ast5, rules)); // Expected: false\n","lang":"javascript","description":"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."},"warnings":[{"fix":"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.","message":"The `ast-test` package is abandoned, with its last release in 2017. It is highly unlikely to be compatible with modern Node.js versions (e.g., Node.js 16+), newer JavaScript syntax features (e.g., private class fields, top-level await), or recent ESTree specification changes. Using it in a modern project may lead to parsing errors or unexpected behavior.","severity":"breaking","affected_versions":">=1.1.1"},{"fix":"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`.","message":"This library is distributed exclusively as a CommonJS module. It does not provide an ESM export, meaning it cannot be directly `import`ed in an ES module environment without specific loader configurations or bundler adaptations, which is a common footgun for older packages.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"The package does not ship with TypeScript type definitions. Developers using TypeScript will need to create their own declaration files (`.d.ts`) to get type inference and checking, which increases development overhead and reduces type safety.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Evaluate the source code thoroughly before integrating, and be prepared to fork or replace the library if stability or maintenance becomes a critical concern.","message":"The package displays an 'unstable' badge in its README, indicating it was never considered production-ready or feature-complete by its original author. This suggests potential for unannounced breaking changes, API instability, or unaddressed bugs even prior to its abandonment.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"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.","cause":"Attempting to use `require('ast-test')` directly within a JavaScript file that is treated as an ES module (e.g., due to `\"type\": \"module\"` in `package.json` or a `.mjs` extension).","error":"ReferenceError: require is not defined in ES module scope"},{"fix":"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'`.","cause":"A rule function for a specific AST node type (e.g., `AssignmentExpression`) assumes a property exists (e.g., `node.left.name`), but the AST node passed to it, while matching the type, might have a different structure where that property is missing or `undefined` (e.g., `node.left` might be a `MemberExpression` or `ArrayPattern` instead of an `Identifier`).","error":"TypeError: Cannot read properties of undefined (reading 'name') at AssignmentExpression"}],"ecosystem":"npm"}