UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

143 lines (142 loc) 5.05 kB
import * as ts from "typescript"; import { Constructor } from "./../../Constructor"; import { ImportDeclarationStructure, ExportDeclarationStructure, SourceFileStructure } from "./../../structures"; import { TextInsertableNode } from "./../base"; import { Node, Symbol } from "./../common"; import { StatementedNode } from "./../statement"; import { Diagnostic, EmitResult } from "./../tools"; import { ImportDeclaration } from "./ImportDeclaration"; import { ExportDeclaration } from "./ExportDeclaration"; export declare const SourceFileBase: Constructor<StatementedNode> & Constructor<TextInsertableNode> & typeof Node; export declare class SourceFile extends SourceFileBase<ts.SourceFile> { /** * Fills the node from a structure. * @param structure - Structure to fill. */ fill(structure: Partial<SourceFileStructure>): this; /** * Gets the file path. */ getFilePath(): string; /** * Copy this source file to a new file. * @param filePath - A new file path. Can be relative to the original file or an absolute path. */ copy(filePath: string): SourceFile; /** * Asynchronously saves this file with any changes. */ save(): Promise<void>; /** * Synchronously saves this file with any changes. */ saveSync(): void; /** * Gets any referenced files. */ getReferencedFiles(): SourceFile[]; /** * Gets the source files for any type reference directives. */ getTypeReferenceDirectives(): SourceFile[]; /** * Gets the source file language variant. */ getLanguageVariant(): ts.LanguageVariant; /** * Gets if this is a declaration file. */ isDeclarationFile(): boolean; /** * Gets if this source file has been saved or if the latest changes have been saved. */ isSaved(): boolean; /** * Add an import. * @param structure - Structure that represents the import. */ addImport(structure: ImportDeclarationStructure): ImportDeclaration; /** * Add imports. * @param structures - Structures that represent the imports. */ addImports(structures: ImportDeclarationStructure[]): ImportDeclaration[]; /** * Insert an import. * @param index - Index to insert at. * @param structure - Structure that represents the import. */ insertImport(index: number, structure: ImportDeclarationStructure): ImportDeclaration; /** * Insert imports into a file. * @param index - Index to insert at. * @param structures - Structures that represent the imports to insert. */ insertImports(index: number, structures: ImportDeclarationStructure[]): ImportDeclaration[]; /** * Gets the first import declaration that matches a condition, or undefined if it doesn't exist. * @param condition - Condition to get the import by. */ getImport(condition: (importDeclaration: ImportDeclaration) => boolean): ImportDeclaration | undefined; /** * Get the file's import declarations. */ getImports(): ImportDeclaration[]; /** * Add an export. * @param structure - Structure that represents the export. */ addExport(structure: ExportDeclarationStructure): ImportDeclaration; /** * Add exports. * @param structures - Structures that represent the exports. */ addExports(structures: ExportDeclarationStructure[]): ImportDeclaration[]; /** * Insert an export. * @param index - Index to insert at. * @param structure - Structure that represents the export. */ insertExport(index: number, structure: ExportDeclarationStructure): ImportDeclaration; /** * Insert exports into a file. * @param index - Index to insert at. * @param structures - Structures that represent the exports to insert. */ insertExports(index: number, structures: ExportDeclarationStructure[]): ImportDeclaration[]; /** * Gets the first export declaration that matches a condition, or undefined if it doesn't exist. * @param condition - Condition to get the export by. */ getExport(condition: (exportDeclaration: ExportDeclaration) => boolean): ExportDeclaration | undefined; /** * Get the file's export declarations. */ getExports(): ExportDeclaration[]; /** * Gets the default export symbol of the file. */ getDefaultExportSymbol(): Symbol | undefined; /** * Gets the compiler diagnostics. */ getDiagnostics(): Diagnostic[]; /** * Removes any "export default"; */ removeDefaultExport(defaultExportSymbol?: Symbol | undefined): this; /** * Emits the source file. */ emit(options?: { emitOnlyDtsFiles?: boolean; }): EmitResult; /** * Formats the source file text using the internal typescript printer. * * WARNING: This will dispose any previously navigated descendant nodes. */ formatText(opts?: { removeComments?: boolean; }): void; }