Skip to content

Commit 0b38a9a

Browse files
authored
Add support for extraFileExtensions on WatchCompilerHost (microsoft#37726)
To support typescript-eslint/typescript-eslint#1813
1 parent 0e48e68 commit 0b38a9a

10 files changed

Lines changed: 151 additions & 41 deletions

File tree

src/compiler/commandLineParser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,8 @@ namespace ts {
14631463
optionsToExtend: CompilerOptions,
14641464
host: ParseConfigFileHost,
14651465
extendedConfigCache?: Map<ExtendedConfigCacheEntry>,
1466-
watchOptionsToExtend?: WatchOptions
1466+
watchOptionsToExtend?: WatchOptions,
1467+
extraFileExtensions?: readonly FileExtensionInfo[],
14671468
): ParsedCommandLine | undefined {
14681469
const configFileText = tryReadFile(configFileName, fileName => host.readFile(fileName));
14691470
if (!isString(configFileText)) {
@@ -1483,7 +1484,7 @@ namespace ts {
14831484
optionsToExtend,
14841485
getNormalizedAbsolutePath(configFileName, cwd),
14851486
/*resolutionStack*/ undefined,
1486-
/*extraFileExtension*/ undefined,
1487+
extraFileExtensions,
14871488
extendedConfigCache,
14881489
watchOptionsToExtend
14891490
);

src/compiler/watch.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,23 +413,49 @@ namespace ts {
413413
system.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
414414
}
415415

416+
export interface CreateWatchCompilerHostInput<T extends BuilderProgram> {
417+
system: System;
418+
createProgram?: CreateProgram<T>;
419+
reportDiagnostic?: DiagnosticReporter;
420+
reportWatchStatus?: WatchStatusReporter;
421+
}
422+
423+
export interface CreateWatchCompilerHostOfConfigFileInput<T extends BuilderProgram> extends CreateWatchCompilerHostInput<T> {
424+
configFileName: string;
425+
optionsToExtend?: CompilerOptions;
426+
watchOptionsToExtend?: WatchOptions;
427+
extraFileExtensions?: readonly FileExtensionInfo[];
428+
}
416429
/**
417430
* Creates the watch compiler host from system for config file in watch mode
418431
*/
419-
export function createWatchCompilerHostOfConfigFile<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>(configFileName: string, optionsToExtend: CompilerOptions | undefined, watchOptionsToExtend: WatchOptions | undefined, system: System, createProgram?: CreateProgram<T>, reportDiagnostic?: DiagnosticReporter, reportWatchStatus?: WatchStatusReporter): WatchCompilerHostOfConfigFile<T> {
432+
export function createWatchCompilerHostOfConfigFile<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({
433+
configFileName, optionsToExtend, watchOptionsToExtend, extraFileExtensions,
434+
system, createProgram, reportDiagnostic, reportWatchStatus
435+
}: CreateWatchCompilerHostOfConfigFileInput<T>): WatchCompilerHostOfConfigFile<T> {
420436
const diagnosticReporter = reportDiagnostic || createDiagnosticReporter(system);
421437
const host = createWatchCompilerHost(system, createProgram, diagnosticReporter, reportWatchStatus) as WatchCompilerHostOfConfigFile<T>;
422438
host.onUnRecoverableConfigFileDiagnostic = diagnostic => reportUnrecoverableDiagnostic(system, diagnosticReporter, diagnostic);
423439
host.configFileName = configFileName;
424440
host.optionsToExtend = optionsToExtend;
425441
host.watchOptionsToExtend = watchOptionsToExtend;
442+
host.extraFileExtensions = extraFileExtensions;
426443
return host;
427444
}
428445

446+
export interface CreateWatchCompilerHostOfFilesAndCompilerOptionsInput<T extends BuilderProgram> extends CreateWatchCompilerHostInput<T> {
447+
rootFiles: string[];
448+
options: CompilerOptions;
449+
watchOptions: WatchOptions | undefined;
450+
projectReferences?: readonly ProjectReference[];
451+
}
429452
/**
430453
* Creates the watch compiler host from system for compiling root files and options in watch mode
431454
*/
432-
export function createWatchCompilerHostOfFilesAndCompilerOptions<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>(rootFiles: string[], options: CompilerOptions, watchOptions: WatchOptions | undefined, system: System, createProgram?: CreateProgram<T>, reportDiagnostic?: DiagnosticReporter, reportWatchStatus?: WatchStatusReporter, projectReferences?: readonly ProjectReference[]): WatchCompilerHostOfFilesAndCompilerOptions<T> {
455+
export function createWatchCompilerHostOfFilesAndCompilerOptions<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({
456+
rootFiles, options, watchOptions, projectReferences,
457+
system, createProgram, reportDiagnostic, reportWatchStatus
458+
}: CreateWatchCompilerHostOfFilesAndCompilerOptionsInput<T>): WatchCompilerHostOfFilesAndCompilerOptions<T> {
433459
const host = createWatchCompilerHost(system, createProgram, reportDiagnostic || createDiagnosticReporter(system), reportWatchStatus) as WatchCompilerHostOfFilesAndCompilerOptions<T>;
434460
host.rootFiles = rootFiles;
435461
host.options = options;

src/compiler/watchPublic.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ namespace ts {
149149

150150
watchOptionsToExtend?: WatchOptions;
151151

152+
extraFileExtensions?: readonly FileExtensionInfo[]
153+
152154
/**
153155
* Used to generate source file names from the config file and its include, exclude, files rules
154156
* and also to cache the directory stucture
@@ -191,14 +193,32 @@ namespace ts {
191193
/**
192194
* Create the watch compiler host for either configFile or fileNames and its options
193195
*/
194-
export function createWatchCompilerHost<T extends BuilderProgram>(configFileName: string, optionsToExtend: CompilerOptions | undefined, system: System, createProgram?: CreateProgram<T>, reportDiagnostic?: DiagnosticReporter, reportWatchStatus?: WatchStatusReporter, watchOptionsToExtend?: WatchOptions): WatchCompilerHostOfConfigFile<T>;
196+
export function createWatchCompilerHost<T extends BuilderProgram>(configFileName: string, optionsToExtend: CompilerOptions | undefined, system: System, createProgram?: CreateProgram<T>, reportDiagnostic?: DiagnosticReporter, reportWatchStatus?: WatchStatusReporter, watchOptionsToExtend?: WatchOptions, extraFileExtensions?: readonly FileExtensionInfo[]): WatchCompilerHostOfConfigFile<T>;
195197
export function createWatchCompilerHost<T extends BuilderProgram>(rootFiles: string[], options: CompilerOptions, system: System, createProgram?: CreateProgram<T>, reportDiagnostic?: DiagnosticReporter, reportWatchStatus?: WatchStatusReporter, projectReferences?: readonly ProjectReference[], watchOptions?: WatchOptions): WatchCompilerHostOfFilesAndCompilerOptions<T>;
196-
export function createWatchCompilerHost<T extends BuilderProgram>(rootFilesOrConfigFileName: string | string[], options: CompilerOptions | undefined, system: System, createProgram?: CreateProgram<T>, reportDiagnostic?: DiagnosticReporter, reportWatchStatus?: WatchStatusReporter, projectReferencesOrWatchOptionsToExtend?: readonly ProjectReference[] | WatchOptions, watchOptions?: WatchOptions): WatchCompilerHostOfFilesAndCompilerOptions<T> | WatchCompilerHostOfConfigFile<T> {
198+
export function createWatchCompilerHost<T extends BuilderProgram>(rootFilesOrConfigFileName: string | string[], options: CompilerOptions | undefined, system: System, createProgram?: CreateProgram<T>, reportDiagnostic?: DiagnosticReporter, reportWatchStatus?: WatchStatusReporter, projectReferencesOrWatchOptionsToExtend?: readonly ProjectReference[] | WatchOptions, watchOptionsOrExtraFileExtensions?: WatchOptions | readonly FileExtensionInfo[]): WatchCompilerHostOfFilesAndCompilerOptions<T> | WatchCompilerHostOfConfigFile<T> {
197199
if (isArray(rootFilesOrConfigFileName)) {
198-
return createWatchCompilerHostOfFilesAndCompilerOptions(rootFilesOrConfigFileName, options!, watchOptions, system, createProgram, reportDiagnostic, reportWatchStatus, projectReferencesOrWatchOptionsToExtend as readonly ProjectReference[]); // TODO: GH#18217
200+
return createWatchCompilerHostOfFilesAndCompilerOptions({
201+
rootFiles: rootFilesOrConfigFileName,
202+
options: options!,
203+
watchOptions: watchOptionsOrExtraFileExtensions as WatchOptions,
204+
projectReferences: projectReferencesOrWatchOptionsToExtend as readonly ProjectReference[],
205+
system,
206+
createProgram,
207+
reportDiagnostic,
208+
reportWatchStatus,
209+
});
199210
}
200211
else {
201-
return createWatchCompilerHostOfConfigFile(rootFilesOrConfigFileName, options, projectReferencesOrWatchOptionsToExtend as WatchOptions, system, createProgram, reportDiagnostic, reportWatchStatus);
212+
return createWatchCompilerHostOfConfigFile({
213+
configFileName: rootFilesOrConfigFileName,
214+
optionsToExtend: options,
215+
watchOptionsToExtend: projectReferencesOrWatchOptionsToExtend as WatchOptions,
216+
extraFileExtensions: watchOptionsOrExtraFileExtensions as readonly FileExtensionInfo[],
217+
system,
218+
createProgram,
219+
reportDiagnostic,
220+
reportWatchStatus,
221+
});
202222
}
203223
}
204224

@@ -237,7 +257,7 @@ namespace ts {
237257

238258
const useCaseSensitiveFileNames = host.useCaseSensitiveFileNames();
239259
const currentDirectory = host.getCurrentDirectory();
240-
const { configFileName, optionsToExtend: optionsToExtendForConfigFile = {}, watchOptionsToExtend, createProgram } = host;
260+
const { configFileName, optionsToExtend: optionsToExtendForConfigFile = {}, watchOptionsToExtend, extraFileExtensions, createProgram } = host;
241261
let { rootFiles: rootFileNames, options: compilerOptions, watchOptions, projectReferences } = host;
242262
let configFileSpecs: ConfigFileSpecs;
243263
let configFileParsingDiagnostics: Diagnostic[] | undefined;
@@ -614,7 +634,7 @@ namespace ts {
614634
}
615635

616636
function parseConfigFile() {
617-
setConfigFileParsingResult(getParsedCommandLineOfConfigFile(configFileName, optionsToExtendForConfigFile, parseConfigFileHost, /*extendedConfigCache*/ undefined, watchOptionsToExtend)!); // TODO: GH#18217
637+
setConfigFileParsingResult(getParsedCommandLineOfConfigFile(configFileName, optionsToExtendForConfigFile, parseConfigFileHost, /*extendedConfigCache*/ undefined, watchOptionsToExtend, extraFileExtensions)!); // TODO: GH#18217
618638
}
619639

620640
function setConfigFileParsingResult(configFileParseResult: ParsedCommandLine) {

src/executeCommandLine/executeCommandLine.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -566,45 +566,43 @@ namespace ts {
566566
}
567567

568568
function createWatchOfConfigFile(
569-
sys: System,
569+
system: System,
570570
cb: ExecuteCommandLineCallbacks,
571571
reportDiagnostic: DiagnosticReporter,
572572
configParseResult: ParsedCommandLine,
573573
optionsToExtend: CompilerOptions,
574574
watchOptionsToExtend: WatchOptions | undefined,
575575
) {
576-
const watchCompilerHost = createWatchCompilerHostOfConfigFile(
577-
configParseResult.options.configFilePath!,
576+
const watchCompilerHost = createWatchCompilerHostOfConfigFile({
577+
configFileName: configParseResult.options.configFilePath!,
578578
optionsToExtend,
579579
watchOptionsToExtend,
580-
sys,
581-
/*createProgram*/ undefined,
580+
system,
582581
reportDiagnostic,
583-
createWatchStatusReporter(sys, configParseResult.options)
584-
); // TODO: GH#18217
585-
updateWatchCompilationHost(sys, cb, watchCompilerHost);
582+
reportWatchStatus: createWatchStatusReporter(system, configParseResult.options)
583+
});
584+
updateWatchCompilationHost(system, cb, watchCompilerHost);
586585
watchCompilerHost.configFileParsingResult = configParseResult;
587586
return createWatchProgram(watchCompilerHost);
588587
}
589588

590589
function createWatchOfFilesAndCompilerOptions(
591-
sys: System,
590+
system: System,
592591
cb: ExecuteCommandLineCallbacks,
593592
reportDiagnostic: DiagnosticReporter,
594593
rootFiles: string[],
595594
options: CompilerOptions,
596595
watchOptions: WatchOptions | undefined,
597596
) {
598-
const watchCompilerHost = createWatchCompilerHostOfFilesAndCompilerOptions(
597+
const watchCompilerHost = createWatchCompilerHostOfFilesAndCompilerOptions({
599598
rootFiles,
600599
options,
601600
watchOptions,
602-
sys,
603-
/*createProgram*/ undefined,
601+
system,
604602
reportDiagnostic,
605-
createWatchStatusReporter(sys, options)
606-
);
607-
updateWatchCompilationHost(sys, cb, watchCompilerHost);
603+
reportWatchStatus: createWatchStatusReporter(system, options)
604+
});
605+
updateWatchCompilationHost(system, cb, watchCompilerHost);
608606
return createWatchProgram(watchCompilerHost);
609607
}
610608

src/testRunner/unittests/reuseProgramStructure.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -937,12 +937,20 @@ namespace ts {
937937
}
938938

939939
function verifyProgramWithoutConfigFile(system: System, rootFiles: string[], options: CompilerOptions) {
940-
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, /*watchOptions*/ undefined, system)).getCurrentProgram().getProgram();
940+
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions({
941+
rootFiles,
942+
options,
943+
watchOptions: undefined,
944+
system
945+
})).getCurrentProgram().getProgram();
941946
verifyProgramIsUptoDate(program, duplicate(rootFiles), duplicate(options));
942947
}
943948

944949
function verifyProgramWithConfigFile(system: System, configFileName: string) {
945-
const program = createWatchProgram(createWatchCompilerHostOfConfigFile(configFileName, {}, /*watchOptionsToExtend*/ undefined, system)).getCurrentProgram().getProgram();
950+
const program = createWatchProgram(createWatchCompilerHostOfConfigFile({
951+
configFileName,
952+
system
953+
})).getCurrentProgram().getProgram();
946954
const { fileNames, options } = parseConfigFileWithSystem(configFileName, {}, /*watchOptionsToExtend*/ undefined, system, notImplemented)!; // TODO: GH#18217
947955
verifyProgramIsUptoDate(program, fileNames, options);
948956
}
@@ -1081,7 +1089,12 @@ namespace ts {
10811089
const rootFiles = [module1.path, module2.path, module3.path];
10821090
const system = createTestSystem([module1, module2, module3]);
10831091
const options = {};
1084-
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, /*watchOptions*/ undefined, system)).getCurrentProgram().getProgram();
1092+
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions({
1093+
rootFiles,
1094+
options,
1095+
watchOptions: undefined,
1096+
system
1097+
})).getCurrentProgram().getProgram();
10851098
verifyProgramIsUptoDate(program, duplicate(rootFiles), duplicate(options));
10861099
});
10871100

@@ -1112,7 +1125,12 @@ namespace ts {
11121125
const newRootFiles = [module1.path, module2.path, module3.path];
11131126
const system = createTestSystem([module1, module2, module3]);
11141127
const options = {};
1115-
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, /*watchOptions*/ undefined, system)).getCurrentProgram().getProgram();
1128+
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions({
1129+
rootFiles,
1130+
options,
1131+
watchOptions: undefined,
1132+
system
1133+
})).getCurrentProgram().getProgram();
11161134
verifyProgramIsNotUptoDate(program, duplicate(newRootFiles), duplicate(options));
11171135
});
11181136
it("has one root file replaced by another", () => {
@@ -1132,7 +1150,12 @@ namespace ts {
11321150
const newRootFiles = [module2.path, module3.path];
11331151
const system = createTestSystem([module1, module2, module3]);
11341152
const options = {};
1135-
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, /*watchOptions*/ undefined, system)).getCurrentProgram().getProgram();
1153+
const program = createWatchProgram(createWatchCompilerHostOfFilesAndCompilerOptions({
1154+
rootFiles,
1155+
options,
1156+
watchOptions: undefined,
1157+
system
1158+
})).getCurrentProgram().getProgram();
11361159
verifyProgramIsNotUptoDate(program, duplicate(newRootFiles), duplicate(options));
11371160
});
11381161
});

