CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ts-morph

TypeScript compiler wrapper for static analysis and code manipulation.

Pending
Overview
Eval results
Files

ast-nodes.mddocs/

AST Nodes

Complete set of AST node classes providing navigation, analysis, and manipulation capabilities for all TypeScript language constructs. Each node represents a specific element in the TypeScript Abstract Syntax Tree.

Capabilities

Base Node Class

The foundational class from which all AST nodes inherit, providing core navigation and manipulation functionality.

/**
 * Base class for all AST nodes
 */
abstract class Node<T = ts.Node> {
  /** Get the syntax kind of this node */
  getKind(): SyntaxKind;
  
  /** Get the name of the syntax kind */
  getKindName(): string;
  
  /** Get the text of this node */
  getText(): string;
  
  /** Get the full text including leading trivia */
  getFullText(): string;
  
  /** Get the start position */
  getStart(includeJsDocComments?: boolean): number;
  
  /** Get the end position */
  getEnd(): number;
  
  /** Get the full start position including trivia */
  getFullStart(): number;
  
  /** Get the width of this node */
  getWidth(includeJsDocComments?: boolean): number;
  
  /** Get the full width including trivia */
  getFullWidth(): number;
  
  /** Get the parent node */
  getParent(): Node | undefined;
  
  /** Get the parent node, throwing if not found */
  getParentOrThrow(): Node;
  
  /** Get all child nodes */
  getChildren(): Node[];
  
  /** Get the first child node */
  getFirstChild(): Node | undefined;
  
  /** Get the first child node with optional condition */
  getFirstChild<T extends Node>(condition: (node: Node) => node is T): T | undefined;
  
  /** Get the first child node, throwing if not found */
  getFirstChildOrThrow(): Node;
  
  /** Get the last child node */
  getLastChild(): Node | undefined;
  
  /** Get the last child node with optional condition */
  getLastChild<T extends Node>(condition: (node: Node) => node is T): T | undefined;
  
  /** Get the last child node, throwing if not found */
  getLastChildOrThrow(): Node;
  
  /** Get the previous sibling */
  getPreviousSibling(): Node | undefined;
  
  /** Get the previous sibling with optional condition */
  getPreviousSibling<T extends Node>(condition: (node: Node) => node is T): T | undefined;
  
  /** Get the next sibling */
  getNextSibling(): Node | undefined;
  
  /** Get the next sibling with optional condition */
  getNextSibling<T extends Node>(condition: (node: Node) => node is T): T | undefined;
  
  /** Get all previous siblings */
  getPreviousSiblings(): Node[];
  
  /** Get all next siblings */
  getNextSiblings(): Node[];
  
  /** Get the source file this node belongs to */
  getSourceFile(): SourceFile;
  
  /** Get the project this node belongs to */
  getProject(): Project;
  
  /** Get ancestors of this node */
  getAncestors(): Node[];
  
  /** Get descendants of this node */
  getDescendants(): Node[];
  
  /** Get the first descendant matching condition */
  getFirstDescendant<T extends Node>(condition?: (node: Node) => node is T): T | undefined;
  
  /** Get the first descendant matching condition, throwing if not found */
  getFirstDescendantOrThrow<T extends Node>(condition?: (node: Node) => node is T): T;
  
  /** Check if this node is of a specific kind */
  isKind<TKind extends SyntaxKind>(kind: TKind): this is KindToNodeMappings[TKind];
  
  /** Cast this node to a specific kind */
  asKind<TKind extends SyntaxKind>(kind: TKind): KindToNodeMappings[TKind] | undefined;
  
  /** Cast this node to a specific kind, throwing if not that kind */
  asKindOrThrow<TKind extends SyntaxKind>(kind: TKind): KindToNodeMappings[TKind];
  
  /** Get the symbol associated with this node */
  getSymbol(): Symbol | undefined;
  
  /** Get the symbol associated with this node, throwing if not found */
  getSymbolOrThrow(): Symbol;
  
  /** Get the type associated with this node */
  getType(): Type;
  
  /** Forget this node and release it from cache */
  forget(): void;
  
  /** Forget all descendant nodes */
  forgetDescendants(): void;
  
  /** Check if this node was forgotten */
  wasForgotten(): boolean;
  
  /** Print this node to a string */
  print(options?: PrintNodeOptions): string;
  
