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

builders.mddocs/

AST Node Builders

Builder functions create properly structured AST nodes with automatic validation. Each builder corresponds to a specific AST node type and ensures the created node has all required properties and passes validation.

Capabilities

Core Expression Builders

Identifier Builder

Creates identifier nodes for variable names, property names, and other identifiers.

/**
 * Creates an identifier node
 * @param name - The identifier name
 * @returns Identifier AST node
 */
function identifier(name: string): t.Identifier;

Literal Builders

Create literal value nodes for primitive values.

/**
 * Creates a string literal node
 * @param value - The string value
 * @returns StringLiteral AST node
 */
function stringLiteral(value: string): t.StringLiteral;

/**
 * Creates a numeric literal node
 * @param value - The numeric value
 * @returns NumericLiteral AST node
 */
function numericLiteral(value: number): t.NumericLiteral;

/**
 * Creates a boolean literal node
 * @param value - The boolean value
 * @returns BooleanLiteral AST node
 */
function booleanLiteral(value: boolean): t.BooleanLiteral;

/**
 * Creates a null literal node
 * @returns NullLiteral AST node
 */
function nullLiteral(): t.NullLiteral;

/**
 * Creates a regular expression literal node
 * @param pattern - The regex pattern
 * @param flags - The regex flags
 * @returns RegExpLiteral AST node
 */
function regExpLiteral(pattern: string, flags?: string): t.RegExpLiteral;

/**
 * Creates a BigInt literal node
 * @param value - The BigInt value as string
 * @returns BigIntLiteral AST node
 */
function bigIntLiteral(value: string): t.BigIntLiteral;

Binary Expression Builder

Creates binary expression nodes for arithmetic, logical, and comparison operations.

/**
 * Creates a binary expression node
 * @param operator - The binary operator (+, -, *, /, %, ==, ===, etc.)
 * @param left - Left operand expression
 * @param right - Right operand expression
 * @returns BinaryExpression AST node
 */
function binaryExpression(
  operator: "+" | "-" | "/" | "%" | "*" | "**" | "&" | "|" | ">>" | ">>>" | 
           "<<" | "^" | "==" | "===" | "!=" | "!==" | "in" | "instanceof" | 
           ">" | "<" | ">=" | "<=" | "|>",
  left: t.Expression | t.PrivateName,
  right: t.Expression
): t.BinaryExpression;

Unary Expression Builder

Creates unary expression nodes for operations with single operands.

/**
 * Creates a unary expression node
 * @param operator - The unary operator (!, -, +, ~, typeof, void, delete)
 * @param argument - The operand expression
 * @param prefix - Whether the operator is prefix (default: true)
 * @returns UnaryExpression AST node
 */
function unaryExpression(
  operator: "void" | "throw" | "delete" | "!" | "+" | "-" | "~" | "typeof",
  argument: t.Expression,
  prefix: boolean = true
): t.UnaryExpression;

Assignment Expression Builder

Creates assignment expression nodes for variable assignments.

/**
 * Creates an assignment expression node
 * @param operator - The assignment operator (=, +=, -=, etc.)
 * @param left - Left-hand side (assignable expression)
 * @param right - Right-hand side expression
 * @returns AssignmentExpression AST node
 */
function assignmentExpression(
  operator: "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "**=" | "<<=" | ">>=" | 
           ">>>=" | "|=" | "^=" | "&=" | "||=" | "&&=" | "??=",
  left: t.LVal | t.OptionalMemberExpression,
  right: t.Expression
): t.AssignmentExpression;

Array and Object Builders

Array Expression Builder

Creates array literal expressions.

/**
 * Creates an array expression node
 * @param elements - Array of expressions and spread elements (null for holes)
 * @returns ArrayExpression AST node
 */
function arrayExpression(
  elements?: Array<null | t.Expression | t.SpreadElement>
): t.ArrayExpression;

Object Expression Builder

Creates object literal expressions.

/**
 * Creates an object expression node
 * @param properties - Array of object properties and methods
 * @returns ObjectExpression AST node
 */
function objectExpression(
  properties: Array<t.ObjectMethod | t.ObjectProperty | t.SpreadElement>
): t.ObjectExpression;

Object Property Builder

