Skip to content

Commit a77a79f

Browse files
authored
Remove some unnecessary createGetCanonicalFileName by exposing one from program (#51796)
1 parent 6684e3d commit a77a79f

File tree

6 files changed

+19
-32
lines changed

6 files changed

+19
-32
lines changed

src/compiler/builder.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import {
4242
forEachEntry,
4343
forEachKey,
4444
generateDjb2Hash,
45-
GetCanonicalFileName,
4645
getDirectoryPath,
4746
getEmitDeclarations,
4847
getNormalizedAbsolutePath,
@@ -450,7 +449,6 @@ function getEmitSignatureFromOldSignature(options: CompilerOptions, oldOptions:
450449
function convertToDiagnostics(diagnostics: readonly ReusableDiagnostic[], newProgram: Program): readonly Diagnostic[] {
451450
if (!diagnostics.length) return emptyArray;
452451
let buildInfoDirectory: string | undefined;
453-
let getCanonicalFileName: GetCanonicalFileName | undefined;
454452
return diagnostics.map(diagnostic => {
455453
const result: Diagnostic = convertToDiagnosticRelatedInformation(diagnostic, newProgram, toPath);
456454
result.reportsUnnecessary = diagnostic.reportsUnnecessary;
@@ -468,7 +466,7 @@ function convertToDiagnostics(diagnostics: readonly ReusableDiagnostic[], newPro
468466

469467
function toPath(path: string) {
470468
buildInfoDirectory ??= getDirectoryPath(getNormalizedAbsolutePath(getTsBuildInfoEmitOutputFilePath(newProgram.getCompilerOptions())!, newProgram.getCurrentDirectory()));
471-
return ts.toPath(path, buildInfoDirectory, getCanonicalFileName ??= createGetCanonicalFileName(newProgram.useCaseSensitiveFileNames()));
469+
return ts.toPath(path, buildInfoDirectory, newProgram.getCanonicalFileName);
472470
}
473471
}
474472

@@ -962,7 +960,6 @@ export function isProgramBundleEmitBuildInfo(info: ProgramBuildInfo): info is Pr
962960
*/
963961
function getBuildInfo(state: BuilderProgramState, bundle: BundleBuildInfo | undefined): BuildInfo {
964962
const currentDirectory = Debug.checkDefined(state.program).getCurrentDirectory();
965-
const getCanonicalFileName = createGetCanonicalFileName(state.program!.useCaseSensitiveFileNames());
966963
const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(getTsBuildInfoEmitOutputFilePath(state.compilerOptions)!, currentDirectory));
967964
// Convert the file name to Path here if we set the fileName instead to optimize multiple d.ts file emits and having to compute Canonical path
968965
const latestChangedDtsFile = state.latestChangedDtsFile ? relativeToBuildInfoEnsuringAbsolutePath(state.latestChangedDtsFile) : undefined;
@@ -1119,7 +1116,7 @@ function getBuildInfo(state: BuilderProgramState, bundle: BundleBuildInfo | unde
11191116
}
11201117

11211118
function relativeToBuildInfo(path: string) {
1122-
return ensurePathIsNonModuleName(getRelativePathFromDirectory(buildInfoDirectory, path, getCanonicalFileName));
1119+
return ensurePathIsNonModuleName(getRelativePathFromDirectory(buildInfoDirectory, path, state.program!.getCanonicalFileName));
11231120
}
11241121

11251122
function toFileId(path: Path): ProgramBuildInfoFileId {
@@ -1262,7 +1259,6 @@ export function computeSignatureWithDiagnostics(
12621259
host: HostForComputeHash,
12631260
data: WriteFileCallbackData | undefined
12641261
) {
1265-
let getCanonicalFileName: GetCanonicalFileName | undefined;
12661262
text = getTextHandlingSourceMapForSignature(text, data);
12671263
let sourceFileDirectory: string | undefined;
12681264
if (data?.diagnostics?.length) {
@@ -1288,7 +1284,7 @@ export function computeSignatureWithDiagnostics(
12881284
return `${ensurePathIsNonModuleName(getRelativePathFromDirectory(
12891285
sourceFileDirectory,
12901286
diagnostic.file.resolvedPath,
1291-
getCanonicalFileName ??= createGetCanonicalFileName(program.useCaseSensitiveFileNames())
1287+
program.getCanonicalFileName,
12921288
))}(${diagnostic.start},${diagnostic.length})`;
12931289
}
12941290
}

src/compiler/builderState.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
arrayFrom,
33
CancellationToken,
44
computeSignatureWithDiagnostics,
5-
createGetCanonicalFileName,
65
CustomTransformers,
76
Debug,
87
EmitOutput,
@@ -303,7 +302,6 @@ export namespace BuilderState {
303302
createManyToManyPathMap() : undefined;
304303
const exportedModulesMap = referencedMap ? createManyToManyPathMap() : undefined;
305304
const useOldState = canReuseOldState(referencedMap, oldState);
306-
const getCanonicalFileName = createGetCanonicalFileName(newProgram.useCaseSensitiveFileNames());
307305

308306
// Ensure source files have parent pointers set
309307
newProgram.getTypeChecker();
@@ -316,7 +314,7 @@ export namespace BuilderState {
316314
useOldState ? oldState!.fileInfos.get(sourceFile.resolvedPath)?.signature : undefined :
317315
oldUncommittedSignature || undefined;
318316
if (referencedMap) {
319-
const newReferences = getReferencedFiles(newProgram, sourceFile, getCanonicalFileName);
317+
const newReferences = getReferencedFiles(newProgram, sourceFile, newProgram.getCanonicalFileName);
320318
if (newReferences) {
321319
referencedMap.set(sourceFile.resolvedPath, newReferences);
322320
}

src/compiler/program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
18061806
getSymlinkCache,
18071807
realpath: host.realpath?.bind(host),
18081808
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
1809+
getCanonicalFileName,
18091810
getFileIncludeReasons: () => fileReasons,
18101811
structureIsReused,
18111812
writeFile,

src/compiler/tsbuildPublic.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
createCompilerHostFromProgramHost,
2323
createDiagnosticCollection,
2424
createDiagnosticReporter,
25-
createGetCanonicalFileName,
2625
createModuleResolutionCache,
2726
createModuleResolutionLoader,
2827
CreateProgram,
@@ -54,7 +53,6 @@ import {
5453
formatColorAndReset,
5554
getAllProjectOutputs,
5655
getBuildInfoFileVersionMap,
57-
GetCanonicalFileName,
5856
getConfigFileParsingDiagnostics,
5957
getDirectoryPath,
6058
getEntries,
@@ -370,8 +368,6 @@ interface BuildInfoCacheEntry {
370368
interface SolutionBuilderState<T extends BuilderProgram = BuilderProgram> extends WatchFactory<WatchType, ResolvedConfigFileName> {
371369
readonly host: SolutionBuilderHost<T>;
372370
readonly hostWithWatch: SolutionBuilderWithWatchHost<T>;
373-
readonly currentDirectory: string;
374-
readonly getCanonicalFileName: GetCanonicalFileName;
375371
readonly parseConfigFileHost: ParseConfigFileHost;
376372
readonly write: ((s: string) => void) | undefined;
377373

@@ -426,8 +422,6 @@ interface SolutionBuilderState<T extends BuilderProgram = BuilderProgram> extend
426422
function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, hostOrHostWithWatch: SolutionBuilderHost<T> | SolutionBuilderWithWatchHost<T>, rootNames: readonly string[], options: BuildOptions, baseWatchOptions: WatchOptions | undefined): SolutionBuilderState<T> {
427423
const host = hostOrHostWithWatch as SolutionBuilderHost<T>;
428424
const hostWithWatch = hostOrHostWithWatch as SolutionBuilderWithWatchHost<T>;
429-
const currentDirectory = host.getCurrentDirectory();
430-
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames());
431425

432426
// State of the solution
433427
const baseCompilerOptions = getCompilerOptionsOfBuildOptions(options);
@@ -441,7 +435,7 @@ function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, ho
441435
compilerHost.getModuleResolutionCache = maybeBind(host, host.getModuleResolutionCache);
442436
let moduleResolutionCache: ModuleResolutionCache | undefined, typeReferenceDirectiveResolutionCache: TypeReferenceDirectiveResolutionCache | undefined;
443437
if (!compilerHost.resolveModuleNameLiterals && !compilerHost.resolveModuleNames) {
444-
moduleResolutionCache = createModuleResolutionCache(currentDirectory, getCanonicalFileName);
438+
moduleResolutionCache = createModuleResolutionCache(compilerHost.getCurrentDirectory(), compilerHost.getCanonicalFileName);
445439
compilerHost.resolveModuleNameLiterals = (moduleNames, containingFile, redirectedReference, options, containingSourceFile) =>
446440
loadWithModeAwareCache(
447441
moduleNames,
@@ -456,7 +450,7 @@ function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, ho
456450
compilerHost.getModuleResolutionCache = () => moduleResolutionCache;
457451
}
458452
if (!compilerHost.resolveTypeReferenceDirectiveReferences && !compilerHost.resolveTypeReferenceDirectives) {
459-
typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(currentDirectory, getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache());
453+
typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(compilerHost.getCurrentDirectory(), compilerHost.getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache());
460454
compilerHost.resolveTypeReferenceDirectiveReferences = (typeDirectiveNames, containingFile, redirectedReference, options, containingSourceFile) =>
461455
loadWithModeAwareCache(
462456
typeDirectiveNames,
@@ -476,8 +470,6 @@ function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, ho
476470
const state: SolutionBuilderState<T> = {
477471
host,
478472
hostWithWatch,
479-
currentDirectory,
480-
getCanonicalFileName,
481473
parseConfigFileHost: parseConfigHostFromCompilerHostLike(host),
482474
write: maybeBind(host, host.trace),
483475

@@ -534,7 +526,7 @@ function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, ho
534526
}
535527

536528
function toPath(state: SolutionBuilderState, fileName: string) {
537-
return ts.toPath(fileName, state.currentDirectory, state.getCanonicalFileName);
529+
return ts.toPath(fileName, state.compilerHost.getCurrentDirectory(), state.compilerHost.getCanonicalFileName);
538530
}
539531

540532
function toResolvedConfigFilePath(state: SolutionBuilderState, fileName: ResolvedConfigFileName): ResolvedConfigFilePath {
@@ -583,7 +575,7 @@ function parseConfigFile(state: SolutionBuilderState, configFileName: ResolvedCo
583575
}
584576

585577
function resolveProjectName(state: SolutionBuilderState, name: string): ResolvedConfigFileName {
586-
return resolveConfigFileProjectName(resolvePath(state.currentDirectory, name));
578+
return resolveConfigFileProjectName(resolvePath(state.compilerHost.getCurrentDirectory(), name));
587579
}
588580

589581
function createBuildOrder(state: SolutionBuilderState, roots: readonly ResolvedConfigFileName[]): AnyBuildOrder {
@@ -887,7 +879,7 @@ function createUpdateOutputFileStampsProject(
887879
projectPath,
888880
buildOrder,
889881
getCompilerOptions: () => config.options,
890-
getCurrentDirectory: () => state.currentDirectory,
882+
getCurrentDirectory: () => state.compilerHost.getCurrentDirectory(),
891883
updateOutputFileStatmps: () => {
892884
updateOutputTimestamps(state, config, projectPath);
893885
updateOutputFileStampsPending = false;
@@ -935,7 +927,7 @@ function createBuildOrUpdateInvalidedProject<T extends BuilderProgram>(
935927
projectPath,
936928
buildOrder,
937929
getCompilerOptions: () => config.options,
938-
getCurrentDirectory: () => state.currentDirectory,
930+
getCurrentDirectory: () => state.compilerHost.getCurrentDirectory(),
939931
getBuilderProgram: () => withProgramOrUndefined(identity),
940932
getProgram: () =>
941933
withProgramOrUndefined(
@@ -1000,7 +992,7 @@ function createBuildOrUpdateInvalidedProject<T extends BuilderProgram>(
1000992
projectPath,
1001993
buildOrder,
1002994
getCompilerOptions: () => config.options,
1003-
getCurrentDirectory: () => state.currentDirectory,
995+
getCurrentDirectory: () => state.compilerHost.getCurrentDirectory(),
1004996
emit: (writeFile: WriteFileCallback | undefined, customTransformers: CustomTransformers | undefined) => {
1005997
if (step !== BuildStep.EmitBundle) return invalidatedProjectOfBundle;
1006998
return emitBundle(writeFile, customTransformers);
@@ -2334,7 +2326,7 @@ function watchWildCardDirectories(state: SolutionBuilderState, resolved: Resolve
23342326
fileOrDirectory,
23352327
fileOrDirectoryPath: toPath(state, fileOrDirectory),
23362328
configFileName: resolved,
2337-
currentDirectory: state.currentDirectory,
2329+
currentDirectory: state.compilerHost.getCurrentDirectory(),
23382330
options: parsed.options,
23392331
program: state.builderPrograms.get(resolvedPath) || getCachedParsedConfigFile(state, resolvedPath)?.fileNames,
23402332
useCaseSensitiveFileNames: state.parseConfigFileHost.useCaseSensitiveFileNames,
@@ -2454,7 +2446,7 @@ function createSolutionBuilderWorker<T extends BuilderProgram>(watch: boolean, h
24542446
}
24552447

24562448
function relName(state: SolutionBuilderState, path: string): string {
2457-
return convertToRelativePath(path, state.currentDirectory, f => state.getCanonicalFileName(f));
2449+
return convertToRelativePath(path, state.compilerHost.getCurrentDirectory(), state.compilerHost.getCanonicalFileName);
24582450
}
24592451

24602452
function reportStatus(state: SolutionBuilderState, message: DiagnosticMessage, ...args: string[]) {

src/compiler/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
BaseNodeFactory,
33
CreateSourceFileOptions,
44
EmitHelperFactory,
5+
GetCanonicalFileName,
56
MapLike,
67
ModeAwareCache,
78
ModeAwareCacheKey,
@@ -4529,6 +4530,7 @@ export interface Program extends ScriptReferenceHost {
45294530
isEmittedFile(file: string): boolean;
45304531
/** @internal */ getFileIncludeReasons(): MultiMap<Path, FileIncludeReason>;
45314532
/** @internal */ useCaseSensitiveFileNames(): boolean;
4533+
/** @internal */ getCanonicalFileName: GetCanonicalFileName;
45324534

45334535
getProjectReferences(): readonly ProjectReference[] | undefined;
45344536
getResolvedProjectReferences(): readonly (ResolvedProjectReference | undefined)[] | undefined;

src/compiler/watch.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,7 @@ export function listFiles<T extends BuilderProgram>(program: Program | T, write:
344344
/** @internal */
345345
export function explainFiles(program: Program, write: (s: string) => void) {
346346
const reasons = program.getFileIncludeReasons();
347-
const getCanonicalFileName = createGetCanonicalFileName(program.useCaseSensitiveFileNames());
348-
const relativeFileName = (fileName: string) => convertToRelativePath(fileName, program.getCurrentDirectory(), getCanonicalFileName);
347+
const relativeFileName = (fileName: string) => convertToRelativePath(fileName, program.getCurrentDirectory(), program.getCanonicalFileName);
349348
for (const file of program.getSourceFiles()) {
350349
write(`${toFileName(file, relativeFileName)}`);
351350
reasons.get(file.path)?.forEach(reason => write(` ${fileIncludeReasonToDiagnostics(program, reason, relativeFileName).messageText}`));
@@ -411,10 +410,9 @@ export function getMatchedFileSpec(program: Program, fileName: string) {
411410
const configFile = program.getCompilerOptions().configFile;
412411
if (!configFile?.configFileSpecs?.validatedFilesSpec) return undefined;
413412

414-
const getCanonicalFileName = createGetCanonicalFileName(program.useCaseSensitiveFileNames());
415-
const filePath = getCanonicalFileName(fileName);
413+
const filePath = program.getCanonicalFileName(fileName);
416414
const basePath = getDirectoryPath(getNormalizedAbsolutePath(configFile.fileName, program.getCurrentDirectory()));
417-
return find(configFile.configFileSpecs.validatedFilesSpec, fileSpec => getCanonicalFileName(getNormalizedAbsolutePath(fileSpec, basePath)) === filePath);
415+
return find(configFile.configFileSpecs.validatedFilesSpec, fileSpec => program.getCanonicalFileName(getNormalizedAbsolutePath(fileSpec, basePath)) === filePath);
418416
}
419417

420418
/** @internal */

0 commit comments

Comments
 (0)