{"id":12838,"library":"ast-types-x","title":"AST Types X","description":"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.","status":"active","version":"1.18.0","language":"javascript","source_language":"en","source_url":"git://github.com/pionxzh/ast-types-x","tags":["javascript","ast","abstract syntax tree","hierarchy","mozilla","spidermonkey","parser api","esprima","types","typescript"],"install":[{"cmd":"npm install ast-types-x","lang":"bash","label":"npm"},{"cmd":"yarn add ast-types-x","lang":"bash","label":"yarn"},{"cmd":"pnpm add ast-types-x","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Primary way to access AST type definitions for checking and assertion. Uses named export.","wrong":"const namedTypes = require('ast-types-x').namedTypes;","symbol":"namedTypes","correct":"import { namedTypes } from 'ast-types-x';"},{"note":"Used for programmatically constructing AST nodes (e.g., `b.identifier('foo')`). Uses named export.","wrong":"const builders = require('ast-types-x').builders;","symbol":"builders","correct":"import { builders } from 'ast-types-x';"},{"note":"The main entry point for AST traversal. Uses named export.","wrong":"const visit = require('ast-types-x').visit;","symbol":"visit","correct":"import { visit } from 'ast-types-x';"},{"note":"Low-level utilities for inspecting AST node fields, including default values. Uses named exports.","wrong":"const getFieldNames = require('ast-types-x').getFieldNames;","symbol":"{ getFieldNames, getFieldValue }","correct":"import { getFieldNames, getFieldValue } from 'ast-types-x';"}],"quickstart":{"code":"import assert from 'assert';\nimport { namedTypes as n, builders as b } from 'ast-types-x';\n\n// Create an identifier node for 'foo'\nvar fooId = b.identifier('foo');\n\n// Create an if statement: if (foo) { foo(); }\nvar ifFoo = b.ifStatement(fooId, b.blockStatement([\n  b.expressionStatement(b.callExpression(fooId, []))\n]));\n\n// Assertions to check node types and structure\nassert.ok(n.IfStatement.check(ifFoo));\nassert.ok(n.Statement.check(ifFoo));\nassert.ok(n.Node.check(ifFoo));\n\nassert.ok(n.BlockStatement.check(ifFoo.consequent));\nassert.strictEqual(\n  ifFoo.consequent.body[0].expression.arguments.length,\n  0\n);\n\nassert.strictEqual(ifFoo.test, fooId);\nassert.ok(n.Expression.check(ifFoo.test));\nassert.ok(n.Identifier.check(ifFoo.test));\nassert.ok(!n.Statement.check(ifFoo.test));\n\nconsole.log('AST node creation and type checking successful!');","lang":"javascript","description":"Demonstrates creation of AST nodes using the builders API and type checking using the namedTypes API, asserting the structure of a simple 'if' statement."},"warnings":[{"fix":"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.","message":"As new ECMAScript features are introduced, the structure of AST nodes (e.g., `ClassDeclaration`, `ClassProperty`) might change with new properties being added (like `decorators` or `PrivateIdentifier`). Code relying on exact AST shape or missing properties might break.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"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`).","message":"Mixing CommonJS `require()` with ESM `import` statements can lead to issues, especially with default exports or interop. `ast-types-x` is primarily designed for ESM usage as shown in its documentation.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"When manipulating AST nodes, direct mutation of properties might lead to unexpected behavior or issues with subsequent traversals if not managed carefully. The library provides helpers, but explicit copying might be needed for immutable-like operations.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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.","cause":"Attempting to use `n.NodeType.check()` on a type that doesn't exist or is incorrectly accessed from `namedTypes`.","error":"TypeError: Cannot read properties of undefined (reading 'check')"},{"fix":"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.","cause":"Incorrectly importing a symbol that is not a named export from `ast-types-x` or trying to use `require()` for ESM-only exports.","error":"TypeError: The requested module 'ast-types-x' does not provide an export named '...' (e.g., 'namedTypes')"},{"fix":"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.","cause":"`ast-types-x`'s `visit` function requires a valid AST node object (with a `type` property) as its first argument.","error":"Error: `visit` called with an object that has no `type` property, or `visit` was called with a null or undefined object."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null}