ts-simple-ast
Version:
TypeScript compiler wrapper for AST navigation and code generation.
246 lines (245 loc) • 10.3 kB
TypeScript
import * as ts from "typescript";
import * as compiler from "./compiler";
import { SourceFileStructure } from "./structures";
import { FileSystemHost, Directory } from "./fileSystem";
import { ManipulationSettings, ManipulationSettingsContainer } from "./ManipulationSettings";
export interface Options {
/** Compiler options */
compilerOptions?: ts.CompilerOptions;
/** File path to the tsconfig.json file */
tsConfigFilePath?: string;
/** Manipulation settings */
manipulationSettings?: Partial<ManipulationSettings>;
}
/**
* Compiler wrapper.
*/
export declare class TsSimpleAst {
private fileSystem;
/**
* Initializes a new instance.
* @param options - Optional options.
* @param fileSystem - Optional file system host. Useful for mocking access to the file system.
*/
constructor(options?: Options, fileSystem?: FileSystemHost);
/** Gets the manipulation settings. */
readonly manipulationSettings: ManipulationSettingsContainer;
/**
* Adds an existing directory from the path.
*
* Will return the directory if it was already added.
* @param dirPath - Path to add the directory at.
*/
addExistingDirectory(dirPath: string): Directory;
/**
* Creates a directory at the specified path.
* Note: Will not save the directory to disk until one of its source files is saved.
* @param dirPath - Path to create the directory at.
* @throws - InvalidOperationError if a directory already exists at the provided file path.
*/
createDirectory(dirPath: string): Directory;
/**
* Gets a directory by the specified path or throws it doesn't exist.
* @param dirPath - Path to create the directory at.
*/
getDirectoryOrThrow(dirPath: string): Directory;
/**
* Gets a directory by the specified path or returns undefined if it doesn't exist.
* @param dirPath - Directory path.
*/
getDirectory(dirPath: string): Directory | undefined;
/**
* Gets all the directories.
*/
getDirectories(): Directory[];
/**
* Gets the directories without a parent.
*/
getRootDirectories(): Directory[];
/**
* Add source files based on file globs.
* @param fileGlobs - File globs to add files based on.
* @returns The matched source files.
*/
addExistingSourceFiles(...fileGlobs: string[]): compiler.SourceFile[];
/**
* Adds an existing source file from a file path.
*
* Will return the source file if it was already added.
* @param filePath - File path to get the file from.
*/
addExistingSourceFile(filePath: string): compiler.SourceFile;
/**
* Creates a source file at the specified file path.
*
* Note: The file will not be created and saved to the file system until .save() is called on the source file.
* @param filePath - File path of the source file.
* @throws - InvalidOperationError if a source file already exists at the provided file path.
*/
createSourceFile(filePath: string): compiler.SourceFile;
/**
* Creates a source file at the specified file path with the specified text.
*
* Note: The file will not be created and saved to the file system until .save() is called on the source file.
* @param filePath - File path of the source file.
* @param sourceFileText - Text of the source file.
* @throws - InvalidOperationError if a source file already exists at the provided file path.
*/
createSourceFile(filePath: string, sourceFileText: string): compiler.SourceFile;
/**
* Creates a source file at the specified file path with the specified text.
*
* Note: The file will not be created and saved to the file system until .save() is called on the source file.
* @param filePath - File path of the source file.
* @param structure - Structure that represents the source file.
* @throws - InvalidOperationError if a source file already exists at the provided file path.
*/
createSourceFile(filePath: string, structure: SourceFileStructure): compiler.SourceFile;
/**
* Removes a source file from the AST.
* @param sourceFile - Source file to remove.
* @returns True if removed.
*/
removeSourceFile(sourceFile: compiler.SourceFile): boolean;
/**
* Gets a source file by a file name or file path. Throws an error if it doesn't exist.
* @param fileNameOrPath - File name or path that the path could end with or equal.
*/
getSourceFileOrThrow(fileNameOrPath: string): compiler.SourceFile;
/**
* Gets a source file by a search function. Throws an erorr if it doesn't exist.
* @param searchFunction - Search function.
*/
getSourceFileOrThrow(searchFunction: (file: compiler.SourceFile) => boolean): compiler.SourceFile;
/**
* Gets a source file by a file name or file path. Returns undefined if none exists.
* @param fileNameOrPath - File name or path that the path could end with or equal.
*/
getSourceFile(fileNameOrPath: string): compiler.SourceFile | undefined;
/**
* Gets a source file by a search function. Returns undefined if none exists.
* @param searchFunction - Search function.
*/
getSourceFile(searchFunction: (file: compiler.SourceFile) => boolean): compiler.SourceFile | undefined;
/**
* Gets all the source files contained in the compiler wrapper.
* @param globPattern - Glob pattern for filtering out the source files.
*/
getSourceFiles(globPattern?: string): compiler.SourceFile[];
/**
* Saves all the unsaved source files.
*/
saveUnsavedSourceFiles(): Promise<void[]>;
/**
* Saves all the unsaved source files synchronously.
*
* Remarks: This might be very slow compared to the asynchronous version if there are a lot of files.
*/
saveUnsavedSourceFilesSync(): void;
private getUnsavedSourceFiles();
/**
* Gets the compiler diagnostics.
*/
getDiagnostics(): compiler.Diagnostic[];
/**
* Gets the pre-emit diagnostics.
*/
getPreEmitDiagnostics(): compiler.Diagnostic[];
/**
* Gets the language service.
*/
getLanguageService(): compiler.LanguageService;
/**
* Gets the program.
*/
getProgram(): compiler.Program;
/**
* Gets the type checker.
*/
getTypeChecker(): compiler.TypeChecker;
/**
* Emits all the source files.
* @param emitOptions - Optional emit options.
*/
emit(emitOptions?: compiler.EmitOptions): compiler.EmitResult;
/**
* Gets the compiler options.
*/
getCompilerOptions(): {
[x: string]: string | number | boolean | string[] | (string | number)[] | ts.JsonSourceFile | ts.MapLike<string[]> | ts.PluginImport[] | null | undefined;
allowJs?: boolean | undefined;
allowSyntheticDefaultImports?: boolean | undefined;
allowUnreachableCode?: boolean | undefined;
allowUnusedLabels?: boolean | undefined;
alwaysStrict?: boolean | undefined;
baseUrl?: string | undefined;
charset?: string | undefined;
checkJs?: boolean | undefined;
declaration?: boolean | undefined;
declarationDir?: string | undefined;
disableSizeLimit?: boolean | undefined;
downlevelIteration?: boolean | undefined;
emitBOM?: boolean | undefined;
emitDecoratorMetadata?: boolean | undefined;
experimentalDecorators?: boolean | undefined;
forceConsistentCasingInFileNames?: boolean | undefined;
importHelpers?: boolean | undefined;
inlineSourceMap?: boolean | undefined;
inlineSources?: boolean | undefined;
isolatedModules?: boolean | undefined;
jsx?: ts.JsxEmit | undefined;
lib?: string[] | undefined;
locale?: string | undefined;
mapRoot?: string | undefined;
maxNodeModuleJsDepth?: number | undefined;
module?: ts.ModuleKind | undefined;
moduleResolution?: ts.ModuleResolutionKind | undefined;
newLine?: ts.NewLineKind | undefined;
noEmit?: boolean | undefined;
noEmitHelpers?: boolean | undefined;
noEmitOnError?: boolean | undefined;
noErrorTruncation?: boolean | undefined;
noFallthroughCasesInSwitch?: boolean | undefined;
noImplicitAny?: boolean | undefined;
noImplicitReturns?: boolean | undefined;
noImplicitThis?: boolean | undefined;
noStrictGenericChecks?: boolean | undefined;
noUnusedLocals?: boolean | undefined;
noUnusedParameters?: boolean | undefined;
noImplicitUseStrict?: boolean | undefined;
noLib?: boolean | undefined;
noResolve?: boolean | undefined;
out?: string | undefined;
outDir?: string | undefined;
outFile?: string | undefined;
paths?: ts.MapLike<string[]> | undefined;
preserveConstEnums?: boolean | undefined;
preserveSymlinks?: boolean | undefined;
project?: string | undefined;
reactNamespace?: string | undefined;
jsxFactory?: string | undefined;
removeComments?: boolean | undefined;
rootDir?: string | undefined;
rootDirs?: string[] | undefined;
skipLibCheck?: boolean | undefined;
skipDefaultLibCheck?: boolean | undefined;
sourceMap?: boolean | undefined;
sourceRoot?: string | undefined;
strict?: boolean | undefined;
strictFunctionTypes?: boolean | undefined;
strictNullChecks?: boolean | undefined;
suppressExcessPropertyErrors?: boolean | undefined;
suppressImplicitAnyIndexErrors?: boolean | undefined;
target?: ts.ScriptTarget | undefined;
traceResolution?: boolean | undefined;
types?: string[] | undefined;
typeRoots?: string[] | undefined;
};
/**
* Forgets the nodes created in the scope of the passed in block.
*
* This is an advanced method that can be used to easily "forget" all the nodes created within the scope of the block.
* @param block - Block of code to run.
*/
forgetNodesCreatedInBlock(block: (remember: (...node: compiler.Node[]) => void) => void): void;
}