Skip to content

Convert reuse program structure and tracing of module resolution to baseline for easier updates #51628

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/jsTyping/jsTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
removeFileExtension,
removeMinAndVersionNumbers,
some,
toFileNameLowerCase,
TypeAcquisition,
Version,
versionMajorMinor,
Expand Down Expand Up @@ -313,8 +314,8 @@ export function discoverTypings(
// packages. So that needs this dance here.
const pathComponents = getPathComponents(normalizePath(manifestPath));
const isScoped = pathComponents[pathComponents.length - 3][0] === "@";
return isScoped && pathComponents[pathComponents.length - 4].toLowerCase() === modulesDirName || // `node_modules/@foo/bar`
!isScoped && pathComponents[pathComponents.length - 3].toLowerCase() === modulesDirName; // `node_modules/foo`
return isScoped && toFileNameLowerCase(pathComponents[pathComponents.length - 4]) === modulesDirName || // `node_modules/@foo/bar`
!isScoped && toFileNameLowerCase(pathComponents[pathComponents.length - 3]) === modulesDirName; // `node_modules/foo`
});

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

const inferredTypingName = removeFileExtension(getBaseFileName(j.toLowerCase()));
const inferredTypingName = removeFileExtension(toFileNameLowerCase(getBaseFileName(j)));
const cleanedTypingName = removeMinAndVersionNumbers(inferredTypingName);
return safeList.get(cleanedTypingName);
});
Expand Down
2 changes: 1 addition & 1 deletion src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ export class LanguageServiceShimHostAdapter implements LanguageServiceHost {
if ("getTypeReferenceDirectiveResolutionsForFile" in this.shimHost) {
this.resolveTypeReferenceDirectives = (typeDirectiveNames, containingFile) => {
const typeDirectivesForFile = JSON.parse(this.shimHost.getTypeReferenceDirectiveResolutionsForFile!(containingFile)) as MapLike<ResolvedTypeReferenceDirective>; // TODO: GH#18217
return map(typeDirectiveNames as (string | FileReference)[], name => getProperty(typeDirectivesForFile, isString(name) ? name : name.fileName.toLowerCase()));
return map(typeDirectiveNames as (string | FileReference)[], name => getProperty(typeDirectivesForFile, isString(name) ? name : toFileNameLowerCase(name.fileName)));
};
}
}
Expand Down
38 changes: 2 additions & 36 deletions src/testRunner/unittests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ProgramWithSourceTexts extends ts.Program {

export interface TestCompilerHost extends ts.CompilerHost {
getTrace(): string[];
clearTrace(): void;
}

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

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 {
const file = program.getSourceFile(fileName);
assert.isTrue(file !== undefined, `cannot find file ${fileName}`);
const cache = getCache(file!);
if (expectedContent === undefined) {
assert.isTrue(cache === undefined, `expected ${caption} to be undefined`);
}
else {
assert.isTrue(cache !== undefined, `expected ${caption} to be set`);
assert.isTrue(mapEqualToCache(expectedContent, cache!, entryChecker), `contents of ${caption} did not match the expected contents.`);
}
}

/** True if the maps have the same keys and values. */
function mapEqualToCache<T>(left: Map<string, T>, right: ts.ModeAwareCache<T>, valuesAreEqual?: (left: T, right: T) => boolean): boolean {
if (left as any === right) return true; // given the type mismatch (the tests never pass a cache), this'll never be true
if (!left || !right) return false;
const someInLeftHasNoMatch = ts.forEachEntry(left, (leftValue, leftKey) => {
if (!right.has(leftKey, /*mode*/ undefined)) return true;
const rightValue = right.get(leftKey, /*mode*/ undefined)!;
return !(valuesAreEqual ? valuesAreEqual(leftValue, rightValue) : leftValue === rightValue);
});
if (someInLeftHasNoMatch) return false;
let someInRightHasNoMatch = false;
right.forEach((_, rightKey) => someInRightHasNoMatch = someInRightHasNoMatch || !left.has(rightKey));
return !someInRightHasNoMatch;
}

export function checkResolvedModulesCache(program: ts.Program, fileName: string, expectedContent: Map<string, ts.ResolvedModule | undefined> | undefined): void {
checkCache("resolved modules", program, fileName, expectedContent, f => f.resolvedModules, checkResolvedModule);
}

export function checkResolvedTypeDirectivesCache(program: ts.Program, fileName: string, expectedContent: Map<string, ts.ResolvedTypeReferenceDirective> | undefined): void {
checkCache("resolved type directives", program, fileName, expectedContent, f => f.resolvedTypeReferenceDirectiveNames, checkResolvedTypeDirective);
}

export function createResolvedModule(resolvedFileName: string, isExternalLibraryImport = false): ts.ResolvedModuleFull {
return { resolvedFileName, extension: ts.extensionFromPath(resolvedFileName), isExternalLibraryImport };
}
Expand Down
Loading