1namespace ts { 2 export type AffectedFileResult<T> = { result: T; affected: SourceFile | Program; } | undefined; 3 4 export interface BuilderProgramHost { 5 /** 6 * return true if file names are treated with case sensitivity 7 */ 8 useCaseSensitiveFileNames(): boolean; 9 /** 10 * If provided this would be used this hash instead of actual file shape text for detecting changes 11 */ 12 createHash?: (data: string) => string; 13 /** 14 * When emit or emitNextAffectedFile are called without writeFile, 15 * this callback if present would be used to write files 16 */ 17 writeFile?: WriteFileCallback; 18 } 19 20 /** 21 * Builder to manage the program state changes 22 */ 23 export interface BuilderProgram { 24 /*@internal*/ 25 getState(): ReusableBuilderProgramState; 26 /*@internal*/ 27 backupState(): void; 28 /*@internal*/ 29 restoreState(): void; 30 /** 31 * Returns current program 32 */ 33 getProgram(): Program; 34 /** 35 * Returns current program that could be undefined if the program was released 36 */ 37 /*@internal*/ 38 getProgramOrUndefined(): Program | undefined; 39 /** 40 * Releases reference to the program, making all the other operations that need program to fail. 41 */ 42 /*@internal*/ 43 releaseProgram(): void; 44 /** 45 * Get compiler options of the program 46 */ 47 getCompilerOptions(): CompilerOptions; 48 /** 49 * Get the source file in the program with file name 50 */ 51 getSourceFile(fileName: string): SourceFile | undefined; 52 /** 53 * Get a list of files in the program 54 */ 55 getSourceFiles(): readonly SourceFile[]; 56 /** 57 * Get the diagnostics for compiler options 58 */ 59 getOptionsDiagnostics(cancellationToken?: CancellationToken): readonly Diagnostic[]; 60 /** 61 * Get the diagnostics that dont belong to any file 62 */ 63 getGlobalDiagnostics(cancellationToken?: CancellationToken): readonly Diagnostic[]; 64 /** 65 * Get the diagnostics from config file parsing 66 */ 67 getConfigFileParsingDiagnostics(): readonly Diagnostic[]; 68 /** 69 * Get the syntax diagnostics, for all source files if source file is not supplied 70 */ 71 getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly Diagnostic[]; 72 /** 73 * Get the declaration diagnostics, for all source files if source file is not supplied 74 */ 75 getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly DiagnosticWithLocation[]; 76 /** 77 * Get all the dependencies of the file 78 */ 79 getAllDependencies(sourceFile: SourceFile): readonly string[]; 80 81 /** 82 * Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program 83 * The semantic diagnostics are cached and managed here 84 * Note that it is assumed that when asked about semantic diagnostics through this API, 85 * the file has been taken out of affected files so it is safe to use cache or get from program and cache the diagnostics 86 * In case of SemanticDiagnosticsBuilderProgram if the source file is not provided, 87 * it will iterate through all the affected files, to ensure that cache stays valid and yet provide a way to get all semantic diagnostics 88 */ 89 getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly Diagnostic[]; 90 /** 91 * Emits the JavaScript and declaration files. 92 * When targetSource file is specified, emits the files corresponding to that source file, 93 * otherwise for the whole program. 94 * In case of EmitAndSemanticDiagnosticsBuilderProgram, when targetSourceFile is specified, 95 * it is assumed that that file is handled from affected file list. If targetSourceFile is not specified, 96 * it will only emit all the affected files instead of whole program 97 * 98 * The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host 99 * in that order would be used to write the files 100 */ 101 emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult; 102 /*@internal*/ 103 emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult; 104 /** 105 * Get the current directory of the program 106 */ 107 getCurrentDirectory(): string; 108 /*@internal*/ 109 close(): void; 110 } 111 112 /** 113 * The builder that caches the semantic diagnostics for the program and handles the changed files and affected files 114 */ 115 export interface SemanticDiagnosticsBuilderProgram extends BuilderProgram { 116 /** 117 * Gets the semantic diagnostics from the program for the next affected file and caches it 118 * Returns undefined if the iteration is complete 119 */ 120 getSemanticDiagnosticsOfNextAffectedFile(cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<readonly Diagnostic[]>; 121 } 122 123 /** 124 * The builder that can handle the changes in program and iterate through changed file to emit the files 125 * The semantic diagnostics are cached per file and managed by clearing for the changed/affected files 126 */ 127 export interface EmitAndSemanticDiagnosticsBuilderProgram extends SemanticDiagnosticsBuilderProgram { 128 /** 129 * Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete 130 * The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host 131 * in that order would be used to write the files 132 */ 133 emitNextAffectedFile(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult>; 134 } 135 136 /** 137 * Create the builder to manage semantic diagnostics and cache them 138 */ 139 export function createSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): SemanticDiagnosticsBuilderProgram; 140 export function createSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): SemanticDiagnosticsBuilderProgram; 141 export function createSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]) { 142 return createBuilderProgram(BuilderProgramKind.SemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences)); 143 } 144 145 /** 146 * Create the builder that can handle the changes in program and iterate through changed files 147 * to emit the those files and manage semantic diagnostics cache as well 148 */ 149 export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): EmitAndSemanticDiagnosticsBuilderProgram; 150 export function createEmitAndSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): EmitAndSemanticDiagnosticsBuilderProgram; 151 export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]) { 152 return createBuilderProgram(BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences)); 153 } 154 155 /** 156 * Creates a builder thats just abstraction over program and can be used with watch 157 */ 158 export function createAbstractBuilder(newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): BuilderProgram; 159 export function createAbstractBuilder(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram; 160 export function createAbstractBuilder(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | BuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram { 161 const { newProgram, configFileParsingDiagnostics: newConfigFileParsingDiagnostics } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences); 162 return createRedirectedBuilderProgram({ program: newProgram, compilerOptions: newProgram.getCompilerOptions() }, newConfigFileParsingDiagnostics); 163 } 164} 165