ast-types-x is a JavaScript library providing an efficient, modular, and Esprima-compatible implementation of the Mozilla Parser API's abstract syntax tree (AST) type hierarchy. It serves as a foundational tool for working with JavaScript ASTs, enabling robust static analysis, code transformation, and code generation. The current stable version is 1.18.0, released in August 2024, indicating active maintenance and feature development, such as recent additions for `PrivateIdentifier` and `decorators`. Key differentiators include its comprehensive understanding of the AST type system, facilitating advanced node iteration and traversal mechanisms, and its builder API for programmatically constructing AST nodes. It aims to offer complete control over AST manipulation through primitives like `getFieldNames` and `getFieldValue`, alongside a powerful `visit` abstraction for tree traversal, making it suitable for complex code analysis and manipulation tasks. It ships with TypeScript types, enhancing developer experience in TypeScript projects.
npm install ast-types-xVerified import paths — ran on the pinned version, not inferred.
Demonstrates creation of AST nodes using the builders API and type checking using the namedTypes API, asserting the structure of a simple 'if' statement.
Always use `ast-types-x`'s type checking (`n.NodeType.check()`) and field iteration (`eachField`) utilities for robust code that adapts to new AST structures. Avoid hardcoding assumptions about property existence.
Prefer `import ... from 'ast-types-x'` syntax in modern Node.js environments. If using CommonJS, ensure you're correctly accessing named exports (e.g., `require('ast-types-x').namedTypes`).For safe node transformation, consider creating new nodes or using the provided traversal utilities (like `visit`) which often provide mechanisms to return new nodes or manage transformations. If manually copying, use `eachField` to ensure all fields and default values are correctly transferred.
Ensure the AST node type you are checking (`NodeType`) is a valid and exported type from `ast-types-x`'s `namedTypes` object (e.g., `n.IfStatement`, `n.Identifier`). Double-check spelling and case sensitivity.
Verify that the symbol you are trying to import (e.g., `namedTypes`, `builders`, `visit`) is indeed a named export. Ensure your environment is configured for ESM if using `import` statements, or use correct CommonJS access if in a CJS module.
Ensure that the first argument passed to `visit` is a well-formed AST node object. Check for `null`, `undefined`, or plain JavaScript objects that lack the required `type` property.
No dependency data recorded yet.