UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

218 lines (217 loc) 8.05 kB
import * as ts from "typescript"; import { Directory } from "./../../fileSystem"; 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, FormatCodeSettings } from "./../tools"; import { ImportDeclaration } from "./ImportDeclaration"; import { ExportDeclaration } from "./ExportDeclaration"; import { FileSystemRefreshResult } from "./FileSystemRefreshResult"; 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; /** * Gets the file path's base name. */ getBaseName(): string; /** * Gets the directory that the source file is contained in. */ getDirectory(): Directory; /** * Gets the directory path that the source file is contained in. */ getDirectoryPath(): 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 deletes the file from the file system. */ delete(): Promise<void>; /** * Synchronously deletes the file from the file system. */ deleteSync(): void; /** * 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; /** * Gets the first import declaration that matches a condition, or throws if it doesn't exist. * @param condition - Condition to get the import by. */ getImportOrThrow(condition: (importDeclaration: ImportDeclaration) => boolean): ImportDeclaration; /** * 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; /** * Gets the first export declaration that matches a condition, or throws if it doesn't exist. * @param condition - Condition to get the export by. */ getExportOrThrow(condition: (exportDeclaration: ExportDeclaration) => boolean): ExportDeclaration; /** * Get the file's export declarations. */ getExports(): ExportDeclaration[]; /** * Gets the default export symbol of the file. */ getDefaultExportSymbol(): Symbol | undefined; /** * Gets the default export symbol of the file or throws if it doesn't exist. */ getDefaultExportSymbolOrThrow(): Symbol; /** * Gets the syntactic, semantic, and declaration diagnostics. */ getDiagnostics(): Diagnostic[]; /** * Gets the pre-emit diagnostics. */ getPreEmitDiagnostics(): Diagnostic[]; /** * Removes any "export default"; */ removeDefaultExport(defaultExportSymbol?: Symbol | undefined): this; /** * Deindents the line at the specified position. * @param pos - Position. * @param times - Times to unindent. Specify a negative value to indent. */ unindent(pos: number, times?: number): this; /** * Deindents the lines within the specified range. * @param positionRange - Position range. * @param times - Times to unindent. Specify a negative value to indent. */ unindent(positionRange: [number, number], times?: number): this; /** * Indents the line at the specified position. * @param pos - Position. * @param times - Times to indent. Specify a negative value to unindent. */ indent(pos: number, times?: number): this; /** * Indents the lines within the specified range. * @param positionRange - Position range. * @param times - Times to indent. Specify a negative value to unindent. */ indent(positionRange: [number, number], times?: number): this; /** * Emits the source file. */ emit(options?: { emitOnlyDtsFiles?: boolean; }): EmitResult; /** * Formats the source file text using the internal TypeScript formatting API. */ formatText(settings?: FormatCodeSettings): void; /** * Refresh the source file from the file system. * * WARNING: When updating from the file system, this will "forget" any previously navigated nodes. * @returns What action ended up taking place. */ refreshFromFileSystem(): Promise<FileSystemRefreshResult>; /** * Synchronously refreshes the source file from the file system. * * WARNING: When updating from the file system, this will "forget" any previously navigated nodes. * @returns What action ended up taking place. */ refreshFromFileSystemSync(): FileSystemRefreshResult; private _refreshFromFileSystemInternal(fileReadResult); }