@@ -161,7 +161,7 @@ namespace ts {
161161 } ;
162162 }
163163
164- export interface WatchHost {
164+ export interface WatchCompilerHost {
165165 /** FS system to use */
166166 system : System ;
167167
@@ -170,14 +170,18 @@ namespace ts {
170170 /** If provided, callback to invoke after every new program creation */
171171 afterProgramCreate ?( host : DirectoryStructureHost , program : Program ) : void ;
172172
173- /** Optional module name resolver */
173+ // Sub set of compiler host methods to read and generate new program
174+ useCaseSensitiveFileNames ( ) : boolean ;
175+ getNewLine ( ) : string ;
176+
177+ /** If provided this function would be used to resolve the module names, otherwise typescript's default module resolution */
174178 resolveModuleNames ?( moduleNames : string [ ] , containingFile : string , reusedNames ?: string [ ] ) : ResolvedModule [ ] ;
175179 }
176180
177181 /**
178182 * Host to create watch with root files and options
179183 */
180- export interface WatchOfFilesAndCompilerOptionsHost extends WatchHost {
184+ export interface WatchCompilerHostOfFilesAndCompilerOptions extends WatchCompilerHost {
181185 /** root files to use to generate program */
182186 rootFiles : string [ ] ;
183187
@@ -188,7 +192,7 @@ namespace ts {
188192 /**
189193 * Host to create watch with config file
190194 */
191- export interface WatchOfConfigFileHost extends WatchHost {
195+ export interface WatchCompilerHostOfConfigFile extends WatchCompilerHost {
192196 /** Name of the config file to compile */
193197 configFileName : string ;
194198
@@ -199,11 +203,11 @@ namespace ts {
199203 onConfigFileDiagnostic ( diagnostic : Diagnostic ) : void ;
200204 }
201205
202- /*@internal */
203206 /**
204207 * Host to create watch with config file that is already parsed (from tsc)
205208 */
206- export interface WatchOfConfigFileHost extends WatchHost {
209+ /*@internal */
210+ export interface WatchCompilerHostOfConfigFile extends WatchCompilerHost {
207211 rootFiles ?: string [ ] ;
208212 options ?: CompilerOptions ;
209213 optionsToExtend ?: CompilerOptions ;
@@ -234,39 +238,48 @@ namespace ts {
234238 }
235239
236240 /**
237- * Create the watched program for config file
241+ * Creates the watch compiler host that can be extended with config file or root file names and options host
238242 */
239- export function createWatchOfConfigFile ( configFileName : string , optionsToExtend ?: CompilerOptions , system = sys , reportDiagnostic ?: DiagnosticReporter ) : WatchOfConfigFile {
240- return createWatch ( {
243+ /*@internal */
244+ export function createWatchCompilerHost ( system = sys , reportDiagnostic ?: DiagnosticReporter ) : WatchCompilerHost {
245+ return {
246+ useCaseSensitiveFileNames : ( ) => system . useCaseSensitiveFileNames ,
247+ getNewLine : ( ) => system . newLine ,
241248 system,
242- afterProgramCreate : createProgramCompilerWithBuilderState ( system , reportDiagnostic ) ,
243- onConfigFileDiagnostic : reportDiagnostic || createDiagnosticReporter ( system ) ,
244- configFileName,
245- optionsToExtend
246- } ) ;
249+ afterProgramCreate : createProgramCompilerWithBuilderState ( system , reportDiagnostic )
250+ } ;
251+ }
252+
253+ /**
254+ * Create the watched program for config file
255+ */
256+ export function createWatchOfConfigFile ( configFileName : string , optionsToExtend ?: CompilerOptions , system ?: System , reportDiagnostic ?: DiagnosticReporter ) : WatchOfConfigFile {
257+ const host = createWatchCompilerHost ( system ) as WatchCompilerHostOfConfigFile ;
258+ host . onConfigFileDiagnostic = reportDiagnostic || createDiagnosticReporter ( system ) ;
259+ host . configFileName = configFileName ;
260+ host . optionsToExtend = optionsToExtend ;
261+ return createWatch ( host ) ;
247262 }
248263
249264 /**
250265 * Create the watched program for root files and compiler options
251266 */
252267 export function createWatchOfFilesAndCompilerOptions ( rootFiles : string [ ] , options : CompilerOptions , system = sys , reportDiagnostic ?: DiagnosticReporter ) : WatchOfFilesAndCompilerOptions {
253- return createWatch ( {
254- system,
255- afterProgramCreate : createProgramCompilerWithBuilderState ( system , reportDiagnostic ) ,
256- rootFiles,
257- options
258- } ) ;
268+ const host = createWatchCompilerHost ( system , reportDiagnostic ) as WatchCompilerHostOfFilesAndCompilerOptions ;
269+ host . rootFiles = rootFiles ;
270+ host . options = options ;
271+ return createWatch ( host ) ;
259272 }
260273
261274 /**
262275 * Creates the watch from the host for root files and compiler options
263276 */
264- export function createWatch ( host : WatchOfFilesAndCompilerOptionsHost ) : WatchOfFilesAndCompilerOptions ;
277+ export function createWatch ( host : WatchCompilerHostOfFilesAndCompilerOptions ) : WatchOfFilesAndCompilerOptions ;
265278 /**
266279 * Creates the watch from the host for config file
267280 */
268- export function createWatch ( host : WatchOfConfigFileHost ) : WatchOfConfigFile ;
269- export function createWatch ( host : WatchOfFilesAndCompilerOptionsHost | WatchOfConfigFileHost ) : WatchOfFilesAndCompilerOptions | WatchOfConfigFile {
281+ export function createWatch ( host : WatchCompilerHostOfConfigFile ) : WatchOfConfigFile ;
282+ export function createWatch ( host : WatchCompilerHostOfFilesAndCompilerOptions | WatchCompilerHostOfConfigFile ) : WatchOfFilesAndCompilerOptions | WatchOfConfigFile {
270283 interface HostFileInfo {
271284 version : number ;
272285 sourceFile : SourceFile ;
@@ -284,10 +297,10 @@ namespace ts {
284297 let hasChangedCompilerOptions = false ; // True if the compiler options have changed between compilations
285298 let hasChangedAutomaticTypeDirectiveNames = false ; // True if the automatic type directives have changed
286299
287- const { system, configFileName, onConfigFileDiagnostic, optionsToExtend : optionsToExtendForConfigFile = { } } = host as WatchOfConfigFileHost ;
288- const beforeProgramCreate : WatchHost [ "beforeProgramCreate" ] = host . beforeProgramCreate ? host . beforeProgramCreate . bind ( host ) : noop ;
289- const afterProgramCreate : WatchHost [ "afterProgramCreate" ] = host . afterProgramCreate ? host . afterProgramCreate . bind ( host ) : noop ;
290- let { rootFiles : rootFileNames , options : compilerOptions , configFileSpecs, configFileWildCardDirectories } = host as WatchOfConfigFileHost ;
300+ const { system, configFileName, onConfigFileDiagnostic, optionsToExtend : optionsToExtendForConfigFile = { } } = host as WatchCompilerHostOfConfigFile ;
301+ const beforeProgramCreate : WatchCompilerHost [ "beforeProgramCreate" ] = host . beforeProgramCreate ? host . beforeProgramCreate . bind ( host ) : noop ;
302+ const afterProgramCreate : WatchCompilerHost [ "afterProgramCreate" ] = host . afterProgramCreate ? host . afterProgramCreate . bind ( host ) : noop ;
303+ let { rootFiles : rootFileNames , options : compilerOptions , configFileSpecs, configFileWildCardDirectories } = host as WatchCompilerHostOfConfigFile ;
291304
292305 // From tsc we want to get already parsed result and hence check for rootFileNames
293306 const directoryStructureHost = configFileName ? createCachedDirectoryStructureHost ( system ) : system ;
@@ -296,7 +309,7 @@ namespace ts {
296309 }
297310
298311 const loggingEnabled = compilerOptions . diagnostics || compilerOptions . extendedDiagnostics ;
299- const writeLog : ( s : string ) => void = loggingEnabled ? s => { system . write ( s ) ; system . write ( system . newLine ) ; } : noop ;
312+ const writeLog : ( s : string ) => void = loggingEnabled ? s => { system . write ( s ) ; system . write ( newLine ) ; } : noop ;
300313 const watchFile = compilerOptions . extendedDiagnostics ? ts . addFileWatcherWithLogging : loggingEnabled ? ts . addFileWatcherWithOnlyTriggerLogging : ts . addFileWatcher ;
301314 const watchFilePath = compilerOptions . extendedDiagnostics ? ts . addFilePathWatcherWithLogging : ts . addFilePathWatcher ;
302315 const watchDirectoryWorker = compilerOptions . extendedDiagnostics ? ts . addDirectoryWatcherWithLogging : ts . addDirectoryWatcher ;
@@ -308,8 +321,9 @@ namespace ts {
308321 const getCurrentDirectory = memoize ( ( ) => directoryStructureHost . getCurrentDirectory ( ) ) ;
309322 const realpath = system . realpath && ( ( path : string ) => system . realpath ( path ) ) ;
310323 const getCachedDirectoryStructureHost = configFileName && ( ( ) => directoryStructureHost as CachedDirectoryStructureHost ) ;
311- const getCanonicalFileName = createGetCanonicalFileName ( system . useCaseSensitiveFileNames ) ;
312- let newLine = getNewLineCharacter ( compilerOptions , system ) ;
324+ const useCaseSensitiveFileNames = memoize ( ( ) => host . useCaseSensitiveFileNames ( ) ) ;
325+ const getCanonicalFileName = createGetCanonicalFileName ( useCaseSensitiveFileNames ( ) ) ;
326+ let newLine = updateNewLine ( ) ;
313327
314328 const compilerHost : CompilerHost & ResolutionCacheHost = {
315329 // Members for CompilerHost
@@ -319,7 +333,7 @@ namespace ts {
319333 getDefaultLibFileName : options => combinePaths ( getDefaultLibLocation ( ) , getDefaultLibFileName ( options ) ) ,
320334 writeFile : notImplemented ,
321335 getCurrentDirectory,
322- useCaseSensitiveFileNames : ( ) => system . useCaseSensitiveFileNames ,
336+ useCaseSensitiveFileNames,
323337 getCanonicalFileName,
324338 getNewLine : ( ) => newLine ,
325339 fileExists,
@@ -370,7 +384,7 @@ namespace ts {
370384 writeLog ( `Synchronizing program` ) ;
371385
372386 if ( hasChangedCompilerOptions ) {
373- newLine = getNewLineCharacter ( compilerOptions , system ) ;
387+ newLine = updateNewLine ( ) ;
374388 if ( program && changesAffectModuleResolution ( program . getCompilerOptions ( ) , compilerOptions ) ) {
375389 resolutionCache . clear ( ) ;
376390 }
@@ -423,6 +437,10 @@ namespace ts {
423437 scheduleProgramUpdate ( ) ;
424438 }
425439
440+ function updateNewLine ( ) {
441+ return getNewLineCharacter ( compilerOptions , ( ) => host . getNewLine ( ) ) ;
442+ }
443+
426444 function toPath ( fileName : string ) {
427445 return ts . toPath ( fileName , getCurrentDirectory ( ) , getCanonicalFileName ) ;
428446 }
0 commit comments