Comprehensive system for working with JavaScript Abstract Syntax Trees. The types module provides type checking, AST building, and utility functions for manipulating JavaScript ASTs.
/**
* Check if node matches the given type
* @param type - AST node type name or alias
* @param node - AST node to check
* @param opts - Optional properties to match
* @param skipAliasCheck - Skip alias checking for performance
* @returns True if node matches type
*/
function is(type: string, node: Node, opts?: Object, skipAliasCheck?: boolean): boolean;Auto-generated type checking functions for all AST node types:
/**
* Check if node is an Identifier
*/
function isIdentifier(node: Node, opts?: Object): boolean;
/**
* Check if node is a Function (FunctionDeclaration or FunctionExpression)
*/
function isFunction(node: Node, opts?: Object): boolean;
/**
* Check if node is a Statement
*/
function isStatement(node: Node, opts?: Object): boolean;
/**
* Check if node is an Expression
*/
function isExpression(node: Node, opts?: Object): boolean;
/**
* Check if node is a Declaration
*/
function isDeclaration(node: Node, opts?: Object): boolean;
/**
* Check if node is a Literal value
*/
function isLiteral(node: Node, opts?: Object): boolean;
/**
* Check if node is a MemberExpression
*/
function isMemberExpression(node: Node, opts?: Object): boolean;
/**
* Check if node is a CallExpression
*/
function isCallExpression(node: Node, opts?: Object): boolean;
/**
* Check if node is a VariableDeclaration
*/
function isVariableDeclaration(node: Node, opts?: Object): boolean;
/**
* Check if node is a BlockStatement
*/
function isBlockStatement(node: Node, opts?: Object): boolean;
/**
* Check if node is an ArrowFunctionExpression
*/
function isArrowFunctionExpression(node: Node, opts?: Object): boolean;
/**
* Check if node is a ClassDeclaration
*/
function isClassDeclaration(node: Node, opts?: Object): boolean;
/**
* Check if node is a Property
*/
function isProperty(node: Node, opts?: Object): boolean;
/**
* Check if node is a Pattern (destructuring pattern)
*/
function isPattern(node: Node, opts?: Object): boolean;
/**
* Check if node is a Loop (for, while, do-while)
*/
function isLoop(node: Node, opts?: Object): boolean;
/**
* Check if node is Scopable (creates a new scope)
*/
function isScopable(node: Node, opts?: Object): boolean;Auto-generated assertion functions that throw if type check fails:
/**
* Assert node is an Identifier, throw if not
*/
function assertIdentifier(node: Node, opts?: Object): void;
/**
* Assert node is a Function, throw if not
*/
function assertFunction(node: Node, opts?: Object): void;
/**
* Assert node is a Statement, throw if not
*/
function assertStatement(node: Node, opts?: Object): void;
// ... similar assert functions for all node typesAuto-generated builder functions for creating AST nodes. These functions are dynamically generated based on the VISITOR_KEYS and BUILDER_KEYS configuration:
/**
* Build an Identifier node
* @param name - Identifier name
*/
function identifier(name: string): Identifier;
/**
* Build a Literal node
* @param value - Literal value
*/
function literal(value: string | number | boolean | null): Literal;
/**
* Build a FunctionExpression node
* @param id - Function name (null for anonymous)
* @param params - Parameter list
* @param body - Function body
*/
function functionExpression(id: Identifier | null, params: Pattern[], body: BlockStatement): FunctionExpression;
/**
* Build a CallExpression node
* @param callee - Function being called
* @param arguments - Argument list
*/
function callExpression(callee: Expression, arguments: Expression[]): CallExpression;
/**
* Build a MemberExpression node
* @param object - Object being accessed
* @param property - Property being accessed
* @param computed - Whether access is computed (obj[prop] vs obj.prop)
*/
function memberExpression(object: Expression, property: Expression, computed: boolean): MemberExpression;
/**
* Build a VariableDeclaration node
* @param kind - Declaration kind ("var", "let", "const")
* @param declarations - Array of variable declarators
*/
function variableDeclaration(kind: "var" | "let" | "const", declarations: VariableDeclarator[]): VariableDeclaration;
/**
* Build a VariableDeclarator node
* @param id - Variable identifier
* @param init - Initial value (null if no initializer)
*/
function variableDeclarator(id: Pattern, init: Expression | null): VariableDeclarator;
/**
* Build a BlockStatement node
* @param body - Array of statements
*/
function blockStatement(body: Statement[]): BlockStatement;
/**
* Build an ExpressionStatement node
* @param expression - Expression to wrap
*/
function expressionStatement(expression: Expression): ExpressionStatement;
/**
* Build a ReturnStatement node
* @param argument - Return value (null for bare return)
*/
function returnStatement(argument: Expression | null): ReturnStatement;
/**
* Build an AssignmentExpression node
* @param operator - Assignment operator
* @param left - Left-hand side
* @param right - Right-hand side
*/
function assignmentExpression(operator: string, left: Pattern, right: Expression): AssignmentExpression;
/**
* Build a BinaryExpression node
* @param operator - Binary operator
* @param left - Left operand
* @param right - Right operand
*/
function binaryExpression(operator: string, left: Expression, right: Expression): BinaryExpression;
/**
* Build a Program node (root of AST)
* @param body - Top-level statements
*/
function program(body: Statement[]): Program;
/**
* Build a SequenceExpression node
* @param expressions - Comma-separated expressions
*/
function sequenceExpression(expressions: Expression[]): SequenceExpression;/**
* Check if node is referenced in its context
* @param node - AST node to check
* @param parent - Parent node
* @returns True if node is a reference
*/
function isReferenced(node: Node, parent: Node): boolean;
/**
* Check if node is a referenced identifier
* @param node - AST node to check
* @param parent - Parent node
* @param opts - Optional matching properties
* @returns True if node is referenced identifier
*/
function isReferencedIdentifier(node: Node, parent: Node, opts?: Object): boolean;
/**
* Check if node creates a scope
* @param node - AST node to check
* @param parent - Parent node
* @returns True if node is scopable
*/
function isScope(node: Node, parent: Node): boolean;/**
* Check if string is valid JavaScript identifier
* @param name - String to validate
* @returns True if valid identifier
*/
function isValidIdentifier(name: string): boolean;
/**
* Convert string to valid identifier
* @param name - String to convert
* @returns Valid identifier string
*/
function toIdentifier(name: string): string;/**
* Convert property key to computed form if needed
* @param node - Property node
* @param key - Key expression
* @returns Computed key expression
*/
function toComputedKey(node: Node, key: Expression): Expression;
/**
* Ensure node has block statement body
* @param node - Node to modify
* @param key - Property key to ensure is block (default: "body")
* @returns Block statement
*/
function ensureBlock(node: Node, key?: string): BlockStatement;
/**
* Convert node to statement if possible
* @param node - Node to convert
* @param ignore - Don't throw if conversion impossible
* @returns Statement node or false
*/
function toStatement(node: Node, ignore?: boolean): Statement | false;
/**
* Convert node to expression if possible
* @param node - Node to convert
* @returns Expression node
*/
function toExpression(node: Node): Expression;
/**
* Convert node to block statement
* @param node - Node to convert
* @param parent - Parent context
* @returns Block statement
*/
function toBlock(node: Node | Node[], parent?: Node): BlockStatement;
/**
* Convert statements to sequence expression
* @param nodes - Array of statement nodes
* @param scope - Scope information
* @returns Sequence expression
*/
function toSequenceExpression(nodes: Node[], scope: Object): Expression;/**
* Shallow comparison of node properties
* @param actual - Actual node
* @param expected - Expected properties
* @returns True if properties match
*/
function shallowEqual(actual: Object, expected: Object): boolean;
/**
* Check if expression is falsy
* @param node - Expression node
* @returns True if expression is falsy
*/
function isFalsyExpression(node: Expression): boolean;/**
* Append property to member expression
* @param member - Member expression to extend
* @param append - Property to append
* @param computed - Whether appended access is computed
* @returns Modified member expression
*/
function appendToMemberExpression(member: MemberExpression, append: Expression | Identifier, computed?: boolean): MemberExpression;
/**
* Prepend property to member expression
* @param member - Member expression to extend
* @param append - Property to prepend
* @returns Modified member expression
*/
function prependToMemberExpression(member: MemberExpression, append: Expression): MemberExpression;
/**
* Build matcher function for member expressions
* @param match - Dot-separated member expression string
* @param allowPartial - Allow partial matches
* @returns Matcher function
*/
function buildMatchMemberExpression(match: string, allowPartial?: boolean): (member: Node) => boolean;/**
* Get all binding identifiers from node
* @param node - Node to analyze
* @returns Object mapping identifier names to nodes
*/
function getBindingIdentifiers(node: Node): { [name: string]: Identifier };
/**
* Check if variable declaration uses let/const
* @param node - Variable declaration node
* @returns True if let/const declaration
*/
function isLet(node: VariableDeclaration): boolean;
/**
* Check if declaration is block-scoped
* @param node - Declaration node
* @returns True if block-scoped
*/
function isBlockScoped(node: Node): boolean;
/**
* Check if variable declaration uses var
* @param node - Variable declaration node
* @returns True if var declaration
*/
function isVar(node: VariableDeclaration): boolean;/**
* Remove comments from node
* @param child - Node to remove comments from
* @returns Node without comments
*/
function removeComments(child: Node): Node;
/**
* Copy comments from parent to child
* @param child - Node to copy comments to
* @param parent - Node to copy comments from
* @returns Child node with inherited comments
*/
function inheritsComments(child: Node, parent: Node): Node;
/**
* Copy all properties from parent to child
* @param child - Node to copy to
* @param parent - Node to copy from
* @returns Child node with inherited properties
*/
function inherits(child: Node, parent: Node): Node;/**
* Get final statements that could be reached
* @param node - Node to analyze
* @returns Array of final statements
*/
function getLastStatements(node: Node): Statement[];/**
* Get name from import/export specifier
* @param specifier - Import/export specifier
* @returns Specifier name (string or Node)
*/
function getSpecifierName(specifier: ImportSpecifier | ExportSpecifier): string | Node;
/**
* Get identifier from import/export specifier
* @param specifier - Import/export specifier
* @returns Specifier identifier (may be "default" identifier)
*/
function getSpecifierId(specifier: ImportSpecifier | ExportSpecifier): Identifier;
/**
* Check if specifier is default import/export
* @param specifier - Import/export specifier
* @returns True if default specifier
*/
function isSpecifierDefault(specifier: ImportSpecifier | ExportSpecifier): boolean;/**
* Keys used for AST traversal by node type
*/
const VISITOR_KEYS: { [nodeType: string]: string[] };
/**
* Type aliases mapping alias names to node types
*/
const ALIAS_KEYS: { [nodeType: string]: string[] };
/**
* Keys used for AST building by node type
*/
const BUILDER_KEYS: { [nodeType: string]: { [key: string]: any } };
/**
* Flipped alias mapping from alias to node types
*/
const FLIPPED_ALIAS_KEYS: { [alias: string]: string[] };
/**
* Keys for statement or block properties
*/
const STATEMENT_OR_BLOCK_KEYS: string[];
/**
* Keys for for-loop initialization
*/
const FOR_INIT_KEYS: string[];
/**
* Keys for comment properties
*/
const COMMENT_KEYS: string[];
/**
* Native JavaScript type names
*/
const NATIVE_TYPE_NAMES: string[];const t = require("6to5").types;
// Check node types
if (t.isIdentifier(node)) {
console.log("Identifier:", node.name);
}
if (t.isFunction(node)) {
console.log("Function with", node.params.length, "parameters");
}
// Check with options
if (t.isIdentifier(node, { name: "React" })) {
console.log("Found React identifier");
}const t = require("6to5").types;
// Build function expression
const fn = t.functionExpression(
t.identifier("add"),
[t.identifier("a"), t.identifier("b")],
t.blockStatement([
t.returnStatement(
t.binaryExpression("+", t.identifier("a"), t.identifier("b"))
)
])
);
// Build variable declaration
const decl = t.variableDeclaration("const", [
t.variableDeclarator(t.identifier("result"), t.literal(42))
]);const t = require("6to5").types;
// Convert to valid identifier
const ident = t.toIdentifier("my-variable"); // "myVariable"
// Check references
if (t.isReferenced(node, parent)) {
console.log("Node is referenced");
}
// Get bindings
const bindings = t.getBindingIdentifiers(node);
console.log("Bound identifiers:", Object.keys(bindings));/**
* Base AST node interface - all AST nodes extend this
*/
interface Node {
type: string;
start?: number;
end?: number;
loc?: SourceLocation;
[key: string]: any;
}
/**
* Transformer interface for modular ES6+ to ES5 transformations
*/
interface Transformer {
transform(ast: Node, file: File): void;
metadata?: {
optional?: boolean;
group?: string;
};
}
/**
* File context object used during transformation
*/
interface File {
ast: Node;
code: string;
opts: TransformOptions;
addCode(code: string): void;
transform(ast: Node): void;
generate(): TransformResult;
}
/**
* Source location information for debugging
*/
interface SourceLocation {
start: Position;
end: Position;
}
/**
* Position within source code
*/
interface Position {
line: number;
column: number;
}