Skip to content
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

Make auto-import modules host provided #16

Open
wants to merge 2 commits into
base: branch_v5.7.3_patch
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/services/codefixes/importFixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1258,8 +1258,10 @@ function getNewImportFixes(
const compilerOptions = program.getCompilerOptions();
const moduleSpecifierResolutionHost = createModuleSpecifierResolutionHost(program, host);
const getChecker = createGetChecker(program, host);
const moduleResolution = getEmitModuleResolutionKind(compilerOptions);
const rejectNodeModulesRelativePaths = moduleResolutionUsesNodeModules(moduleResolution);
// const moduleResolution = getEmitModuleResolutionKind(compilerOptions);
// deno: do not reject node_modules relative paths because
// they will be corrected later on by deno
const rejectNodeModulesRelativePaths = false; // moduleResolutionUsesNodeModules(moduleResolution);
const getModuleSpecifiers = fromCacheOnly
? (exportInfo: SymbolExportInfo | FutureSymbolExportInfo) => moduleSpecifiers.tryGetModuleSpecifiersFromCache(exportInfo.moduleSymbol, sourceFile, moduleSpecifierResolutionHost, preferences)
: (exportInfo: SymbolExportInfo | FutureSymbolExportInfo, checker: TypeChecker) => moduleSpecifiers.getModuleSpecifiersWithCacheInfo(exportInfo.moduleSymbol, checker, compilerOptions, sourceFile, moduleSpecifierResolutionHost, preferences, /*options*/ undefined, /*forAutoImport*/ true);
Expand Down Expand Up @@ -1638,7 +1640,7 @@ function getExportInfos(
originalSymbolToExportInfos.add(getUniqueSymbolId(exportedSymbol, checker).toString(), { symbol: exportedSymbol, moduleSymbol, moduleFileName: toFile?.fileName, exportKind, targetFlags: skipAlias(exportedSymbol, checker).flags, isFromPackageJson });
}
}
forEachExternalModuleToImportFrom(program, host, preferences, useAutoImportProvider, (moduleSymbol, sourceFile, program, isFromPackageJson) => {
forEachExternalModuleToImportFrom(program, host, preferences, useAutoImportProvider, fromFile, (moduleSymbol, sourceFile, program, isFromPackageJson) => {
const checker = program.getTypeChecker();
cancellationToken.throwIfCancellationRequested();

Expand Down
91 changes: 51 additions & 40 deletions src/services/exportInfoMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,13 @@ export interface ExportInfoMap {
onFileChanged(oldSourceFile: SourceFile, newSourceFile: SourceFile, typeAcquisitionEnabled: boolean): boolean;
}

/** @internal */
export interface CacheableExportInfoMapHost {
getCurrentProgram(): Program | undefined;
getPackageJsonAutoImportProvider(): Program | undefined;
getGlobalTypingsCacheLocation(): string | undefined;
}

export type ExportMapInfoKey = string & { __exportInfoKey: void; };
/** @internal */
export function createCacheableExportInfoMap(host: CacheableExportInfoMapHost): ExportInfoMap {
let exportInfoId = 1;
const exportInfo = createMultiMap<ExportMapInfoKey, CachedSymbolExportInfo>();
Expand Down Expand Up @@ -370,10 +368,10 @@ export function isImportable(
fromFile: SourceFile,
toFile: SourceFile | undefined,
toModule: Symbol,
preferences: UserPreferences,
_preferences: UserPreferences,
packageJsonFilter: PackageJsonImportFilter | undefined,
moduleSpecifierResolutionHost: ModuleSpecifierResolutionHost,
moduleSpecifierCache: ModuleSpecifierCache | undefined,
_moduleSpecifierCache: ModuleSpecifierCache | undefined,
): boolean {
if (!toFile) {
// Ambient module
Expand All @@ -389,40 +387,44 @@ export function isImportable(

Debug.assertIsDefined(toFile);
if (fromFile === toFile) return false;
const cachedResult = moduleSpecifierCache?.get(fromFile.path, toFile.path, preferences, {});
if (cachedResult?.isBlockedByPackageJsonDependencies !== undefined) {
return !cachedResult.isBlockedByPackageJsonDependencies || !!cachedResult.packageName && fileContainsPackageImport(fromFile, cachedResult.packageName);
}

const getCanonicalFileName = hostGetCanonicalFileName(moduleSpecifierResolutionHost);
const globalTypingsCache = moduleSpecifierResolutionHost.getGlobalTypingsCacheLocation?.();
const hasImportablePath = !!moduleSpecifiers.forEachFileNameOfModule(
fromFile.fileName,
toFile.fileName,
moduleSpecifierResolutionHost,
/*preferSymlinks*/ false,
toPath => {
const file = program.getSourceFile(toPath);
// Determine to import using toPath only if toPath is what we were looking at
// or there doesnt exist the file in the program by the symlink
return (file === toFile || !file) &&
isImportablePath(
fromFile.fileName,
toPath,
getCanonicalFileName,
globalTypingsCache,
moduleSpecifierResolutionHost,
);
},
);

if (packageJsonFilter) {
const importInfo = hasImportablePath ? packageJsonFilter.getSourceFileInfo(toFile, moduleSpecifierResolutionHost) : undefined;
moduleSpecifierCache?.setBlockedByPackageJsonDependencies(fromFile.path, toFile.path, preferences, {}, importInfo?.packageName, !importInfo?.importable);
return !!importInfo?.importable || hasImportablePath && !!importInfo?.packageName && fileContainsPackageImport(fromFile, importInfo.packageName);
}

return hasImportablePath;
// deno: any module provided by deno's host will always be importable
return true;

// const cachedResult = moduleSpecifierCache?.get(fromFile.path, toFile.path, preferences, {});
// if (cachedResult?.isBlockedByPackageJsonDependencies !== undefined) {
// return !cachedResult.isBlockedByPackageJsonDependencies || !!cachedResult.packageName && fileContainsPackageImport(fromFile, cachedResult.packageName);
// }

// const getCanonicalFileName = hostGetCanonicalFileName(moduleSpecifierResolutionHost);
// const globalTypingsCache = moduleSpecifierResolutionHost.getGlobalTypingsCacheLocation?.();
// const hasImportablePath = !!moduleSpecifiers.forEachFileNameOfModule(
// fromFile.fileName,
// toFile.fileName,
// moduleSpecifierResolutionHost,
// /*preferSymlinks*/ false,
// toPath => {
// const file = program.getSourceFile(toPath);
// // Determine to import using toPath only if toPath is what we were looking at
// // or there doesnt exist the file in the program by the symlink
// return (file === toFile || !file) &&
// isImportablePath(
// fromFile.fileName,
// toPath,
// getCanonicalFileName,
// globalTypingsCache,
// moduleSpecifierResolutionHost,
// );
// },
// );

// if (packageJsonFilter) {
// const importInfo = hasImportablePath ? packageJsonFilter.getSourceFileInfo(toFile, moduleSpecifierResolutionHost) : undefined;
// moduleSpecifierCache?.setBlockedByPackageJsonDependencies(fromFile.path, toFile.path, preferences, {}, importInfo?.packageName, !importInfo?.importable);
// return !!importInfo?.importable || hasImportablePath && !!importInfo?.packageName && fileContainsPackageImport(fromFile, importInfo.packageName);
// }

// return hasImportablePath;
}

function fileContainsPackageImport(sourceFile: SourceFile, packageName: string) {
Expand Down Expand Up @@ -458,17 +460,26 @@ export function forEachExternalModuleToImportFrom(
host: LanguageServiceHost,
preferences: UserPreferences,
useAutoImportProvider: boolean,
referrerFile: SourceFile | FutureSourceFile,
cb: (module: Symbol, moduleFile: SourceFile | undefined, program: Program, isFromPackageJson: boolean) => void,
): void {
const useCaseSensitiveFileNames = hostUsesCaseSensitiveFileNames(host);
const excludePatterns = preferences.autoImportFileExcludePatterns && getIsExcludedPatterns(preferences, useCaseSensitiveFileNames);

forEachExternalModule(program.getTypeChecker(), program.getSourceFiles(), excludePatterns, host, (module, file) => cb(module, file, program, /*isFromPackageJson*/ false));
const importableFilePaths = host.getExportImportablePathsFromModule(referrerFile.path);
const files = importableFilePaths
.map(path => program.getSourceFile(path))
.filter(s => s != null) as SourceFile[];

forEachExternalModule(program.getTypeChecker(), files, excludePatterns, host, (module, file) => cb(module, file, program, /*isFromPackageJson*/ false));
const autoImportProvider = useAutoImportProvider && host.getPackageJsonAutoImportProvider?.();
if (autoImportProvider) {
const start = timestamp();
const checker = program.getTypeChecker();
forEachExternalModule(autoImportProvider.getTypeChecker(), autoImportProvider.getSourceFiles(), excludePatterns, host, (module, file) => {
const files = importableFilePaths
.map(path => autoImportProvider.getSourceFile(path))
.filter(s => s != null) as SourceFile[];
forEachExternalModule(autoImportProvider.getTypeChecker(), files, excludePatterns, host, (module, file) => {
if (file && !program.getSourceFile(file.fileName) || !file && !checker.resolveName(module.name, /*location*/ undefined, SymbolFlags.Module, /*excludeGlobals*/ false)) {
// The AutoImportProvider filters files already in the main program out of its *root* files,
// but non-root files can still be present in both programs, and already in the export info map
Expand Down Expand Up @@ -554,7 +565,7 @@ export function getExportInfoMap(importingFile: SourceFile | FutureSourceFile, h
host.log?.("getExportInfoMap: cache miss or empty; calculating new results");
let moduleCount = 0;
try {
forEachExternalModuleToImportFrom(program, host, preferences, /*useAutoImportProvider*/ true, (moduleSymbol, moduleFile, program, isFromPackageJson) => {
forEachExternalModuleToImportFrom(program, host, preferences, /*useAutoImportProvider*/ true, importingFile, (moduleSymbol, moduleFile, program, isFromPackageJson) => {
if (++moduleCount % 100 === 0) cancellationToken?.throwIfCancellationRequested();
const seenExports = new Set<__String>();
const checker = program.getTypeChecker();
Expand Down
3 changes: 2 additions & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ export interface LanguageServiceHost extends GetEffectiveTypeRootsHost, MinimalR
/** @internal */ getPackageJsonsVisibleToFile?(fileName: string, rootDir?: string): readonly ProjectPackageJsonInfo[];
/** @internal */ getNearestAncestorDirectoryWithPackageJson?(fileName: string): string | undefined;
/** @internal */ getPackageJsonsForAutoImport?(rootDir?: string): readonly ProjectPackageJsonInfo[];
/** @internal */ getCachedExportInfoMap?(): ExportInfoMap;
getExportImportablePathsFromModule(filePath: string): string[];
getCachedExportInfoMap?(): ExportInfoMap;
/** @internal */ getModuleSpecifierCache?(): ModuleSpecifierCache;
/** @internal */ setCompilerHost?(host: CompilerHost): void;
/** @internal */ useSourceOfProjectReferenceRedirect?(): boolean;
Expand Down