  /** Remove this node */
  remove(): void;
  
  /** Replace this node with text */
  replaceWithText(text: string): Node;
  
  /** Iterate over each child */
  forEachChild<T>(cbNode: (node: Node) => T | undefined): T | undefined;
  
  /** Iterate over each descendant */
  forEachDescendant<T>(cbNode: (node: Node) => T | undefined): T | undefined;
}

Source File

The root node representing a TypeScript/JavaScript source file.

class SourceFile extends Node {
  /** Get the file path */
  getFilePath(): string;
  
  /** Get the base name of the file */
  getBaseName(): string;
  
  /** Get the directory name */
  getDirName(): string;
  
  /** Get the extension */
  getExtension(): string;
  
  /** Check if file was saved */
  isSaved(): boolean;
  
  /** Save the source file */
  save(): Promise<void>;
  
  /** Save the source file synchronously */
  saveSync(): void;
  
  /** Copy the source file to a new location */
  copy(filePath: string, options?: SourceFileCopyOptions): SourceFile;
  
  /** Move the source file to a new location */
  move(filePath: string, options?: SourceFileMoveOptions): SourceFile;
  
  /** Delete the source file */
  delete(): Promise<void>;
  
  /** Delete the source file synchronously */
  deleteSync(): void;
  
  /** Get all import declarations */
  getImportDeclarations(): ImportDeclaration[];
  
  /** Add an import declaration */
  addImportDeclaration(structure: ImportDeclarationStructure): ImportDeclaration;
  
  /** Get all export declarations */
  getExportDeclarations(): ExportDeclaration[];
  
  /** Add an export declaration */
  addExportDeclaration(structure: ExportDeclarationStructure): ExportDeclaration;
  
  /** Get all class declarations */
  getClasses(): ClassDeclaration[];
  
  /** Get a class by name */
  getClass(name: string): ClassDeclaration | undefined;
  
  /** Add a class */
  addClass(structure: ClassDeclarationStructure): ClassDeclaration;
  
  /** Get all function declarations */
  getFunctions(): FunctionDeclaration[];
  
  /** Get a function by name */
  getFunction(name: string): FunctionDeclaration | undefined;
  
  /** Add a function */
  addFunction(structure: FunctionDeclarationStructure): FunctionDeclaration;
  
  /** Get all interface declarations */
  getInterfaces(): InterfaceDeclaration[];
  
  /** Get an interface by name */
  getInterface(name: string): InterfaceDeclaration | undefined;
  
  /** Add an interface */
  addInterface(structure: InterfaceDeclarationStructure): InterfaceDeclaration;
  
  /** Get all enum declarations */
  getEnums(): EnumDeclaration[];
  
  /** Add an enum */
  addEnum(structure: EnumDeclarationStructure): EnumDeclaration;
  
  /** Get all type alias declarations */
  getTypeAliases(): TypeAliasDeclaration[];
  
  /** Add a type alias */
  addTypeAlias(structure: TypeAliasDeclarationStructure): TypeAliasDeclaration;
  
  /** Get all variable statements */
  getVariableStatements(): VariableStatement[];
  
  /** Add a variable statement */
  addVariableStatement(structure: VariableStatementStructure): VariableStatement;
  
  /** Organize imports */
  organizeImports(): void;
  
  /** Fix unused identifiers */
  fixUnusedIdentifiers(): void;
}

Class Declaration

Represents a TypeScript class declaration.

class ClassDeclaration extends Node {
  /** Get the class name */
  getName(): string | undefined;
  
  /** Set the class name */
  setName(name: string): this;
  
  /** Get the extends clause */
  getExtends(): ExpressionWithTypeArguments | undefined;
  
  /** Set the extends clause */
  setExtends(text: string): this;
  
  /** Get implemented interfaces */
  getImplements(): ExpressionWithTypeArguments[];
  
  /** Add an implements clause */
  addImplements(text: string): ExpressionWithTypeArguments;
  
  /** Get all constructors */
  getConstructors(): ConstructorDeclaration[];
  
  /** Add a constructor */
  addConstructor(structure?: ConstructorDeclarationStructure): ConstructorDeclaration;
  
  /** Get all properties */
  getProperties(): PropertyDeclaration[];
  
