Registry / type-stubs / ast-types-x

ast-types-x

JSON →
library1.18.0jsnpmunverified

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-x
INSTALL
IMPORT
SIG · AST-TYPES-X
A
ast-types-x
type-stubsjavascriptv1.18.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

namedTypes
import { namedTypes } from 'ast-types-x';
const namedTypes = require('ast-types-x').namedTypes;
Primary way to access AST type definitions for checking and assertion. Uses named export.
builders
import { builders } from 'ast-types-x';
const builders = require('ast-types-x').builders;
Used for programmatically constructing AST nodes (e.g., `b.identifier('foo')`). Uses named export.
visit
import { visit } from 'ast-types-x';
const visit = require('ast-types-x').visit;
The main entry point for AST traversal. Uses named export.
{ getFieldNames, getFieldValue }
import { getFieldNames, getFieldValue } from 'ast-types-x';
const getFieldNames = require('ast-types-x').getFieldNames;
Low-level utilities for inspecting AST node fields, including default values. Uses named exports.

Demonstrates creation of AST nodes using the builders API and type checking using the namedTypes API, asserting the structure of a simple 'if' statement.

import assert from 'assert'; import { namedTypes as n, builders as b } from 'ast-types-x'; // Create an identifier node for 'foo' var fooId = b.identifier('foo'); // Create an if statement: if (foo) { foo(); } var ifFoo = b.ifStatement(fooId, b.blockStatement([ b.expressionStatement(b.callExpression(fooId, [])) ])); // Assertions to check node types and structure assert.ok(n.IfStatement.check(ifFoo)); assert.ok(n.Statement.check(ifFoo)); assert.ok(n.Node.check(ifFoo)); assert.ok(n.BlockStatement.check(ifFoo.consequent)); assert.strictEqual( ifFoo.consequent.body[0].expression.arguments.length, 0 ); assert.strictEqual(ifFoo.test, fooId); assert.ok(n.Expression.check(ifFoo.test)); assert.ok(n.Identifier.check(ifFoo.test)); assert.ok(!n.Statement.check(ifFoo.test)); console.log('AST node creation and type checking successful!');
Debug
Known issues
breakingAs 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.
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.
affects: >=1.0.0
gotchaMixing 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.
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`).
affects: >=1.0.0
gotchaWhen 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.
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'check')
Attempting to use `n.NodeType.check()` on a type that doesn't exist or is incorrectly accessed from `namedTypes`.
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.
TypeError: The requested module 'ast-types-x' does not provide an export named '...' (e.g., 'namedTypes')
Incorrectly importing a symbol that is not a named export from `ast-types-x` or trying to use `require()` for ESM-only exports.
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.
Error: `visit` called with an object that has no `type` property, or `visit` was called with a null or undefined object.
`ast-types-x`'s `visit` function requires a valid AST node object (with a `type` property) as its first argument.
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.
Upgrade
Version history
1.18.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
47 hits · last 30 days
node
42
OpenAI (training)
1
Resources
ast-types-x — npm install ast-types-x · libregistry