or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

builders.mdcloning.mdcomments.mdconstants.mdconverters.mdindex.mdmodifications.mdreact.mdretrievers.mdtraversal.mdvalidators.md
tile.json

tessl/npm-babel--types

Babel Types is a Lodash-esque utility library for AST nodes

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@babel/types@7.27.x

To install, run

npx @tessl/cli install tessl/npm-babel--types@7.27.0

index.mddocs/

Babel Types

Babel 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.

Package Information

  • Package Name: @babel/types
  • Package Type: npm
  • Language: TypeScript/JavaScript
  • Installation: npm install @babel/types

Core Imports

import * 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");

Basic Usage

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);

Architecture

Babel Types is organized around several key functional areas:

  • Builders: Factory functions to create AST nodes with proper structure and validation
  • Validators: Type guards and assertion functions to check node types at runtime
  • Traversal: Utilities for walking and manipulating AST structures
  • Cloning: Deep and shallow cloning utilities for AST nodes
  • Converters: Functions to transform between different AST representations
  • Constants: Collections of operators, keywords, and other AST-related constants

Capabilities

AST Node Builders

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;

AST Node Builders

Type Validators and Assertions

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;

Type Validators

AST Traversal

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;

AST Traversal

Node Cloning

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;

Node Cloning

Type Converters

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;

Type Converters

AST Modifications

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;

AST Modifications

Comments

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;

Comments

Identifier and Binding Retrieval

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

React Utilities

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[];
};

React Utilities

Constants and Utilities

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;

Constants and Utilities

Types

// 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 */;