mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-15 03:23:08 -06:00
Create api to create Watch<BuilderProgram>
This commit is contained in:
parent
dc62bb9abc
commit
9b54d2e458
@ -223,6 +223,14 @@ namespace ts {
|
||||
export function createBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram: BaseBuilderProgram | undefined, kind: BuilderProgramKind.SemanticDiagnosticsBuilderProgram): SemanticDiagnosticsBuilderProgram;
|
||||
export function createBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram: BaseBuilderProgram | undefined, kind: BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram): EmitAndSemanticDiagnosticsBuilderProgram;
|
||||
export function createBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram: BaseBuilderProgram | undefined, kind: BuilderProgramKind) {
|
||||
// Return same program if underlying program doesnt change
|
||||
let oldState = oldProgram && oldProgram.getState();
|
||||
if (oldState && newProgram === oldState.program) {
|
||||
newProgram = undefined;
|
||||
oldState = undefined;
|
||||
return oldProgram;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the canonical file name for identity
|
||||
*/
|
||||
@ -231,11 +239,12 @@ namespace ts {
|
||||
* Computing hash to for signature verification
|
||||
*/
|
||||
const computeHash = host.createHash || identity;
|
||||
const state = createBuilderProgramState(newProgram, getCanonicalFileName, oldProgram && oldProgram.getState());
|
||||
const state = createBuilderProgramState(newProgram, getCanonicalFileName, oldState);
|
||||
|
||||
// To ensure that we arent storing any references to old program or new program without state
|
||||
newProgram = undefined;
|
||||
oldProgram = undefined;
|
||||
oldState = undefined;
|
||||
|
||||
const result: BaseBuilderProgram = {
|
||||
getState: () => state,
|
||||
|
||||
@ -165,13 +165,13 @@ namespace ts {
|
||||
watchCompilerHost.options = configParseResult.options;
|
||||
watchCompilerHost.configFileSpecs = configParseResult.configFileSpecs;
|
||||
watchCompilerHost.configFileWildCardDirectories = configParseResult.wildcardDirectories;
|
||||
createWatch(watchCompilerHost);
|
||||
createWatchProgram(watchCompilerHost);
|
||||
}
|
||||
|
||||
function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions) {
|
||||
const watchCompilerHost = ts.createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, sys, reportDiagnostic);
|
||||
updateWatchCompilationHost(watchCompilerHost);
|
||||
createWatch(watchCompilerHost);
|
||||
createWatchProgram(watchCompilerHost);
|
||||
}
|
||||
|
||||
function enableStatistics(compilerOptions: CompilerOptions) {
|
||||
|
||||
@ -376,24 +376,24 @@ namespace ts {
|
||||
configFileWildCardDirectories?: MapLike<WatchDirectoryFlags>;
|
||||
}
|
||||
|
||||
export interface Watch {
|
||||
export interface Watch<T> {
|
||||
/** Synchronize with host and get updated program */
|
||||
getProgram(): Program;
|
||||
getProgram(): T;
|
||||
/** Gets the existing program without synchronizing with changes on host */
|
||||
/*@internal*/
|
||||
getExistingProgram(): Program;
|
||||
getExistingProgram(): T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the watch what generates program using the config file
|
||||
*/
|
||||
export interface WatchOfConfigFile extends Watch {
|
||||
export interface WatchOfConfigFile<T> extends Watch<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the watch that generates program using the root files and compiler options
|
||||
*/
|
||||
export interface WatchOfFilesAndCompilerOptions extends Watch {
|
||||
export interface WatchOfFilesAndCompilerOptions<T> extends Watch<T> {
|
||||
/** Updates the root files in the program, only if this is not config file compilation */
|
||||
updateRootFileNames(fileNames: string[]): void;
|
||||
}
|
||||
@ -401,26 +401,26 @@ namespace ts {
|
||||
/**
|
||||
* Create the watched program for config file
|
||||
*/
|
||||
export function createWatchOfConfigFile(configFileName: string, optionsToExtend?: CompilerOptions, system = sys, reportDiagnostic?: DiagnosticReporter): WatchOfConfigFile {
|
||||
return createWatch(createWatchCompilerHostOfConfigFile(configFileName, optionsToExtend, system, reportDiagnostic));
|
||||
export function createWatchOfConfigFile(configFileName: string, optionsToExtend?: CompilerOptions, system = sys, reportDiagnostic?: DiagnosticReporter): WatchOfConfigFile<Program> {
|
||||
return createWatchProgram(createWatchCompilerHostOfConfigFile(configFileName, optionsToExtend, system, reportDiagnostic));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the watched program for root files and compiler options
|
||||
*/
|
||||
export function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions, system = sys, reportDiagnostic?: DiagnosticReporter): WatchOfFilesAndCompilerOptions {
|
||||
return createWatch(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, system, reportDiagnostic));
|
||||
export function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions, system = sys, reportDiagnostic?: DiagnosticReporter): WatchOfFilesAndCompilerOptions<Program> {
|
||||
return createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, system, reportDiagnostic));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the watch from the host for root files and compiler options
|
||||
*/
|
||||
export function createWatch(host: WatchCompilerHostOfFilesAndCompilerOptions): WatchOfFilesAndCompilerOptions;
|
||||
export function createWatchProgram(host: WatchCompilerHostOfFilesAndCompilerOptions): WatchOfFilesAndCompilerOptions<Program>;
|
||||
/**
|
||||
* Creates the watch from the host for config file
|
||||
*/
|
||||
export function createWatch(host: WatchCompilerHostOfConfigFile): WatchOfConfigFile;
|
||||
export function createWatch(host: WatchCompilerHostOfFilesAndCompilerOptions & WatchCompilerHostOfConfigFile): WatchOfFilesAndCompilerOptions | WatchOfConfigFile {
|
||||
export function createWatchProgram(host: WatchCompilerHostOfConfigFile): WatchOfConfigFile<Program>;
|
||||
export function createWatchProgram(host: WatchCompilerHostOfFilesAndCompilerOptions & WatchCompilerHostOfConfigFile): WatchOfFilesAndCompilerOptions<Program> | WatchOfConfigFile<Program> {
|
||||
interface HostFileInfo {
|
||||
version: number;
|
||||
sourceFile: SourceFile;
|
||||
@ -890,4 +890,22 @@ namespace ts {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the watch from the host for root files and compiler options
|
||||
*/
|
||||
export function createBuilderWatchProgram<T extends BaseBuilderProgram>(host: WatchCompilerHostOfFilesAndCompilerOptions & BuilderProgramHost, createBuilderProgram: (newProgram: Program, host: BuilderProgramHost, oldProgram?: T) => T): WatchOfFilesAndCompilerOptions<T>;
|
||||
/**
|
||||
* Creates the watch from the host for config file
|
||||
*/
|
||||
export function createBuilderWatchProgram<T extends BaseBuilderProgram>(host: WatchCompilerHostOfConfigFile & BuilderProgramHost, createBuilderProgram: (newProgram: Program, host: BuilderProgramHost, oldProgram?: T) => T): WatchOfConfigFile<T>;
|
||||
export function createBuilderWatchProgram<T extends BaseBuilderProgram>(host: WatchCompilerHostOfFilesAndCompilerOptions & WatchCompilerHostOfConfigFile & BuilderProgramHost, createBuilderProgram: (newProgram: Program, host: BuilderProgramHost, oldProgram?: T) => T): WatchOfFilesAndCompilerOptions<T> | WatchOfConfigFile<T> {
|
||||
const watch = createWatchProgram(host);
|
||||
let builderProgram: T | undefined;
|
||||
return {
|
||||
getProgram: () => builderProgram = createBuilderProgram(watch.getProgram(), host, builderProgram),
|
||||
getExistingProgram: () => builderProgram = createBuilderProgram(watch.getExistingProgram(), host, builderProgram),
|
||||
updateRootFileNames: watch.updateRootFileNames && (fileNames => watch.updateRootFileNames(fileNames))
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
24
tests/baselines/reference/api/typescript.d.ts
vendored
24
tests/baselines/reference/api/typescript.d.ts
vendored
@ -3935,38 +3935,46 @@ declare namespace ts {
|
||||
*/
|
||||
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
|
||||
}
|
||||
interface Watch {
|
||||
interface Watch<T> {
|
||||
/** Synchronize with host and get updated program */
|
||||
getProgram(): Program;
|
||||
getProgram(): T;
|
||||
}
|
||||
/**
|
||||
* Creates the watch what generates program using the config file
|
||||
*/
|
||||
interface WatchOfConfigFile extends Watch {
|
||||
interface WatchOfConfigFile<T> extends Watch<T> {
|
||||
}
|
||||
/**
|
||||
* Creates the watch that generates program using the root files and compiler options
|
||||
*/
|
||||
interface WatchOfFilesAndCompilerOptions extends Watch {
|
||||
interface WatchOfFilesAndCompilerOptions<T> extends Watch<T> {
|
||||
/** Updates the root files in the program, only if this is not config file compilation */
|
||||
updateRootFileNames(fileNames: string[]): void;
|
||||
}
|
||||
/**
|
||||
* Create the watched program for config file
|
||||
*/
|
||||
function createWatchOfConfigFile(configFileName: string, optionsToExtend?: CompilerOptions, system?: System, reportDiagnostic?: DiagnosticReporter): WatchOfConfigFile;
|
||||
function createWatchOfConfigFile(configFileName: string, optionsToExtend?: CompilerOptions, system?: System, reportDiagnostic?: DiagnosticReporter): WatchOfConfigFile<Program>;
|
||||
/**
|
||||
* Create the watched program for root files and compiler options
|
||||
*/
|
||||
function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions, system?: System, reportDiagnostic?: DiagnosticReporter): WatchOfFilesAndCompilerOptions;
|
||||
function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions, system?: System, reportDiagnostic?: DiagnosticReporter): WatchOfFilesAndCompilerOptions<Program>;
|
||||
/**
|
||||
* Creates the watch from the host for root files and compiler options
|
||||
*/
|
||||
function createWatch(host: WatchCompilerHostOfFilesAndCompilerOptions): WatchOfFilesAndCompilerOptions;
|
||||
function createWatchProgram(host: WatchCompilerHostOfFilesAndCompilerOptions): WatchOfFilesAndCompilerOptions<Program>;
|
||||
/**
|
||||
* Creates the watch from the host for config file
|
||||
*/
|
||||
function createWatch(host: WatchCompilerHostOfConfigFile): WatchOfConfigFile;
|
||||
function createWatchProgram(host: WatchCompilerHostOfConfigFile): WatchOfConfigFile<Program>;
|
||||
/**
|
||||
* Creates the watch from the host for root files and compiler options
|
||||
*/
|
||||
function createBuilderWatchProgram<T extends BaseBuilderProgram>(host: WatchCompilerHostOfFilesAndCompilerOptions & BuilderProgramHost, createBuilderProgram: (newProgram: Program, host: BuilderProgramHost, oldProgram?: T) => T): WatchOfFilesAndCompilerOptions<T>;
|
||||
/**
|
||||
* Creates the watch from the host for config file
|
||||
*/
|
||||
function createBuilderWatchProgram<T extends BaseBuilderProgram>(host: WatchCompilerHostOfConfigFile & BuilderProgramHost, createBuilderProgram: (newProgram: Program, host: BuilderProgramHost, oldProgram?: T) => T): WatchOfConfigFile<T>;
|
||||
}
|
||||
declare namespace ts {
|
||||
function parseCommandLine(commandLine: ReadonlyArray<string>, readFile?: (path: string) => string | undefined): ParsedCommandLine;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user