Babel Types is a Lodash-esque utility library for AST nodes
npx @tessl/cli install tessl/npm-babel--types@7.27.0Babel Types is a Lodash-esque utility library for Abstract Syntax Tree (AST) nodes. It provides comprehensive type definitions, validators, builders, and converters for all Babel AST node types, enabling developers to programmatically create, validate, and manipulate JavaScript code representations.
npm install @babel/typesimport * as t from "@babel/types";For individual imports:
import {
identifier,
stringLiteral,
binaryExpression,
isIdentifier,
assertIdentifier
} from "@babel/types";For CommonJS:
const t = require("@babel/types");
const { identifier, stringLiteral, isIdentifier } = require("@babel/types");import * as t from "@babel/types";
// Create AST nodes
const id = t.identifier("myVariable");
const str = t.stringLiteral("hello world");
const expr = t.binaryExpression("+", id, str);
// Validate nodes
if (t.isIdentifier(id)) {
console.log(id.name); // "myVariable"
}
// Assert node types (throws if invalid)
t.assertBinaryExpression(expr);
// Clone nodes
const clonedExpr = t.cloneNode(expr);Babel Types is organized around several key functional areas:
Factory functions for creating all types of AST nodes with automatic validation and proper structure.
function identifier(name: string): t.Identifier;
function stringLiteral(value: string): t.StringLiteral;
function binaryExpression(
operator: string,
left: t.Expression,
right: t.Expression
): t.BinaryExpression;Type guards and assertion functions for runtime validation of AST node types.
function isIdentifier(node: t.Node | null | undefined): node is t.Identifier;
function isBinaryExpression(node: t.Node | null | undefined): node is t.BinaryExpression;
function assertIdentifier(node: t.Node | null | undefined): asserts node is t.Identifier;Utilities for walking AST structures and applying transformations.
function traverse<T>(
node: t.Node,
visitor: TraverseOptions<T>,
scope?: Scope,
state?: T,
parentPath?: NodePath
): void;
function traverseFast(node: t.Node, enter?: (node: t.Node) => void): void;Deep and shallow cloning utilities for duplicating AST nodes.
function cloneNode<T extends t.Node>(node: T, deep?: boolean): T;
function clone<T extends t.Node>(node: T): T;
function cloneDeep<T extends t.Node>(node: T): T;Functions to convert between different AST node types and representations.
function toExpression(node: t.Statement | t.Expression): t.Expression;
function toStatement(node: t.Statement | t.Expression): t.Statement;
function toIdentifier(name: string | t.Identifier): t.Identifier;Utilities for modifying and transforming existing AST nodes.
function removeProperties<T extends t.Node>(node: T, options?: RemovePropertiesOptions): void;
function inherits<T extends t.Node>(child: T, parent: t.Node): T;
function appendToMemberExpression(
member: t.MemberExpression,
append: t.Identifier,
computed?: boolean
): t.MemberExpression;Utilities for managing comments on AST nodes.
function addComment<T extends t.Node>(
node: T,
type: "leading" | "inner" | "trailing",
content: string,
line?: boolean
): T;
function inheritLeadingComments<T extends t.Node>(child: T, parent: t.Node): void;
function removeComments<T extends t.Node>(node: T): T;Utilities for extracting binding identifiers and function names from AST nodes.
function getBindingIdentifiers(
node: t.Node,
duplicates?: boolean,
outerOnly?: boolean
): Record<string, t.Identifier | t.Identifier[]>;
function getAssignmentIdentifiers(node: t.Node): Record<string, t.Identifier[]>;
function getOuterBindingIdentifiers(
node: t.Node,
duplicates?: boolean
): Record<string, t.Identifier | t.Identifier[]>;
function getFunctionName(node: t.Function): t.Identifier | null;Identifier and Binding Retrieval
Specialized utilities for working with React components and JSX.
const react: {
isReactComponent(node: t.Node | null | undefined): boolean;
isCompatTag(tagName: string): boolean;
buildChildren(node: t.JSXElement): t.Node[];
};Collections of operators, keywords, and utility functions for AST manipulation.
const BINARY_OPERATORS: string[];
const UNARY_OPERATORS: string[];
const LOGICAL_OPERATORS: string[];
function shallowEqual(actual: object, expected: object): boolean;// Core AST node types
interface Node {
type: string;
start?: number | null;
end?: number | null;
loc?: SourceLocation | null;
range?: [number, number];
leadingComments?: Comment[] | null;
innerComments?: Comment[] | null;
trailingComments?: Comment[] | null;
}
interface Identifier extends Node {
type: "Identifier";
name: string;
optional?: boolean;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
decorators?: Decorator[] | null;
}
interface StringLiteral extends Node {
type: "StringLiteral";
value: string;
}
interface BinaryExpression extends Node {
type: "BinaryExpression";
operator: string;
left: Expression;
right: Expression;
}
// Union types for different node categories
type Expression = Identifier | StringLiteral | BinaryExpression | /* ...many more */;
type Statement = BlockStatement | ExpressionStatement | ReturnStatement | /* ...many more */;
type Declaration = FunctionDeclaration | VariableDeclaration | ClassDeclaration | /* ...many more */;
type LVal = Identifier | MemberExpression | ArrayPattern | ObjectPattern | /* ...more */;