Creates object property nodes.

/**
 * Creates an object property node
 * @param key - Property key (identifier, string, or computed expression)
 * @param value - Property value expression
 * @param computed - Whether the key is computed (default: false)
 * @param shorthand - Whether this is shorthand property (default: false)
 * @returns ObjectProperty AST node
 */
function objectProperty(
  key: t.Expression | t.Identifier | t.StringLiteral | t.NumericLiteral,
  value: t.Expression | t.PatternLike,
  computed?: boolean,
  shorthand?: boolean
): t.ObjectProperty;

Function Builders

Function Expression Builder

Creates function expression nodes.

/**
 * Creates a function expression node
 * @param id - Function name (optional for anonymous functions)
 * @param params - Function parameters
 * @param body - Function body
 * @param generator - Whether this is a generator function (default: false)
 * @param async - Whether this is an async function (default: false)
 * @returns FunctionExpression AST node
 */
function functionExpression(
  id: t.Identifier | null | undefined,
  params: Array<t.Identifier | t.Pattern | t.RestElement>,
  body: t.BlockStatement,
  generator?: boolean,
  async?: boolean
): t.FunctionExpression;

Arrow Function Expression Builder

Creates arrow function expression nodes.

/**
 * Creates an arrow function expression node
 * @param params - Function parameters
 * @param body - Function body (expression or block statement)
 * @param async - Whether this is an async function (default: false)
 * @returns ArrowFunctionExpression AST node
 */
function arrowFunctionExpression(
  params: Array<t.Identifier | t.Pattern | t.RestElement>,
  body: t.BlockStatement | t.Expression,
  async?: boolean
): t.ArrowFunctionExpression;

Statement Builders

Block Statement Builder

Creates block statement nodes for grouping statements.

/**
 * Creates a block statement node
 * @param body - Array of statements in the block
 * @param directives - Optional directives (like "use strict")
 * @returns BlockStatement AST node
 */
function blockStatement(
  body: Array<t.Statement>,
  directives?: Array<t.Directive>
): t.BlockStatement;

Expression Statement Builder

Creates expression statement nodes.

/**
 * Creates an expression statement node
 * @param expression - The expression to wrap in a statement
 * @returns ExpressionStatement AST node
 */
function expressionStatement(expression: t.Expression): t.ExpressionStatement;

Return Statement Builder

Creates return statement nodes.

/**
 * Creates a return statement node
 * @param argument - The value to return (optional)
 * @returns ReturnStatement AST node
 */
function returnStatement(argument?: t.Expression | null): t.ReturnStatement;

Variable Declaration Builder

Creates variable declaration nodes.

/**
 * Creates a variable declaration node
 * @param kind - Declaration kind (var, let, const)
 * @param declarations - Array of variable declarators
 * @returns VariableDeclaration AST node
 */
function variableDeclaration(
  kind: "var" | "let" | "const",
  declarations: Array<t.VariableDeclarator>
): t.VariableDeclaration;

/**
 * Creates a variable declarator node
 * @param id - Variable identifier or pattern
 * @param init - Initial value expression (optional)
 * @returns VariableDeclarator AST node
 */
function variableDeclarator(
  id: t.LVal,
  init?: t.Expression | null
): t.VariableDeclarator;

Control Flow Builders

If Statement Builder

Creates if statement nodes.

/**
 * Creates an if statement node
 * @param test - Condition expression
 * @param consequent - Statement to execute if true
 * @param alternate - Statement to execute if false (optional)
 * @returns IfStatement AST node
 */
function ifStatement(
  test: t.Expression,
  consequent: t.Statement,
  alternate?: t.Statement | null
): t.IfStatement;

Loop Statement Builders

Creates various loop statement nodes.

/**
 * Creates a while statement node
 * @param test - Loop condition
 * @param body - Loop body statement
 * @returns WhileStatement AST node
 */
function whileStatement(test: t.Expression, body: t.Statement): t.WhileStatement;

/**
 * Creates a for statement node
 * @param init - Loop initialization (optional)
 * @param test - Loop condition (optional)
 * @param update - Loop update expression (optional)
 * @param body - Loop body statement
 * @returns ForStatement AST node
 */
