• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import {
2    BuilderCreationParameters,
3    BuilderProgramKind,
4    CancellationToken,
5    CompilerHost,
6    CompilerOptions,
7    createBuilderProgram,
8    createRedirectedBuilderProgram,
9    CustomTransformers,
10    Diagnostic,
11    DiagnosticWithLocation,
12    EmitResult,
13    getBuilderCreationParameters,
14    PerformanceDotting,
15    Program,
16    ProjectReference,
17    readBuilderProgram,
18    ReusableBuilderProgramState,
19    SavedBuildProgramEmitState,
20    SourceFile,
21    WriteFileCallback,
22} from "./_namespaces/ts";
23
24export type AffectedFileResult<T> = { result: T; affected: SourceFile | Program; } | undefined;
25
26export interface BuilderProgramHost {
27    /**
28     * return true if file names are treated with case sensitivity
29     */
30    useCaseSensitiveFileNames(): boolean;
31    /**
32     * If provided this would be used this hash instead of actual file shape text for detecting changes
33     */
34    createHash?: (data: string) => string;
35    /**
36     * When emit or emitNextAffectedFile are called without writeFile,
37     * this callback if present would be used to write files
38     */
39    writeFile?: WriteFileCallback;
40    /**
41     * disable using source file version as signature for testing
42     * @internal
43     */
44    disableUseFileVersionAsSignature?: boolean;
45    /**
46     * Store the list of files that update signature during the emit
47     * @internal
48     */
49    storeFilesChangingSignatureDuringEmit?: boolean;
50    /**
51     * Gets the current time
52     * @internal
53     */
54    now?(): Date;
55}
56
57/**
58 * Builder to manage the program state changes
59 */
60export interface BuilderProgram {
61    /** @internal */
62    getState(): ReusableBuilderProgramState;
63    /** @internal */
64    saveEmitState(): SavedBuildProgramEmitState;
65    /** @internal */
66    restoreEmitState(saved: SavedBuildProgramEmitState): void;
67    /** @internal */
68    hasChangedEmitSignature?(): boolean;
69    /**
70     * Returns current program
71     */
72    getProgram(): Program;
73    /**
74     * Returns current program that could be undefined if the program was released
75     * @internal
76     */
77    getProgramOrUndefined(): Program | undefined;
78    /**
79     * Releases reference to the program, making all the other operations that need program to fail.
80     * @internal
81     */
82    releaseProgram(): void;
83    /**
84     * Get compiler options of the program
85     */
86    getCompilerOptions(): CompilerOptions;
87    /**
88     * Get the source file in the program with file name
89     */
90    getSourceFile(fileName: string): SourceFile | undefined;
91    /**
92     * Get a list of files in the program
93     */
94    getSourceFiles(): readonly SourceFile[];
95    /**
96     * Get the diagnostics for compiler options
97     */
98    getOptionsDiagnostics(cancellationToken?: CancellationToken): readonly Diagnostic[];
99    /**
100     * Get the diagnostics that dont belong to any file
101     */
102    getGlobalDiagnostics(cancellationToken?: CancellationToken): readonly Diagnostic[];
103    /**
104     * Get the diagnostics from config file parsing
105     */
106    getConfigFileParsingDiagnostics(): readonly Diagnostic[];
107    /**
108     * Get the syntax diagnostics, for all source files if source file is not supplied
109     */
110    getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly Diagnostic[];
111    /**
112     * Get the declaration diagnostics, for all source files if source file is not supplied
113     */
114    getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly DiagnosticWithLocation[];
115    /**
116     * Get all the dependencies of the file
117     */
118    getAllDependencies(sourceFile: SourceFile): readonly string[];
119
120    /**
121     * Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
122     * The semantic diagnostics are cached and managed here
123     * Note that it is assumed that when asked about semantic diagnostics through this API,
124     * the file has been taken out of affected files so it is safe to use cache or get from program and cache the diagnostics
125     * In case of SemanticDiagnosticsBuilderProgram if the source file is not provided,
126     * it will iterate through all the affected files, to ensure that cache stays valid and yet provide a way to get all semantic diagnostics
127     */
128    getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): readonly Diagnostic[];
129    /**
130     * Emits the JavaScript and declaration files.
131     * When targetSource file is specified, emits the files corresponding to that source file,
132     * otherwise for the whole program.
133     * In case of EmitAndSemanticDiagnosticsBuilderProgram, when targetSourceFile is specified,
134     * it is assumed that that file is handled from affected file list. If targetSourceFile is not specified,
135     * it will only emit all the affected files instead of whole program
136     *
137     * The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host
138     * in that order would be used to write the files
139     */
140    emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
141    /** @internal */
142    emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult;
143    /**
144     * Get the current directory of the program
145     */
146    getCurrentDirectory(): string;
147    /** @internal */
148    close(): void;
149
150    isFileUpdateInConstEnumCache?(sourceFile: SourceFile): boolean;
151    builderProgramForLinter?: EmitAndSemanticDiagnosticsBuilderProgram;
152}
153
154/**
155 * The builder that caches the semantic diagnostics for the program and handles the changed files and affected files
156 */
157export interface SemanticDiagnosticsBuilderProgram extends BuilderProgram {
158    /**
159     * Gets the semantic diagnostics from the program for the next affected file and caches it
160     * Returns undefined if the iteration is complete
161     */
162    getSemanticDiagnosticsOfNextAffectedFile(cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<readonly Diagnostic[]>;
163}
164
165/**
166 * The builder that can handle the changes in program and iterate through changed file to emit the files
167 * The semantic diagnostics are cached per file and managed by clearing for the changed/affected files
168 */
169export interface EmitAndSemanticDiagnosticsBuilderProgram extends SemanticDiagnosticsBuilderProgram {
170    /**
171     * Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete
172     * The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host
173     * in that order would be used to write the files
174     */
175    emitNextAffectedFile(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult>;
176}
177
178/**
179 * Create the builder to manage semantic diagnostics and cache them
180 */
181export function createSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): SemanticDiagnosticsBuilderProgram;
182export function createSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): SemanticDiagnosticsBuilderProgram;
183export function createSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]) {
184    return createBuilderProgram(BuilderProgramKind.SemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences));
185}
186
187/**
188 * Create the builder that can handle the changes in program and iterate through changed files
189 * to emit the those files and manage semantic diagnostics cache as well
190 */
191export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): EmitAndSemanticDiagnosticsBuilderProgram;
192export function createEmitAndSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): EmitAndSemanticDiagnosticsBuilderProgram;
193export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]) {
194    return createBuilderProgram(BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences));
195}
196
197export function createEmitAndSemanticDiagnosticsBuilderProgramForArkTs(
198    newProgramOrRootNames: Program | readonly string[] | undefined,
199    hostOrOptions: BuilderProgramHost | CompilerOptions | undefined,
200    oldProgramOrHost?: CompilerHost | EmitAndSemanticDiagnosticsBuilderProgram,
201    configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | EmitAndSemanticDiagnosticsBuilderProgram,
202    configFileParsingDiagnostics?: readonly Diagnostic[],
203    projectReferences?: readonly ProjectReference[]) : EmitAndSemanticDiagnosticsBuilderProgram {
204    PerformanceDotting.start("createEmitAndSemanticDiagnosticsBuilderProgramForArkTs");
205    PerformanceDotting.start("getBuilderCreationParameters");
206    let builderCreatetionParameters: BuilderCreationParameters =
207        getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost,
208            configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences);
209    PerformanceDotting.stop("getBuilderCreationParameters");
210    let newProgram = builderCreatetionParameters.newProgram;
211    PerformanceDotting.start("createBuilderProgram");
212    let builderProgram: EmitAndSemanticDiagnosticsBuilderProgram = createBuilderProgram(
213    BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, builderCreatetionParameters);
214    PerformanceDotting.stop("createBuilderProgram");
215
216    let oldProgramForLinter =
217        readBuilderProgram(hostOrOptions as CompilerOptions, oldProgramOrHost as CompilerHost, true);
218    builderCreatetionParameters.oldProgram = oldProgramForLinter;
219    builderCreatetionParameters.newProgram = newProgram;
220    PerformanceDotting.start("createBuilderProgramForLinter");
221    let builderProgramForLinter: EmitAndSemanticDiagnosticsBuilderProgram = createBuilderProgram(
222    BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, builderCreatetionParameters, true);
223    PerformanceDotting.stop("createBuilderProgramForLinter");
224
225    builderProgram.builderProgramForLinter = builderProgramForLinter;
226    PerformanceDotting.stop("createEmitAndSemanticDiagnosticsBuilderProgramForArkTs");
227    return builderProgram;
228}
229
230/**
231 * Creates a builder thats just abstraction over program and can be used with watch
232 */
233export function createAbstractBuilder(newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): BuilderProgram;
234export function createAbstractBuilder(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram;
235export 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 {
236    const { newProgram, configFileParsingDiagnostics: newConfigFileParsingDiagnostics } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences);
237    return createRedirectedBuilderProgram(() => ({ program: newProgram, compilerOptions: newProgram.getCompilerOptions() }), newConfigFileParsingDiagnostics);
238}
239