  /** Get a property by name */
  getProperty(name: string): PropertyDeclaration | undefined;
  
  /** Add a property */
  addProperty(structure: PropertyDeclarationStructure): PropertyDeclaration;
  
  /** Get all methods */
  getMethods(): MethodDeclaration[];
  
  /** Get a method by name */
  getMethod(name: string): MethodDeclaration | undefined;
  
  /** Add a method */
  addMethod(structure: MethodDeclarationStructure): MethodDeclaration;
  
  /** Get all getters */
  getGetAccessors(): GetAccessorDeclaration[];
  
  /** Add a getter */
  addGetAccessor(structure: GetAccessorDeclarationStructure): GetAccessorDeclaration;
  
  /** Get all setters */
  getSetAccessors(): SetAccessorDeclaration[];
  
  /** Add a setter */
  addSetAccessor(structure: SetAccessorDeclarationStructure): SetAccessorDeclaration;
  
  /** Check if class is abstract */
  isAbstract(): boolean;
  
  /** Set abstract modifier */
  setIsAbstract(value: boolean): this;
  
  /** Check if class is exported */
  isExported(): boolean;
  
  /** Set exported */
  setIsExported(value: boolean): this;
  
  /** Check if class is default export */
  isDefaultExport(): boolean;
  
  /** Set as default export */
  setIsDefaultExport(value: boolean): this;
}

Function Declaration

Represents a function declaration.

class FunctionDeclaration extends Node {
  /** Get the function name */
  getName(): string | undefined;
  
  /** Set the function name */
  setName(name: string): this;
  
  /** Get all parameters */
  getParameters(): ParameterDeclaration[];
  
  /** Add a parameter */
  addParameter(structure: ParameterDeclarationStructure): ParameterDeclaration;
  
  /** Get the return type node */
  getReturnTypeNode(): TypeNode | undefined;
  
  /** Set the return type */
  setReturnType(type: string): this;
  
  /** Get the function body */
  getBody(): Block | undefined;
  
  /** Set the function body */
  setBodyText(text: string): this;
  
  /** Check if function is async */
  isAsync(): boolean;
  
  /** Set async modifier */
  setIsAsync(value: boolean): this;
  
  /** Check if function is a generator */
  isGenerator(): boolean;
  
  /** Set generator modifier */
  setIsGenerator(value: boolean): this;
  
  /** Check if function is exported */
  isExported(): boolean;
  
  /** Set exported */
  setIsExported(value: boolean): this;
  
  /** Check if function is default export */
  isDefaultExport(): boolean;
  
  /** Set as default export */
  setIsDefaultExport(value: boolean): this;
}

Interface Declaration

Represents an interface declaration.

class InterfaceDeclaration extends Node {
  /** Get the interface name */
  getName(): string;
  
  /** Set the interface name */
  setName(name: string): this;
  
  /** Get extends clauses */
  getExtends(): ExpressionWithTypeArguments[];
  
  /** Add an extends clause */
  addExtends(text: string): ExpressionWithTypeArguments;
  
  /** Get all properties */
  getProperties(): PropertySignature[];
  
  /** Add a property */
  addProperty(structure: PropertySignatureStructure): PropertySignature;
  
  /** Get all methods */
  getMethods(): MethodSignature[];
  
  /** Add a method */
  addMethod(structure: MethodSignatureStructure): MethodSignature;
  
  /** Get all call signatures */
  getCallSignatures(): CallSignatureDeclaration[];
  
  /** Add a call signature */
  addCallSignature(structure: CallSignatureDeclarationStructure): CallSignatureDeclaration;
  
  /** Get all construct signatures */
  getConstructSignatures(): ConstructSignatureDeclaration[];
  
  /** Add a construct signature */
  addConstructSignature(structure: ConstructSignatureDeclarationStructure): ConstructSignatureDeclaration;
  
  /** Get all index signatures */
  getIndexSignatures(): IndexSignatureDeclaration[];
  
  /** Add an index signature */
  addIndexSignature(structure: IndexSignatureDeclarationStructure): IndexSignatureDeclaration;
}

Variable Declaration

Represents a variable declaration.

class VariableDeclaration extends Node {
  /** Get the variable name */
  getName(): string;
  
  /** Set the variable name */
  setName(name: string): this;
  
  /** Get the type node */
  getTypeNode(): TypeNode | undefined;
  