function forStatement(
  init: t.VariableDeclaration | t.Expression | null | undefined,
  test: t.Expression | null | undefined,
  update: t.Expression | null | undefined,
  body: t.Statement
): t.ForStatement;

/**
 * Creates a for-in statement node
 * @param left - Loop variable
 * @param right - Object to iterate over
 * @param body - Loop body statement
 * @returns ForInStatement AST node
 */
function forInStatement(
  left: t.VariableDeclaration | t.LVal,
  right: t.Expression,
  body: t.Statement
): t.ForInStatement;

/**
 * Creates a for-of statement node
 * @param left - Loop variable
 * @param right - Iterable to iterate over
 * @param body - Loop body statement
 * @param await - Whether this is for-await-of (default: false)
 * @returns ForOfStatement AST node
 */
function forOfStatement(
  left: t.VariableDeclaration | t.LVal,
  right: t.Expression,
  body: t.Statement,
  await?: boolean
): t.ForOfStatement;

Module Builders

Import Declaration Builder

Creates import declaration nodes.

/**
 * Creates an import declaration node
 * @param specifiers - Import specifiers
 * @param source - Module source string
 * @returns ImportDeclaration AST node
 */
function importDeclaration(
  specifiers: Array<t.ImportSpecifier | t.ImportDefaultSpecifier | t.ImportNamespaceSpecifier>,
  source: t.StringLiteral
): t.ImportDeclaration;

/**
 * Creates an import specifier node
 * @param local - Local binding identifier
 * @param imported - Imported name identifier
 * @returns ImportSpecifier AST node
 */
function importSpecifier(
  local: t.Identifier,
  imported: t.Identifier | t.StringLiteral
): t.ImportSpecifier;

/**
 * Creates an import default specifier node
 * @param local - Local binding identifier
 * @returns ImportDefaultSpecifier AST node
 */
function importDefaultSpecifier(local: t.Identifier): t.ImportDefaultSpecifier;

Export Declaration Builders

Creates export declaration nodes.

/**
 * Creates an export named declaration node
 * @param declaration - Declaration to export (optional)
 * @param specifiers - Export specifiers (optional)
 * @param source - Re-export source (optional)
 * @returns ExportNamedDeclaration AST node
 */
function exportNamedDeclaration(
  declaration?: t.Declaration | null,
  specifiers?: Array<t.ExportSpecifier | t.ExportDefaultSpecifier | t.ExportNamespaceSpecifier>,
  source?: t.StringLiteral | null
): t.ExportNamedDeclaration;

/**
 * Creates an export default declaration node
 * @param declaration - Declaration or expression to export as default
 * @returns ExportDefaultDeclaration AST node
 */
function exportDefaultDeclaration(
  declaration: t.FunctionDeclaration | t.TSDeclareFunction | t.ClassDeclaration | t.Expression
): t.ExportDefaultDeclaration;

Class Builders

Class Declaration Builder

Creates class declaration nodes.

/**
 * Creates a class declaration node
 * @param id - Class name identifier
 * @param superClass - Parent class expression (optional)
 * @param body - Class body
 * @param decorators - Class decorators (optional)
 * @returns ClassDeclaration AST node
 */
function classDeclaration(
  id: t.Identifier | null | undefined,
  superClass: t.Expression | null | undefined,
  body: t.ClassBody,
  decorators?: Array<t.Decorator> | null
): t.ClassDeclaration;

/**
 * Creates a class body node
 * @param body - Array of class methods and properties
 * @returns ClassBody AST node
 */
function classBody(
  body: Array<
    t.ClassMethod | 
    t.ClassPrivateMethod | 
    t.ClassProperty | 
    t.ClassPrivateProperty | 
    t.ClassAccessorProperty | 
    t.TSDeclareMethod | 
    t.TSMethodSignature | 
    t.TSPropertySignature | 
    t.TSIndexSignature | 
    t.TSCallSignatureDeclaration | 
    t.TSConstructSignatureDeclaration | 
    t.StaticBlock
  >
): t.ClassBody;

Template Literal Builders

Template Literal Builder

Creates template literal expression nodes.

/**
 * Creates a template literal node
 * @param quasis - Template element parts
 * @param expressions - Embedded expressions
 * @returns TemplateLiteral AST node
 */
