mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Fix offering auto imports to paths within node modules
This commit is contained in:
1 file changed
+10
-5
@@ -587,14 +587,14 @@ namespace ts.codefix {
|
||||
cb(module);
|
||||
}
|
||||
else if (sourceFile && sourceFile !== from && isImportablePath(from.fileName, sourceFile.fileName)) {
|
||||
const moduleSpecifier = getNodeModulesSpecifierFromFileName(sourceFile.fileName);
|
||||
const moduleSpecifier = getNodeModulesPackageNameFromFileName(sourceFile.fileName);
|
||||
if (!moduleSpecifier || allowsImporting(moduleSpecifier)) {
|
||||
cb(module);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function getNodeModulesSpecifierFromFileName(importedFileName: string): string | undefined {
|
||||
function getNodeModulesPackageNameFromFileName(importedFileName: string): string | undefined {
|
||||
const specifier = moduleSpecifiers.getModuleSpecifier(
|
||||
compilerOptions,
|
||||
from,
|
||||
@@ -605,10 +605,15 @@ namespace ts.codefix {
|
||||
preferences,
|
||||
redirectTargetsMap);
|
||||
|
||||
// Relative paths here are not node_modules, so we don’t care about them;
|
||||
// Paths here are not node_modules, so we don’t care about them;
|
||||
// returning anyting will trigger a lookup in package.json.
|
||||
if (!pathIsRelative(specifier)) {
|
||||
return getPackageNameFromTypesPackageName(specifier);
|
||||
if (!pathIsRelative(specifier) && !isRootedDiskPath(specifier)) {
|
||||
const components = getPathComponents(getPackageNameFromTypesPackageName(specifier)).slice(1);
|
||||
// Scoped packages
|
||||
if (startsWith(components[0], "@")) {
|
||||
return `${components[0]}/${components[1]}`;
|
||||
}
|
||||
return components[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user