AST Node Conditional Tester

1.1.1 · abandoned · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

const parse = require('esprima').parse;
const test = require('ast-test');

// Define a rule set to identify specific AST patterns.
// This rule will match 'AssignmentExpression' nodes where 'foo' is assigned
// and 'Literal' nodes with the value '1'.
const rules = {
  AssignmentExpression: function (node) {
    if (node.operator !== '=') return false;
    // Ensure node.left is an Identifier and its name is 'foo'
    return node.left && node.left.type === 'Identifier' && node.left.name === 'foo';
  },
  Expression: function (node) {
    // A broader rule to catch Literal expressions with specific values.
    // Note: 'Expression' is a supertype, so rules for specific subtypes like
    // 'AssignmentExpression' will be checked first if defined.
    return node.type === 'Literal' && node.value === 1;
  }
};

// Test various AST nodes against the defined rules.
const ast1 = parse('foo = 1;').body[0]; // Variable assignment 'foo = 1;'
const ast2 = parse('var foo = 1;').body[0]; // Variable declaration 'var foo = 1;'
const ast3 = parse('bar = 2;').body[0]; // Variable assignment 'bar = 2;'
const ast4 = parse('1;').body[0].expression; // Literal expression '1;'
const ast5 = parse('2;').body[0].expression; // Literal expression '2;'

console.log("Testing 'foo = 1;' (AssignmentExpression):", test(ast1, rules)); // Expected: true
console.log("Testing 'var foo = 1;' (VariableDeclaration):", test(ast2, rules)); // Expected: false (not an AssignmentExpression)
console.log("Testing 'bar = 2;' (AssignmentExpression, different LHS):", test(ast3, rules)); // Expected: false
console.log("Testing '1;' (Literal Expression):", test(ast4, rules)); // Expected: true
console.log("Testing '2;' (Literal Expression):", test(ast5, rules)); // Expected: false

view raw JSON →