UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

391 lines (390 loc) 17.1 kB
import * as ts from "typescript"; import CodeBlockWriter from "code-block-writer"; import { Constructor } from "./../../Constructor"; import { ClassDeclarationStructure, InterfaceDeclarationStructure, TypeAliasDeclarationStructure, FunctionDeclarationStructure, EnumDeclarationStructure, NamespaceDeclarationStructure, VariableStatementStructure } from "./../../structures"; import { Node } from "./../common"; import * as classes from "./../class"; import * as enums from "./../enum"; import * as functions from "./../function"; import * as interfaces from "./../interface"; import * as namespaces from "./../namespace"; import * as types from "./../type"; import * as statement from "./../statement"; export declare type StatementedNodeExtensionType = Node<ts.SourceFile | ts.FunctionDeclaration | ts.ModuleDeclaration | ts.FunctionLikeDeclaration>; export interface StatementedNode { /** * Gets the node's statements. */ getStatements(): Node[]; /** * Adds statements. * @param text - Text of the statement or statements to add. * @returns The statements that were added. */ addStatements(text: string): Node[]; /** * Add statements. * @param writerFunction - Write the text using the provided writer. * @returns The statements that were added. */ addStatements(writerFunction: (writer: CodeBlockWriter) => void): Node[]; /** * Inserts statements at the specified index. * @param index - Index to insert at. * @param text - Text of the statement or statements to insert. * @returns The statements that were inserted. */ insertStatements(index: number, text: string): Node[]; /** * Inserts statements at the specified index. * @param index - Index to insert at. * @param writerFunction - Write the text using the provided writer. * @returns The statements that were inserted. */ insertStatements(index: number, writerFunction: (writer: CodeBlockWriter) => void): Node[]; /** * Removes the statement at the specified index. * @param index - Index to remove the statement at. */ removeStatement(index: number): this; /** * Removes the statements at the specified index range. * @param indexRange - The start and end inclusive index range to remove. */ removeStatements(indexRange: [number, number]): this; /** * Adds an class declaration as a child. * @param structure - Structure of the class declaration to add. */ addClass(structure: ClassDeclarationStructure): classes.ClassDeclaration; /** * Adds class declarations as a child. * @param structures - Structures of the class declarations to add. */ addClasses(structures: ClassDeclarationStructure[]): classes.ClassDeclaration[]; /** * Inserts an class declaration as a child. * @param index - Index to insert at. * @param structure - Structure of the class declaration to insert. */ insertClass(index: number, structure: ClassDeclarationStructure): classes.ClassDeclaration; /** * Inserts class declarations as a child. * @param index - Index to insert at. * @param structures - Structures of the class declarations to insert. */ insertClasses(index: number, structures: ClassDeclarationStructure[]): classes.ClassDeclaration[]; /** * Gets the direct class declaration children. */ getClasses(): classes.ClassDeclaration[]; /** * Gets a class. * @param name - Name of the class. */ getClass(name: string): classes.ClassDeclaration | undefined; /** * Gets a class. * @param findFunction - Function to use to find the class. */ getClass(findFunction: (declaration: classes.ClassDeclaration) => boolean): classes.ClassDeclaration | undefined; /** * Gets a class or throws if it doesn't exist. * @param name - Name of the class. */ getClassOrThrow(name: string): classes.ClassDeclaration; /** * Gets a class or throws if it doesn't exist. * @param findFunction - Function to use to find the class. */ getClassOrThrow(findFunction: (declaration: classes.ClassDeclaration) => boolean): classes.ClassDeclaration; /** * Adds an enum declaration as a child. * @param structure - Structure of the enum declaration to add. */ addEnum(structure: EnumDeclarationStructure): enums.EnumDeclaration; /** * Adds enum declarations as a child. * @param structures - Structures of the enum declarations to add. */ addEnums(structures: EnumDeclarationStructure[]): enums.EnumDeclaration[]; /** * Inserts an enum declaration as a child. * @param index - Index to insert at. * @param structure - Structure of the enum declaration to insert. */ insertEnum(index: number, structure: EnumDeclarationStructure): enums.EnumDeclaration; /** * Inserts enum declarations as a child. * @param index - Index to insert at. * @param structures - Structures of the enum declarations to insert. */ insertEnums(index: number, structures: EnumDeclarationStructure[]): enums.EnumDeclaration[]; /** * Gets the direct enum declaration children. */ getEnums(): enums.EnumDeclaration[]; /** * Gets an enum. * @param name - Name of the enum. */ getEnum(name: string): enums.EnumDeclaration | undefined; /** * Gets an enum. * @param findFunction - Function to use to find the enum. */ getEnum(findFunction: (declaration: enums.EnumDeclaration) => boolean): enums.EnumDeclaration | undefined; /** * Gets an enum or throws if it doesn't exist. * @param name - Name of the enum. */ getEnumOrThrow(name: string): enums.EnumDeclaration; /** * Gets an enum or throws if it doesn't exist. * @param findFunction - Function to use to find the enum. */ getEnumOrThrow(findFunction: (declaration: enums.EnumDeclaration) => boolean): enums.EnumDeclaration; /** * Adds a function declaration as a child. * @param structure - Structure of the function declaration to add. */ addFunction(structure: FunctionDeclarationStructure): functions.FunctionDeclaration; /** * Adds function declarations as a child. * @param structures - Structures of the function declarations to add. */ addFunctions(structures: FunctionDeclarationStructure[]): functions.FunctionDeclaration[]; /** * Inserts an function declaration as a child. * @param index - Index to insert at. * @param structure - Structure of the function declaration to insert. */ insertFunction(index: number, structure: FunctionDeclarationStructure): functions.FunctionDeclaration; /** * Inserts function declarations as a child. * @param index - Index to insert at. * @param structures - Structures of the function declarations to insert. */ insertFunctions(index: number, structures: FunctionDeclarationStructure[]): functions.FunctionDeclaration[]; /** * Gets the direct function declaration children. */ getFunctions(): functions.FunctionDeclaration[]; /** * Gets a function. * @param name - Name of the function. */ getFunction(name: string): functions.FunctionDeclaration | undefined; /** * Gets a function. * @param findFunction - Function to use to find the function. */ getFunction(findFunction: (declaration: functions.FunctionDeclaration) => boolean): functions.FunctionDeclaration | undefined; /** * Gets a function or throws if it doesn't exist. * @param name - Name of the function. */ getFunctionOrThrow(name: string): functions.FunctionDeclaration; /** * Gets a function or throws if it doesn't exist. * @param findFunction - Function to use to find the function. */ getFunctionOrThrow(findFunction: (declaration: functions.FunctionDeclaration) => boolean): functions.FunctionDeclaration; /** * Adds a interface declaration as a child. * @param structure - Structure of the interface declaration to add. */ addInterface(structure: InterfaceDeclarationStructure): interfaces.InterfaceDeclaration; /** * Adds interface declarations as a child. * @param structures - Structures of the interface declarations to add. */ addInterfaces(structures: InterfaceDeclarationStructure[]): interfaces.InterfaceDeclaration[]; /** * Inserts an interface declaration as a child. * @param index - Index to insert at. * @param structure - Structure of the interface declaration to insert. */ insertInterface(index: number, structure: InterfaceDeclarationStructure): interfaces.InterfaceDeclaration; /** * Inserts interface declarations as a child. * @param index - Index to insert at. * @param structures - Structures of the interface declarations to insert. */ insertInterfaces(index: number, structures: InterfaceDeclarationStructure[]): interfaces.InterfaceDeclaration[]; /** * Gets the direct interface declaration children. */ getInterfaces(): interfaces.InterfaceDeclaration[]; /** * Gets an interface. * @param name - Name of the interface. */ getInterface(name: string): interfaces.InterfaceDeclaration | undefined; /** * Gets an interface. * @param findFunction - Function to use to find the interface. */ getInterface(findFunction: (declaration: interfaces.InterfaceDeclaration) => boolean): interfaces.InterfaceDeclaration | undefined; /** * Gets an interface or throws if it doesn't exist. * @param name - Name of the interface. */ getInterfaceOrThrow(name: string): interfaces.InterfaceDeclaration; /** * Gets an interface or throws if it doesn't exist. * @param findFunction - Function to use to find the interface. */ getInterfaceOrThrow(findFunction: (declaration: interfaces.InterfaceDeclaration) => boolean): interfaces.InterfaceDeclaration; /** * Adds a namespace declaration as a child. * @param structure - Structure of the namespace declaration to add. */ addNamespace(structure: NamespaceDeclarationStructure): namespaces.NamespaceDeclaration; /** * Adds namespace declarations as a child. * @param structures - Structures of the namespace declarations to add. */ addNamespaces(structures: NamespaceDeclarationStructure[]): namespaces.NamespaceDeclaration[]; /** * Inserts an namespace declaration as a child. * @param index - Index to insert at. * @param structure - Structure of the namespace declaration to insert. */ insertNamespace(index: number, structure: NamespaceDeclarationStructure): namespaces.NamespaceDeclaration; /** * Inserts namespace declarations as a child. * @param index - Index to insert at. * @param structures - Structures of the namespace declarations to insert. */ insertNamespaces(index: number, structures: NamespaceDeclarationStructure[]): namespaces.NamespaceDeclaration[]; /** * Gets the direct namespace declaration children. */ getNamespaces(): namespaces.NamespaceDeclaration[]; /** * Gets a namespace. * @param name - Name of the namespace. */ getNamespace(name: string): namespaces.NamespaceDeclaration | undefined; /** * Gets a namespace. * @param findFunction - Function to use to find the namespace. */ getNamespace(findFunction: (declaration: namespaces.NamespaceDeclaration) => boolean): namespaces.NamespaceDeclaration | undefined; /** * Gets a namespace or throws if it doesn't exist. * @param name - Name of the namespace. */ getNamespaceOrThrow(name: string): namespaces.NamespaceDeclaration; /** * Gets a namespace or throws if it doesn't exist. * @param findFunction - Function to use to find the namespace. */ getNamespaceOrThrow(findFunction: (declaration: namespaces.NamespaceDeclaration) => boolean): namespaces.NamespaceDeclaration; /** * Adds a type alias declaration as a child. * @param structure - Structure of the type alias declaration to add. */ addTypeAlias(structure: TypeAliasDeclarationStructure): types.TypeAliasDeclaration; /** * Adds type alias declarations as a child. * @param structures - Structures of the type alias declarations to add. */ addTypeAliases(structures: TypeAliasDeclarationStructure[]): types.TypeAliasDeclaration[]; /** * Inserts an type alias declaration as a child. * @param index - Index to insert at. * @param structure - Structure of the type alias declaration to insert. */ insertTypeAlias(index: number, structure: TypeAliasDeclarationStructure): types.TypeAliasDeclaration; /** * Inserts type alias declarations as a child. * @param index - Index to insert at. * @param structures - Structures of the type alias declarations to insert. */ insertTypeAliases(index: number, structures: TypeAliasDeclarationStructure[]): types.TypeAliasDeclaration[]; /** * Gets the direct type alias declaration children. */ getTypeAliases(): types.TypeAliasDeclaration[]; /** * Gets a type alias. * @param name - Name of the type alias. */ getTypeAlias(name: string): types.TypeAliasDeclaration | undefined; /** * Gets a type alias. * @param findFunction - Function to use to find the type alias. */ getTypeAlias(findFunction: (declaration: types.TypeAliasDeclaration) => boolean): types.TypeAliasDeclaration | undefined; /** * Gets a type alias or throws if it doesn't exist. * @param name - Name of the type alias. */ getTypeAliasOrThrow(name: string): types.TypeAliasDeclaration; /** * Gets a type alias or throws if it doesn't exist. * @param findFunction - Function to use to find the type alias. */ getTypeAliasOrThrow(findFunction: (declaration: types.TypeAliasDeclaration) => boolean): types.TypeAliasDeclaration; /** * Adds a variable statement. * @param structure - Structure of the variable statement. */ addVariableStatement(structure: VariableStatementStructure): statement.VariableStatement; /** * Adds variable statements. * @param structures - Structures of the variable statements. */ addVariableStatements(structures: VariableStatementStructure[]): statement.VariableStatement[]; /** * Inserts a variable statement. * @param structure - Structure of the variable statement. */ insertVariableStatement(index: number, structure: VariableStatementStructure): statement.VariableStatement; /** * Inserts variable statements. * @param structures - Structures of the variable statements. */ insertVariableStatements(index: number, structures: VariableStatementStructure[]): statement.VariableStatement[]; /** * Gets the direct variable statement children. */ getVariableStatements(): statement.VariableStatement[]; /** * Gets a variable statement. * @param findFunction - Function to use to find the variable statement. */ getVariableStatement(findFunction: (declaration: statement.VariableStatement) => boolean): statement.VariableStatement | undefined; /** * Gets a variable statement or throws if it doesn't exist. * @param findFunction - Function to use to find the variable statement. */ getVariableStatementOrThrow(findFunction: (declaration: statement.VariableStatement) => boolean): statement.VariableStatement; /** * Gets all the variable declarations within all the variable declarations of the direct variable statement children. */ getVariableDeclarations(): statement.VariableDeclaration[]; /** * Gets a variable declaration. * @param name - Name of the variable declaration. */ getVariableDeclaration(name: string): statement.VariableDeclaration | undefined; /** * Gets a variable declaration. * @param findFunction - Function to use to find the variable declaration. */ getVariableDeclaration(findFunction: (declaration: statement.VariableDeclaration) => boolean): statement.VariableDeclaration | undefined; /** * Gets a variable declaration or throws if it doesn't exist. * @param name - Name of the variable declaration. */ getVariableDeclarationOrThrow(name: string): statement.VariableDeclaration; /** * Gets a variable declaration or throws if it doesn't exist. * @param findFunction - Function to use to find the variable declaration. */ getVariableDeclarationOrThrow(findFunction: (declaration: statement.VariableDeclaration) => boolean): statement.VariableDeclaration; } export declare function StatementedNode<T extends Constructor<StatementedNodeExtensionType>>(Base: T): Constructor<StatementedNode> & T;