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