Skip to content

Commit c1427c9

Browse files
authored
Convert reuse program structure and tracing of module resolution to baseline for easier updates (microsoft#51628)
* Modify all usages of file name lower casing to use custom lower casing method * Baseline reuse program structure tests for easy update when making changes to module resolution
1 parent 842e733 commit c1427c9

31 files changed

+2716
-359
lines changed

src/jsTyping/jsTyping.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
removeFileExtension,
2727
removeMinAndVersionNumbers,
2828
some,
29+
toFileNameLowerCase,
2930
TypeAcquisition,
3031
Version,
3132
versionMajorMinor,
@@ -313,8 +314,8 @@ export function discoverTypings(
313314
// packages. So that needs this dance here.
314315
const pathComponents = getPathComponents(normalizePath(manifestPath));
315316
const isScoped = pathComponents[pathComponents.length - 3][0] === "@";
316-
return isScoped && pathComponents[pathComponents.length - 4].toLowerCase() === modulesDirName || // `node_modules/@foo/bar`
317-
!isScoped && pathComponents[pathComponents.length - 3].toLowerCase() === modulesDirName; // `node_modules/foo`
317+
return isScoped && toFileNameLowerCase(pathComponents[pathComponents.length - 4]) === modulesDirName || // `node_modules/@foo/bar`
318+
!isScoped && toFileNameLowerCase(pathComponents[pathComponents.length - 3]) === modulesDirName; // `node_modules/foo`
318319
});
319320

320321
if (log) log(`Searching for typing names in ${packagesFolderPath}; all files: ${JSON.stringify(dependencyManifestNames)}`);
@@ -361,7 +362,7 @@ export function discoverTypings(
361362
const fromFileNames = mapDefined(fileNames, j => {
362363
if (!hasJSFileExtension(j)) return undefined;
363364

364-
const inferredTypingName = removeFileExtension(getBaseFileName(j.toLowerCase()));
365+
const inferredTypingName = removeFileExtension(toFileNameLowerCase(getBaseFileName(j)));
365366
const cleanedTypingName = removeMinAndVersionNumbers(inferredTypingName);
366367
return safeList.get(cleanedTypingName);
367368
});

src/services/shims.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ export class LanguageServiceShimHostAdapter implements LanguageServiceHost {
459459
if ("getTypeReferenceDirectiveResolutionsForFile" in this.shimHost) {
460460
this.resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile) => {
461461
const typeDirectivesForFile = JSON.parse(this.shimHost.getTypeReferenceDirectiveResolutionsForFile!(containingFile)) as MapLike<ResolvedTypeReferenceDirective>; // TODO: GH#18217
462-
return map(typeDirectiveNames as (string | FileReference)[], name => getProperty(typeDirectivesForFile, isString(name) ? name : name.fileName.toLowerCase()));
462+
return map(typeDirectiveNames as (string | FileReference)[], name => getProperty(typeDirectivesForFile, isString(name) ? name : toFileNameLowerCase(name.fileName)));
463463
};
464464
}
465465
}

src/testRunner/unittests/helpers.ts

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface ProgramWithSourceTexts extends ts.Program {
2525

2626
export interface TestCompilerHost extends ts.CompilerHost {
2727
getTrace(): string[];
28+
clearTrace(): void;
2829
}
2930

3031
export class SourceText implements ts.IScriptSnapshot {
@@ -124,6 +125,7 @@ export function createTestCompilerHost(texts: readonly NamedSourceText[], target
124125
const result: TestCompilerHost = {
125126
trace: s => trace.push(s),
126127
getTrace: () => trace,
128+
clearTrace: () => trace.length = 0,
127129
getSourceFile: fileName => files.get(fileName),
128130
getDefaultLibFileName: () => "lib.d.ts",
129131
writeFile: ts.notImplemented,
@@ -176,42 +178,6 @@ export function checkResolvedTypeDirective(actual: ts.ResolvedTypeReferenceDirec
176178
return true;
177179
}
178180

179-
function checkCache<T>(caption: string, program: ts.Program, fileName: string, expectedContent: Map<string, T> | undefined, getCache: (f: ts.SourceFile) => ts.ModeAwareCache<T> | undefined, entryChecker: (expected: T, original: T) => boolean): void {
180-
const file = program.getSourceFile(fileName);
181-
assert.isTrue(file !== undefined, `cannot find file ${fileName}`);
182-
const cache = getCache(file!);
183-
if (expectedContent === undefined) {
184-
assert.isTrue(cache === undefined, `expected ${caption} to be undefined`);
185-
}
186-
else {
187-
assert.isTrue(cache !== undefined, `expected ${caption} to be set`);
188-
assert.isTrue(mapEqualToCache(expectedContent, cache!, entryChecker), `contents of ${caption} did not match the expected contents.`);
189-
}
190-
}
191-
192-
/** True if the maps have the same keys and values. */
193-
function mapEqualToCache<T>(left: Map<string, T>, right: ts.ModeAwareCache<T>, valuesAreEqual?: (left: T, right: T) => boolean): boolean {
194-
if (left as any === right) return true; // given the type mismatch (the tests never pass a cache), this'll never be true
195-
if (!left || !right) return false;
196-
const someInLeftHasNoMatch = ts.forEachEntry(left, (leftValue, leftKey) => {
197-
if (!right.has(leftKey, /*mode*/ undefined)) return true;
198-
const rightValue = right.get(leftKey, /*mode*/ undefined)!;
199-
return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue);
200-
});
201-
if (someInLeftHasNoMatch) return false;
202-
let someInRightHasNoMatch = false;
203-
right.forEach((_, rightKey) => someInRightHasNoMatch = someInRightHasNoMatch || !left.has(rightKey));
204-
return !someInRightHasNoMatch;
205-
}
206-
207-
export function checkResolvedModulesCache(program: ts.Program, fileName: string, expectedContent: Map<string, ts.ResolvedModule | undefined> | undefined): void {
208-
checkCache("resolved modules", program, fileName, expectedContent, f => f.resolvedModules, checkResolvedModule);
209-
}
210-
211-
export function checkResolvedTypeDirectivesCache(program: ts.Program, fileName: string, expectedContent: Map<string, ts.ResolvedTypeReferenceDirective> | undefined): void {
212-
checkCache("resolved type directives", program, fileName, expectedContent, f => f.resolvedTypeReferenceDirectiveNames, checkResolvedTypeDirective);
213-
}
214-
215181
export function createResolvedModule(resolvedFileName: string, isExternalLibraryImport = false): ts.ResolvedModuleFull {
216182
return { resolvedFileName, extension: ts.extensionFromPath(resolvedFileName), isExternalLibraryImport };
217183
}

0 commit comments

Comments
 (0)