Fix offering auto imports to paths within node modules

This commit is contained in:
Andrew Branch committed 2019-07-29 10:21:33 -07:00
1 parent 341fb3a56d
commit 426c28b8fd
1 file changed
+10 -5
+10 -5
View File
@@ -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];
}
}
}