src/testRunner/unittests/tscWatch/helpers.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ namespace ts.tscWatch {
3333

3434
export type Watch = WatchOfConfigFile<EmitAndSemanticDiagnosticsBuilderProgram> | WatchOfFilesAndCompilerOptions<EmitAndSemanticDiagnosticsBuilderProgram>;
3535

36-
export function createWatchOfConfigFile(configFileName: string, host: WatchedSystem, optionsToExtend?: CompilerOptions, watchOptionsToExtend?: WatchOptions) {
37-
const compilerHost = createWatchCompilerHostOfConfigFile(configFileName, optionsToExtend || {}, watchOptionsToExtend, host);
36+
export function createWatchOfConfigFile(configFileName: string, system: WatchedSystem, optionsToExtend?: CompilerOptions, watchOptionsToExtend?: WatchOptions) {
37+
const compilerHost = createWatchCompilerHostOfConfigFile({ configFileName, optionsToExtend, watchOptionsToExtend, system });
3838
return createWatchProgram(compilerHost);
3939
}
4040

41-
export function createWatchOfFilesAndCompilerOptions(rootFiles: string[], host: WatchedSystem, options: CompilerOptions = {}, watchOptions?: WatchOptions) {
42-
const compilerHost = createWatchCompilerHostOfFilesAndCompilerOptions(rootFiles, options, watchOptions, host);
41+
export function createWatchOfFilesAndCompilerOptions(rootFiles: string[], system: WatchedSystem, options: CompilerOptions = {}, watchOptions?: WatchOptions) {
42+
const compilerHost = createWatchCompilerHostOfFilesAndCompilerOptions({ rootFiles, options, watchOptions, system });
4343
return createWatchProgram(compilerHost);
4444
}
4545

src/testRunner/unittests/tscWatch/sourceOfProjectReferenceRedirect.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ namespace ts.tscWatch {
1717
solutionBuilder.close();
1818
sys.clearOutput();
1919
}
20-
const host = createWatchCompilerHostOfConfigFile(config, {}, /*watchOptionsToExtend*/ undefined, sys);
20+
const host = createWatchCompilerHostOfConfigFile({
21+
configFileName: config,
22+
system: sys
23+
});
2124
host.useSourceOfProjectReferenceRedirect = returnTrue;
2225
const watch = createWatchProgram(host);
2326
checkProgramActualFiles(watch.getCurrentProgram().getProgram(), expectedProgramFiles);

0 commit comments

Comments
 (0)