• 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
129    /**
130     * The builder that caches the semantic diagnostics for the program and handles the changed files and affected files
131     */
132    export interface SemanticDiagnosticsBuilderProgram extends BuilderProgram {
133        /**
134         * Gets the semantic diagnostics from the program for the next affected file and caches it
135         * Returns undefined if the iteration is complete
136         */
137        getSemanticDiagnosticsOfNextAffectedFile(cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<readonly Diagnostic[]>;
138    }
139
140    /**
141     * The builder that can handle the changes in program and iterate through changed file to emit the files
142     * The semantic diagnostics are cached per file and managed by clearing for the changed/affected files
143     */
144    export interface EmitAndSemanticDiagnosticsBuilderProgram extends SemanticDiagnosticsBuilderProgram {
145        /**
146         * Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete
147         * The first of writeFile if provided, writeFile of BuilderProgramHost if provided, writeFile of compiler host
148         * in that order would be used to write the files
149         */
150        emitNextAffectedFile(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult>;
151    }
152
153    /**
154     * Create the builder to manage semantic diagnostics and cache them
155     */
156    export function createSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): SemanticDiagnosticsBuilderProgram;
157    export function createSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): SemanticDiagnosticsBuilderProgram;
158    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[]) {
159        return createBuilderProgram(BuilderProgramKind.SemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences));
160    }
161
162    /**
163     * Create the builder that can handle the changes in program and iterate through changed files
164     * to emit the those files and manage semantic diagnostics cache as well
165     */
166    export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): EmitAndSemanticDiagnosticsBuilderProgram;
167    export function createEmitAndSemanticDiagnosticsBuilderProgram(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): EmitAndSemanticDiagnosticsBuilderProgram;
168    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[]) {
169        return createBuilderProgram(BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences));
170    }
171
172    /**
173     * Creates a builder thats just abstraction over program and can be used with watch
174     */
175    export function createAbstractBuilder(newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[]): BuilderProgram;
176    export function createAbstractBuilder(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram;
177    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 {
178        const { newProgram, configFileParsingDiagnostics: newConfigFileParsingDiagnostics } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences);
179        return createRedirectedBuilderProgram(() => ({ program: newProgram, compilerOptions: newProgram.getCompilerOptions() }), newConfigFileParsingDiagnostics);
180    }
181}
182