  /** Set the type */
  setType(type: string): this;
  
  /** Get the initializer */
  getInitializer(): Expression | undefined;
  
  /** Set the initializer */
  setInitializer(text: string): this;
  
  /** Remove the initializer */
  removeInitializer(): this;
}

class VariableStatement extends Node {
  /** Get the declaration kind (const, let, var) */
  getDeclarationKind(): VariableDeclarationKind;
  
  /** Set the declaration kind */
  setDeclarationKind(kind: VariableDeclarationKind): this;
  
  /** Get all variable declarations */
  getDeclarations(): VariableDeclaration[];
  
  /** Add a declaration */
  addDeclaration(structure: VariableDeclarationStructure): VariableDeclaration;
}

enum VariableDeclarationKind {
  Var = "var",
  Let = "let",
  Const = "const",
}

Import and Export Declarations

class ImportDeclaration extends Node {
  /** Get the module specifier */
  getModuleSpecifier(): StringLiteral;
  
  /** Set the module specifier */
  setModuleSpecifier(text: string): this;
  
  /** Get the import clause */
  getImportClause(): ImportClause | undefined;
  
  /** Get the default import */
  getDefaultImport(): Identifier | undefined;
  
  /** Set the default import */
  setDefaultImport(name: string): this;
  
  /** Get named imports */
  getNamedImports(): ImportSpecifier[];
  
  /** Add a named import */
  addNamedImport(name: string): ImportSpecifier;
  
  /** Get namespace import */
  getNamespaceImport(): Identifier | undefined;
  
  /** Set namespace import */
  setNamespaceImport(name: string): this;
}

class ExportDeclaration extends Node {
  /** Get the module specifier */
  getModuleSpecifier(): StringLiteral | undefined;
  
  /** Set the module specifier */
  setModuleSpecifier(text: string): this;
  
  /** Get named exports */
  getNamedExports(): ExportSpecifier[];
  
  /** Add a named export */
  addNamedExport(name: string): ExportSpecifier;
  
  /** Check if it's a namespace export */
  isNamespaceExport(): boolean;
}

Expression Nodes

Common expression node types for working with TypeScript expressions.

abstract class Expression extends Node {
  /** Get the type of this expression */
  getType(): Type;
  
  /** Get the symbol of this expression */
  getSymbol(): Symbol | undefined;
}

class CallExpression extends Expression {
  /** Get the expression being called */
  getExpression(): LeftHandSideExpression;
  
  /** Get all arguments */
  getArguments(): Node[];
  
  /** Add an argument */
  addArgument(text: string): Node;
}

class PropertyAccessExpression extends Expression {
  /** Get the object being accessed */
  getExpression(): LeftHandSideExpression;
  
  /** Get the property name */
  getName(): Identifier;
}

class BinaryExpression extends Expression {
  /** Get the left operand */
  getLeft(): Expression;
  
  /** Get the operator token */
  getOperatorToken(): BinaryOperatorToken;
  
  /** Get the right operand */
  getRight(): Expression;
}

Usage Examples:

import { Project, SyntaxKind } from "ts-morph";

const project = new Project();
const sourceFile = project.createSourceFile("example.ts", `
class MyClass {
  private value: number = 0;
  
  getValue(): number {
    return this.value;
  }
  
  setValue(newValue: number): void {
    this.value = newValue;
  }
}
`);

// Get the class
const myClass = sourceFile.getClassOrThrow("MyClass");
console.log("Class name:", myClass.getName());

// Get all methods
const methods = myClass.getMethods();
console.log("Methods:", methods.map(m => m.getName()));

// Add a new method
myClass.addMethod({
  name: "increment",
  returnType: "void",
  statements: ["this.value++;"]
});

// Navigate the AST
const valueProperty = myClass.getPropertyOrThrow("value");
const initializer = valueProperty.getInitializer();
console.log("Initial value:", initializer?.getText());

// Find nodes by syntax kind
const allNumbers = sourceFile.getDescendantsOfKind(SyntaxKind.NumericLiteral);
console.log("All numbers:", allNumbers.map(n => n.getText()));

Install with Tessl CLI

npx tessl i tessl/npm-ts-morph

docs

ast-nodes.md

code-generation.md

file-system.md

index.md

project-management.md

type-system.md

tile.json