Remove some unnecessary createGetCanonicalFileName by exposing one from program (#51796)

This commit is contained in:
Sheetal Nandi
2022-12-07 09:17:52 -08:00
committed by GitHub
parent 6684e3d527
commit a77a79fe4b
6 changed files with 19 additions and 32 deletions
+3 -7
View File
@@ -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})`;
}
}
+1 -3
View File
@@ -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);
}
+1
View File
@@ -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,
+9 -17
View File
@@ -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<T extends BuilderProgram = BuilderProgram> extends WatchFactory<WatchType, ResolvedConfigFileName> {
readonly host: SolutionBuilderHost<T>;
readonly hostWithWatch: SolutionBuilderWithWatchHost<T>;
readonly currentDirectory: string;
readonly getCanonicalFileName: GetCanonicalFileName;
readonly parseConfigFileHost: ParseConfigFileHost;
readonly write: ((s: string) => void) | undefined;
@@ -426,8 +422,6 @@ interface SolutionBuilderState<T extends BuilderProgram = BuilderProgram> extend
function createSolutionBuilderState<T extends BuilderProgram>(watch: boolean, hostOrHostWithWatch: SolutionBuilderHost<T> | SolutionBuilderWithWatchHost<T>, rootNames: readonly string[], options: BuildOptions, baseWatchOptions: WatchOptions | undefined): SolutionBuilderState<T> {
const host = hostOrHostWithWatch as SolutionBuilderHost<T>;
const hostWithWatch = hostOrHostWithWatch as SolutionBuilderWithWatchHost<T>;
const currentDirectory = host.getCurrentDirectory();
const getCanonicalFileName = createGetCanonicalFileName(host.useCaseSensitiveFileNames());
// State of the solution
const baseCompilerOptions = getCompilerOptionsOfBuildOptions(options);
@@ -441,7 +435,7 @@ function createSolutionBuilderState<T extends BuilderProgram>(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<T extends BuilderProgram>(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<T extends BuilderProgram>(watch: boolean, ho
const state: SolutionBuilderState<T> = {
host,
hostWithWatch,
currentDirectory,
getCanonicalFileName,
parseConfigFileHost: parseConfigHostFromCompilerHostLike(host),
write: maybeBind(host, host.trace),
@@ -534,7 +526,7 @@ function createSolutionBuilderState<T extends BuilderProgram>(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<T extends BuilderProgram>(
projectPath,
buildOrder,
getCompilerOptions: () => config.options,
getCurrentDirectory: () => state.currentDirectory,
getCurrentDirectory: () => state.compilerHost.getCurrentDirectory(),
getBuilderProgram: () => withProgramOrUndefined(identity),
getProgram: () =>
withProgramOrUndefined(
@@ -1000,7 +992,7 @@ function createBuildOrUpdateInvalidedProject<T extends BuilderProgram>(
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<T extends BuilderProgram>(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[]) {
+2
View File
@@ -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<Path, FileIncludeReason>;
/** @internal */ useCaseSensitiveFileNames(): boolean;
/** @internal */ getCanonicalFileName: GetCanonicalFileName;
getProjectReferences(): readonly ProjectReference[] | undefined;
getResolvedProjectReferences(): readonly (ResolvedProjectReference | undefined)[] | undefined;
+3 -5
View File
@@ -344,8 +344,7 @@ export function listFiles<T extends BuilderProgram>(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 */