function templateLiteral(
  quasis: Array<t.TemplateElement>,
  expressions: Array<t.Expression | t.TSType>
): t.TemplateLiteral;

/**
 * Creates a template element node
 * @param value - Template element value
 * @param tail - Whether this is the last element (default: false)
 * @returns TemplateElement AST node
 */
function templateElement(
  value: { raw: string; cooked?: string | null },
  tail?: boolean
): t.TemplateElement;

Flow Type Builders

Flow Type Creation

Creates Flow type annotation nodes.

/**
 * Creates a Flow union type annotation
 * @param types - Array of type annotations to union
 * @returns UnionTypeAnnotation AST node
 */
function createFlowUnionType(types: Array<t.FlowType>): t.UnionTypeAnnotation;

/**
 * Creates a type annotation based on typeof operation
 * @param type - The type to create annotation for
 * @returns TypeAnnotation AST node
 */
function createTypeAnnotationBasedOnTypeof(type: t.FlowType): t.TypeAnnotation;

TypeScript Type Builders

TypeScript Union Type Builder

Creates TypeScript union type nodes.

/**
 * Creates a TypeScript union type
 * @param types - Array of types to union
 * @returns TSUnionType AST node
 */
function createTSUnionType(types: Array<t.TSTypeElement>): t.TSUnionType;

React JSX Builders

JSX Element Builders

Creates JSX element nodes for React components.

/**
 * Creates a JSX element node
 * @param openingElement - Opening JSX tag
 * @param closingElement - Closing JSX tag (optional for self-closing)
 * @param children - Child elements
 * @param selfClosing - Whether this is self-closing (optional)
 * @returns JSXElement AST node
 */
function jsxElement(
  openingElement: t.JSXOpeningElement,
  closingElement: t.JSXClosingElement | null | undefined,
  children: Array<t.JSXText | t.JSXExpressionContainer | t.JSXSpreadChild | t.JSXElement | t.JSXFragment>,
  selfClosing?: boolean | null
): t.JSXElement;

/**
 * Creates a JSX opening element node
 * @param name - Element name
 * @param attributes - Element attributes
 * @param selfClosing - Whether self-closing (default: false)
 * @returns JSXOpeningElement AST node
 */
function jsxOpeningElement(
  name: t.JSXIdentifier | t.JSXMemberExpression | t.JSXNamespacedName,
  attributes: Array<t.JSXAttribute | t.JSXSpreadAttribute>,
  selfClosing?: boolean
): t.JSXOpeningElement;

/**
 * Creates a JSX identifier node
 * @param name - Identifier name
 * @returns JSXIdentifier AST node
 */
function jsxIdentifier(name: string): t.JSXIdentifier;

/**
 * Creates a JSX attribute node
 * @param name - Attribute name
 * @param value - Attribute value (optional)
 * @returns JSXAttribute AST node
 */
function jsxAttribute(
  name: t.JSXIdentifier | t.JSXNamespacedName,
  value?: t.StringLiteral | t.JSXExpressionContainer | t.JSXElement | t.JSXFragment | null
): t.JSXAttribute;

Usage Examples

import * as t from "@babel/types";

// Create a simple variable declaration: const x = 42;
const declaration = t.variableDeclaration("const", [
  t.variableDeclarator(
    t.identifier("x"),
    t.numericLiteral(42)
  )
]);

// Create a binary expression: a + b
const addition = t.binaryExpression(
  "+",
  t.identifier("a"),
  t.identifier("b")
);

// Create an arrow function: (x) => x * 2
const arrowFn = t.arrowFunctionExpression(
  [t.identifier("x")],
  t.binaryExpression("*", t.identifier("x"), t.numericLiteral(2))
);

// Create an object: { name: "John", age: 30 }
const obj = t.objectExpression([
  t.objectProperty(t.identifier("name"), t.stringLiteral("John")),
  t.objectProperty(t.identifier("age"), t.numericLiteral(30))
]);

// Create a class declaration
const classDecl = t.classDeclaration(
  t.identifier("MyClass"),
  null,
  t.classBody([
    t.classMethod(
      "constructor",
      t.identifier("constructor"),
      [],
      t.blockStatement([])
    )
  ])
);