From a77a79fe4b73b8dbd46ac80d4dc0f16b2516579f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 7 Dec 2022 09:17:52 -0800 Subject: [PATCH] Remove some unnecessary createGetCanonicalFileName by exposing one from program (#51796) --- src/compiler/builder.ts | 10 +++------- src/compiler/builderState.ts | 4 +--- src/compiler/program.ts | 1 + src/compiler/tsbuildPublic.ts | 26 +++++++++----------------- src/compiler/types.ts | 2 ++ src/compiler/watch.ts | 8 +++----- 6 files changed, 19 insertions(+), 32 deletions(-) diff --git a/src/compiler/builder.ts b/src/compiler/builder.ts index 536859480e3..d4f2dc7f31f 100644 --- a/src/compiler/builder.ts +++ b/src/compiler/builder.ts @@ -42,7 +42,6 @@ import { forEachEntry, forEachKey, generateDjb2Hash, - GetCanonicalFileName, getDirectoryPath, getEmitDeclarations, getNormalizedAbsolutePath, @@ -450,7 +449,6 @@ function getEmitSignatureFromOldSignature(options: CompilerOptions, oldOptions: function convertToDiagnostics(diagnostics: readonly ReusableDiagnostic[], newProgram: Program): readonly Diagnostic[] { if (!diagnostics.length) return emptyArray; let buildInfoDirectory: string | undefined; - let getCanonicalFileName: GetCanonicalFileName | undefined; return diagnostics.map(diagnostic => { const result: Diagnostic = convertToDiagnosticRelatedInformation(diagnostic, newProgram, toPath); result.reportsUnnecessary = diagnostic.reportsUnnecessary; @@ -468,7 +466,7 @@ function convertToDiagnostics(diagnostics: readonly ReusableDiagnostic[], newPro function toPath(path: string) { buildInfoDirectory ??= getDirectoryPath(getNormalizedAbsolutePath(getTsBuildInfoEmitOutputFilePath(newProgram.getCompilerOptions())!, newProgram.getCurrentDirectory())); - return ts.toPath(path, buildInfoDirectory, getCanonicalFileName ??= createGetCanonicalFileName(newProgram.useCaseSensitiveFileNames())); + return ts.toPath(path, buildInfoDirectory, newProgram.getCanonicalFileName); } } @@ -962,7 +960,6 @@ export function isProgramBundleEmitBuildInfo(info: ProgramBuildInfo): info is Pr */ function getBuildInfo(state: BuilderProgramState, bundle: BundleBuildInfo | undefined): BuildInfo { const currentDirectory = Debug.checkDefined(state.program).getCurrentDirectory(); - const getCanonicalFileName = createGetCanonicalFileName(state.program!.useCaseSensitiveFileNames()); const buildInfoDirectory = getDirectoryPath(getNormalizedAbsolutePath(getTsBuildInfoEmitOutputFilePath(state.compilerOptions)!, currentDirectory)); // 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 const latestChangedDtsFile = state.latestChangedDtsFile ? relativeToBuildInfoEnsuringAbsolutePath(state.latestChangedDtsFile) : undefined; @@ -1119,7 +1116,7 @@ function getBuildInfo(state: BuilderProgramState, bundle: BundleBuildInfo | unde } function relativeToBuildInfo(path: string) { - return ensurePathIsNonModuleName(getRelativePathFromDirectory(buildInfoDirectory, path, getCanonicalFileName)); + return ensurePathIsNonModuleName(getRelativePathFromDirectory(buildInfoDirectory, path, state.program!.getCanonicalFileName)); } function toFileId(path: Path): ProgramBuildInfoFileId { @@ -1262,7 +1259,6 @@ export function computeSignatureWithDiagnostics( host: HostForComputeHash, data: WriteFileCallbackData | undefined ) { - let getCanonicalFileName: GetCanonicalFileName | undefined; text = getTextHandlingSourceMapForSignature(text, data); let sourceFileDirectory: string | undefined; if (data?.diagnostics?.length) { @@ -1288,7 +1284,7 @@ export function computeSignatureWithDiagnostics( return `${ensurePathIsNonModuleName(getRelativePathFromDirectory( sourceFileDirectory, diagnostic.file.resolvedPath, - getCanonicalFileName ??= createGetCanonicalFileName(program.useCaseSensitiveFileNames()) + program.getCanonicalFileName, ))}(${diagnostic.start},${diagnostic.length})`; } } diff --git a/src/compiler/builderState.ts b/src/compiler/builderState.ts index 5e4ac326dec..870e82f2f2d 100644 --- a/src/compiler/builderState.ts +++ b/src/compiler/builderState.ts @@ -2,7 +2,6 @@ import { arrayFrom, CancellationToken, computeSignatureWithDiagnostics, - createGetCanonicalFileName, CustomTransformers, Debug, EmitOutput, @@ -303,7 +302,6 @@ export namespace BuilderState { createManyToManyPathMap() : undefined; const exportedModulesMap = referencedMap ? createManyToManyPathMap() : undefined; const useOldState = canReuseOldState(referencedMap, oldState); - const getCanonicalFileName = createGetCanonicalFileName(newProgram.useCaseSensitiveFileNames()); // Ensure source files have parent pointers set newProgram.getTypeChecker(); @@ -316,7 +314,7 @@ export namespace BuilderState { useOldState ? oldState!.fileInfos.get(sourceFile.resolvedPath)?.signature : undefined : oldUncommittedSignature || undefined; if (referencedMap) { - const newReferences = getReferencedFiles(newProgram, sourceFile, getCanonicalFileName); + const newReferences = getReferencedFiles(newProgram, sourceFile, newProgram.getCanonicalFileName); if (newReferences) { referencedMap.set(sourceFile.resolvedPath, newReferences); } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 45e6c8df8dd..a9616e8edce 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1806,6 +1806,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg getSymlinkCache, realpath: host.realpath?.bind(host), useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(), + getCanonicalFileName, getFileIncludeReasons: () => fileReasons, structureIsReused, writeFile, diff --git a/src/compiler/tsbuildPublic.ts b/src/compiler/tsbuildPublic.ts index f2b4ccae01b..d67b8bb3f2c 100644 --- a/src/compiler/tsbuildPublic.ts +++ b/src/compiler/tsbuildPublic.ts @@ -22,7 +22,6 @@ import { createCompilerHostFromProgramHost, createDiagnosticCollection, createDiagnosticReporter, - createGetCanonicalFileName, createModuleResolutionCache, createModuleResolutionLoader, CreateProgram, @@ -54,7 +53,6 @@ import { formatColorAndReset, getAllProjectOutputs, getBuildInfoFileVersionMap, - GetCanonicalFileName, getConfigFileParsingDiagnostics, getDirectoryPath, getEntries, @@ -370,8 +368,6 @@ interface BuildInfoCacheEntry { interface SolutionBuilderState extends WatchFactory { readonly host: SolutionBuilderHost; readonly hostWithWatch: SolutionBuilderWithWatchHost; - readonly currentDirectory: string; - readonly getCanonicalFileName: GetCanonicalFileName; readonly parseConfigFileHost: ParseConfigFileHost; readonly write: ((s: string) => void) | undefined; @@ -426,8 +422,6 @@ interface SolutionBuilderState extend function createSolutionBuilderState(watch: boolean, hostOrHostWithWatch: SolutionBuilderHost | SolutionBuilderWithWatchHost, rootNames: readonly string[], options: BuildOptions, baseWatchOptions: WatchOptions | undefined): SolutionBuilderState { const host = hostOrHostWithWatch as SolutionBuilderHost; const hostWithWatch = hostOrHostWithWatch as SolutionBuilderWithWatchHost; - const currentDirectory = host.getCurrentDirectory(); - const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames()); // State of the solution const baseCompilerOptions = getCompilerOptionsOfBuildOptions(options); @@ -441,7 +435,7 @@ function createSolutionBuilderState(watch: boolean, ho compilerHost.getModuleResolutionCache = maybeBind(host, host.getModuleResolutionCache); let moduleResolutionCache: ModuleResolutionCache | undefined, typeReferenceDirectiveResolutionCache: TypeReferenceDirectiveResolutionCache | undefined; if (!compilerHost.resolveModuleNameLiterals && !compilerHost.resolveModuleNames) { - moduleResolutionCache = createModuleResolutionCache(currentDirectory, getCanonicalFileName); + moduleResolutionCache = createModuleResolutionCache(compilerHost.getCurrentDirectory(), compilerHost.getCanonicalFileName); compilerHost.resolveModuleNameLiterals = (moduleNames, containingFile, redirectedReference, options, containingSourceFile) => loadWithModeAwareCache( moduleNames, @@ -456,7 +450,7 @@ function createSolutionBuilderState(watch: boolean, ho compilerHost.getModuleResolutionCache = () => moduleResolutionCache; } if (!compilerHost.resolveTypeReferenceDirectiveReferences && !compilerHost.resolveTypeReferenceDirectives) { - typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(currentDirectory, getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache()); + typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache(compilerHost.getCurrentDirectory(), compilerHost.getCanonicalFileName, /*options*/ undefined, moduleResolutionCache?.getPackageJsonInfoCache()); compilerHost.resolveTypeReferenceDirectiveReferences = (typeDirectiveNames, containingFile, redirectedReference, options, containingSourceFile) => loadWithModeAwareCache( typeDirectiveNames, @@ -476,8 +470,6 @@ function createSolutionBuilderState(watch: boolean, ho const state: SolutionBuilderState = { host, hostWithWatch, - currentDirectory, - getCanonicalFileName, parseConfigFileHost: parseConfigHostFromCompilerHostLike(host), write: maybeBind(host, host.trace), @@ -534,7 +526,7 @@ function createSolutionBuilderState(watch: boolean, ho } function toPath(state: SolutionBuilderState, fileName: string) { - return ts.toPath(fileName, state.currentDirectory, state.getCanonicalFileName); + return ts.toPath(fileName, state.compilerHost.getCurrentDirectory(), state.compilerHost.getCanonicalFileName); } function toResolvedConfigFilePath(state: SolutionBuilderState, fileName: ResolvedConfigFileName): ResolvedConfigFilePath { @@ -583,7 +575,7 @@ function parseConfigFile(state: SolutionBuilderState, configFileName: ResolvedCo } function resolveProjectName(state: SolutionBuilderState, name: string): ResolvedConfigFileName { - return resolveConfigFileProjectName(resolvePath(state.currentDirectory, name)); + return resolveConfigFileProjectName(resolvePath(state.compilerHost.getCurrentDirectory(), name)); } function createBuildOrder(state: SolutionBuilderState, roots: readonly ResolvedConfigFileName[]): AnyBuildOrder { @@ -887,7 +879,7 @@ function createUpdateOutputFileStampsProject( projectPath, buildOrder, getCompilerOptions: () => config.options, - getCurrentDirectory: () => state.currentDirectory, + getCurrentDirectory: () => state.compilerHost.getCurrentDirectory(), updateOutputFileStatmps: () => { updateOutputTimestamps(state, config, projectPath); updateOutputFileStampsPending = false; @@ -935,7 +927,7 @@ function createBuildOrUpdateInvalidedProject( projectPath, buildOrder, getCompilerOptions: () => config.options, - getCurrentDirectory: () => state.currentDirectory, + getCurrentDirectory: () => state.compilerHost.getCurrentDirectory(), getBuilderProgram: () => withProgramOrUndefined(identity), getProgram: () => withProgramOrUndefined( @@ -1000,7 +992,7 @@ function createBuildOrUpdateInvalidedProject( projectPath, buildOrder, getCompilerOptions: () => config.options, - getCurrentDirectory: () => state.currentDirectory, + getCurrentDirectory: () => state.compilerHost.getCurrentDirectory(), emit: (writeFile: WriteFileCallback | undefined, customTransformers: CustomTransformers | undefined) => { if (step !== BuildStep.EmitBundle) return invalidatedProjectOfBundle; return emitBundle(writeFile, customTransformers); @@ -2334,7 +2326,7 @@ function watchWildCardDirectories(state: SolutionBuilderState, resolved: Resolve fileOrDirectory, fileOrDirectoryPath: toPath(state, fileOrDirectory), configFileName: resolved, - currentDirectory: state.currentDirectory, + currentDirectory: state.compilerHost.getCurrentDirectory(), options: parsed.options, program: state.builderPrograms.get(resolvedPath) || getCachedParsedConfigFile(state, resolvedPath)?.fileNames, useCaseSensitiveFileNames: state.parseConfigFileHost.useCaseSensitiveFileNames, @@ -2454,7 +2446,7 @@ function createSolutionBuilderWorker(watch: boolean, h } function relName(state: SolutionBuilderState, path: string): string { - return convertToRelativePath(path, state.currentDirectory, f => state.getCanonicalFileName(f)); + return convertToRelativePath(path, state.compilerHost.getCurrentDirectory(), state.compilerHost.getCanonicalFileName); } function reportStatus(state: SolutionBuilderState, message: DiagnosticMessage, ...args: string[]) { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 98571662279..f8b0f5ef6de 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2,6 +2,7 @@ import { BaseNodeFactory, CreateSourceFileOptions, EmitHelperFactory, + GetCanonicalFileName, MapLike, ModeAwareCache, ModeAwareCacheKey, @@ -4529,6 +4530,7 @@ export interface Program extends ScriptReferenceHost { isEmittedFile(file: string): boolean; /** @internal */ getFileIncludeReasons(): MultiMap; /** @internal */ useCaseSensitiveFileNames(): boolean; + /** @internal */ getCanonicalFileName: GetCanonicalFileName; getProjectReferences(): readonly ProjectReference[] | undefined; getResolvedProjectReferences(): readonly (ResolvedProjectReference | undefined)[] | undefined; diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index c0e7856a0d1..0d32009aee6 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -344,8 +344,7 @@ export function listFiles(program: Program | T, write: /** @internal */ export function explainFiles(program: Program, write: (s: string) => void) { const reasons = program.getFileIncludeReasons(); - const getCanonicalFileName = createGetCanonicalFileName(program.useCaseSensitiveFileNames()); - const relativeFileName = (fileName: string) => convertToRelativePath(fileName, program.getCurrentDirectory(), getCanonicalFileName); + const relativeFileName = (fileName: string) => convertToRelativePath(fileName, program.getCurrentDirectory(), program.getCanonicalFileName); for (const file of program.getSourceFiles()) { write(`${toFileName(file, relativeFileName)}`); reasons.get(file.path)?.forEach(reason => write(` ${fileIncludeReasonToDiagnostics(program, reason, relativeFileName).messageText}`)); @@ -411,10 +410,9 @@ export function getMatchedFileSpec(program: Program, fileName: string) { const configFile = program.getCompilerOptions().configFile; if (!configFile?.configFileSpecs?.validatedFilesSpec) return undefined; - const getCanonicalFileName = createGetCanonicalFileName(program.useCaseSensitiveFileNames()); - const filePath = getCanonicalFileName(fileName); + const filePath = program.getCanonicalFileName(fileName); const basePath = getDirectoryPath(getNormalizedAbsolutePath(configFile.fileName, program.getCurrentDirectory())); - return find(configFile.configFileSpecs.validatedFilesSpec, fileSpec => getCanonicalFileName(getNormalizedAbsolutePath(fileSpec, basePath)) === filePath); + return find(configFile.configFileSpecs.validatedFilesSpec, fileSpec => program.getCanonicalFileName(getNormalizedAbsolutePath(fileSpec, basePath)) === filePath); } /** @internal */