Skip to content

Commit e694b9e

Browse files
committed
Update the WatchCompilerHost creation
1 parent 43c2610 commit e694b9e

2 files changed

Lines changed: 31 additions & 20 deletions

File tree

src/compiler/tsc.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace ts {
2121
return <string>diagnostic.messageText;
2222
}
2323

24-
let reportDiagnostic = createDiagnosticReporter();
24+
let reportDiagnostic = createDiagnosticReporter(sys);
2525
function udpateReportDiagnostic(options: CompilerOptions) {
2626
if (options.pretty) {
2727
reportDiagnostic = createDiagnosticReporter(sys, /*pretty*/ true);
@@ -150,33 +150,28 @@ namespace ts {
150150
return sys.exit(exitStatus);
151151
}
152152

153-
function createWatchCompilerHost(): WatchCompilerHost {
154-
const watchCompilerHost = ts.createWatchCompilerHost(sys, reportDiagnostic);
153+
function updateWatchCompilationHost(watchCompilerHost: WatchCompilerHost) {
155154
const compilerWithBuilderState = watchCompilerHost.afterProgramCreate;
156155
watchCompilerHost.beforeProgramCreate = enableStatistics;
157156
watchCompilerHost.afterProgramCreate = (host, program) => {
158157
compilerWithBuilderState(host, program);
159158
reportStatistics(program);
160159
};
161-
return watchCompilerHost;
162160
}
163161

164162
function createWatchOfConfigFile(configParseResult: ParsedCommandLine, optionsToExtend: CompilerOptions) {
165-
const watchCompilerHost = createWatchCompilerHost() as WatchCompilerHostOfConfigFile;
166-
watchCompilerHost.onConfigFileDiagnostic = reportDiagnostic;
163+
const watchCompilerHost = ts.createWatchCompilerHostOfConfigFile(configParseResult.options.configFilePath, optionsToExtend, sys, reportDiagnostic);
164+
updateWatchCompilationHost(watchCompilerHost);
167165
watchCompilerHost.rootFiles = configParseResult.fileNames;
168166
watchCompilerHost.options = configParseResult.options;
169-
watchCompilerHost.configFileName = configParseResult.options.configFilePath;
170-
watchCompilerHost.optionsToExtend = optionsToExtend;
171167
watchCompilerHost.configFileSpecs = configParseResult.configFileSpecs;
172168
watchCompilerHost.configFileWildCardDirectories = configParseResult.wildcardDirectories;
173169
createWatch(watchCompilerHost);
174170
}
175171

176172
function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions) {
177-
const watchCompilerHost = createWatchCompilerHost() as WatchCompilerHostOfFilesAndCompilerOptions;
178-
watchCompilerHost.rootFiles = rootFiles;
179-
watchCompilerHost.options = options;
173+
const watchCompilerHost = ts.createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, sys, reportDiagnostic);
174+
updateWatchCompilationHost(watchCompilerHost);
180175
createWatch(watchCompilerHost);
181176
}
182177

src/compiler/watch.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace ts {
1515
* Create a function that reports error by writing to the system and handles the formating of the diagnostic
1616
*/
1717
/*@internal*/
18-
export function createDiagnosticReporter(system = sys, pretty?: boolean): DiagnosticReporter {
18+
export function createDiagnosticReporter(system: System, pretty?: boolean): DiagnosticReporter {
1919
const host: FormatDiagnosticsHost = system === sys ? sysFormatDiagnosticsHost : {
2020
getCurrentDirectory: () => system.getCurrentDirectory(),
2121
getNewLine: () => system.newLine,
@@ -266,6 +266,7 @@ namespace ts {
266266
*/
267267
/*@internal*/
268268
export interface WatchCompilerHostOfConfigFile extends WatchCompilerHost {
269+
cachedDirectoryStructureHost?: CachedDirectoryStructureHost;
269270
rootFiles?: string[];
270271
options?: CompilerOptions;
271272
optionsToExtend?: CompilerOptions;
@@ -298,8 +299,7 @@ namespace ts {
298299
/**
299300
* Creates the watch compiler host that can be extended with config file or root file names and options host
300301
*/
301-
/*@internal*/
302-
export function createWatchCompilerHost(system = sys, reportDiagnostic: DiagnosticReporter | undefined): WatchCompilerHost {
302+
function createWatchCompilerHost(system = sys, reportDiagnostic: DiagnosticReporter | undefined): WatchCompilerHost {
303303
return {
304304
useCaseSensitiveFileNames: () => system.useCaseSensitiveFileNames,
305305
getNewLine: () => system.newLine,
@@ -325,26 +325,42 @@ namespace ts {
325325
}
326326

327327
/**
328-
* Create the watched program for config file
328+
* Creates the watch compiler host from system for config file in watch mode
329329
*/
330-
export function createWatchOfConfigFile(configFileName: string, optionsToExtend?: CompilerOptions, system?: System, reportDiagnostic?: DiagnosticReporter): WatchOfConfigFile {
330+
/*@internal*/
331+
export function createWatchCompilerHostOfConfigFile(configFileName: string, optionsToExtend: CompilerOptions | undefined, system: System, reportDiagnostic: DiagnosticReporter | undefined): WatchCompilerHostOfConfigFile {
331332
reportDiagnostic = reportDiagnostic || createDiagnosticReporter(system);
332333
const host = createWatchCompilerHost(system, reportDiagnostic) as WatchCompilerHostOfConfigFile;
333334
host.onConfigFileDiagnostic = reportDiagnostic;
334335
host.onUnRecoverableConfigFileDiagnostic = diagnostic => reportUnrecoverableDiagnostic(system, reportDiagnostic, diagnostic);
335336
host.configFileName = configFileName;
336337
host.optionsToExtend = optionsToExtend;
337-
return createWatch(host);
338+
return host;
338339
}
339340

340341
/**
341-
* Create the watched program for root files and compiler options
342+
* Creates the watch compiler host from system for compiling root files and options in watch mode
342343
*/
343-
export function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions, system = sys, reportDiagnostic?: DiagnosticReporter): WatchOfFilesAndCompilerOptions {
344+
/*@internal*/
345+
export function createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions, system: System, reportDiagnostic: DiagnosticReporter | undefined): WatchCompilerHostOfFilesAndCompilerOptions {
344346
const host = createWatchCompilerHost(system, reportDiagnostic) as WatchCompilerHostOfFilesAndCompilerOptions;
345347
host.rootFiles = rootFiles;
346348
host.options = options;
347-
return createWatch(host);
349+
return host;
350+
}
351+
352+
/**
353+
* Create the watched program for config file
354+
*/
355+
export function createWatchOfConfigFile(configFileName: string, optionsToExtend?: CompilerOptions, system = sys, reportDiagnostic?: DiagnosticReporter): WatchOfConfigFile {
356+
return createWatch(createWatchCompilerHostOfConfigFile(configFileName, optionsToExtend, system, reportDiagnostic));
357+
}
358+
359+
/**
360+
* Create the watched program for root files and compiler options
361+
*/
362+
export function createWatchOfFilesAndCompilerOptions(rootFiles: string[], options: CompilerOptions, system = sys, reportDiagnostic?: DiagnosticReporter): WatchOfFilesAndCompilerOptions {
363+
return createWatch(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, system, reportDiagnostic));
348364
}
349365

350366
/**

0 commit comments

Comments
 (0)