UNPKG

ts-simple-ast

Version:

TypeScript compiler wrapper for AST navigation and code generation.

265 lines (264 loc) 10.3 kB
import * as ts from "typescript"; import { Directory } from "./../../fileSystem"; import { Constructor } from "./../../Constructor"; import { ImportDeclarationStructure, ExportDeclarationStructure, ExportAssignmentStructure, SourceFileStructure } from "./../../structures"; import { TextInsertableNode } from "./../base"; import { Node, Symbol } from "./../common"; import { StatementedNode } from "./../statement"; import { Diagnostic, EmitResult, EmitOutput, FormatCodeSettings } from "./../tools"; import { ImportDeclaration } from "./ImportDeclaration"; import { ExportDeclaration } from "./ExportDeclaration"; import { ExportAssignment } from "./ExportAssignment"; 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. * @param options - Options for copying. */ copy(filePath: string, options?: { overwrite?: boolean; }): 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. */ addImportDeclaration(structure: ImportDeclarationStructure): ImportDeclaration; /** * Add imports. * @param structures - Structures that represent the imports. */ addImportDeclarations(structures: ImportDeclarationStructure[]): ImportDeclaration[]; /** * Insert an import. * @param index - Index to insert at. * @param structure - Structure that represents the import. */ insertImportDeclaration(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. */ insertImportDeclarations(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. */ getImportDeclarations(): ImportDeclaration[]; /** * Add export declarations. * @param structure - Structure that represents the export. */ addExportDeclaration(structure: ExportDeclarationStructure): ExportDeclaration; /** * Add export declarations. * @param structures - Structures that represent the exports. */ addExportDeclarations(structures: ExportDeclarationStructure[]): ExportDeclaration[]; /** * Insert an export declaration. * @param index - Index to insert at. * @param structure - Structure that represents the export. */ insertExportDeclaration(index: number, structure: ExportDeclarationStructure): ExportDeclaration; /** * Insert export declarations into a file. * @param index - Index to insert at. * @param structures - Structures that represent the exports to insert. */ insertExportDeclarations(index: number, structures: ExportDeclarationStructure[]): ExportDeclaration[]; /** * Gets the first export declaration that matches a condition, or undefined if it doesn't exist. * @param condition - Condition to get the export declaration by. */ getExportDeclaration(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 declaration by. */ getExportDeclarationOrThrow(condition: (exportDeclaration: ExportDeclaration) => boolean): ExportDeclaration; /** * Get the file's export declarations. */ getExportDeclarations(): ExportDeclaration[]; /** * Add export assignments. * @param structure - Structure that represents the export. */ addExportAssignment(structure: ExportAssignmentStructure): ExportAssignment; /** * Add export assignments. * @param structures - Structures that represent the exports. */ addExportAssignments(structures: ExportAssignmentStructure[]): ExportAssignment[]; /** * Insert an export assignment. * @param index - Index to insert at. * @param structure - Structure that represents the export. */ insertExportAssignment(index: number, structure: ExportAssignmentStructure): ExportAssignment; /** * Insert export assignments into a file. * @param index - Index to insert at. * @param structures - Structures that represent the exports to insert. */ insertExportAssignments(index: number, structures: ExportAssignmentStructure[]): ExportAssignment[]; /** * Gets the first export assignment that matches a condition, or undefined if it doesn't exist. * @param condition - Condition to get the export assignment by. */ getExportAssignment(condition: (exportAssignment: ExportAssignment) => boolean): ExportAssignment | undefined; /** * Gets the first export assignment that matches a condition, or throws if it doesn't exist. * @param condition - Condition to get the export assignment by. */ getExportAssignmentOrThrow(condition: (exportAssignment: ExportAssignment) => boolean): ExportAssignment; /** * Get the file's export assignments. */ getExportAssignments(): ExportAssignment[]; /** * 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; /** * Gets the emit output of this source file. * @param options - Emit options. */ getEmitOutput(options?: { emitOnlyDtsFiles?: boolean; }): EmitOutput; /** * 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); }