Skip to content

Commit 9556d3a

Browse files
author
Andy Hanson
committed
moduleSpecifiers: Don't return a relative path to node_modules
1 parent 58f4c6f commit 9556d3a

File tree

3 files changed

+41
-23
lines changed

3 files changed

+41
-23
lines changed

src/services/codefixes/moduleSpecifiers.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@ namespace ts.moduleSpecifiers {
1616
const getCanonicalFileName = hostGetCanonicalFileName(host);
1717
const sourceDirectory = getDirectoryPath(importingSourceFile.fileName);
1818

19-
return getAllModulePaths(program, moduleSymbol.valueDeclaration.getSourceFile()).map(moduleFileName => {
20-
const global = tryGetModuleNameFromAmbientModule(moduleSymbol)
21-
|| tryGetModuleNameFromTypeRoots(compilerOptions, host, getCanonicalFileName, moduleFileName, addJsExtension)
22-
|| tryGetModuleNameAsNodeModule(compilerOptions, moduleFileName, host, getCanonicalFileName, sourceDirectory)
23-
|| rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName);
24-
if (global) {
25-
return [global];
26-
}
19+
const ambient = tryGetModuleNameFromAmbientModule(moduleSymbol);
20+
if (ambient) return [[ambient]];
21+
22+
const modulePaths = getAllModulePaths(program, moduleSymbol.valueDeclaration.getSourceFile());
2723

24+
const global = mapDefined(modulePaths, moduleFileName =>
25+
tryGetModuleNameFromTypeRoots(compilerOptions, host, getCanonicalFileName, moduleFileName, addJsExtension) ||
26+
tryGetModuleNameAsNodeModule(compilerOptions, moduleFileName, host, getCanonicalFileName, sourceDirectory) ||
27+
rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName));
28+
if (global.length) return global.map(g => [g]);
29+
30+
return modulePaths.map(moduleFileName => {
2831
const relativePath = removeExtensionAndIndexPostFix(ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), moduleResolutionKind, addJsExtension);
2932
if (!baseUrl || preferences.importModuleSpecifierPreference === "relative") {
3033
return [relativePath];
@@ -191,11 +194,12 @@ namespace ts.moduleSpecifiers {
191194
// Simplify the full file path to something that can be resolved by Node.
192195

193196
// If the module could be imported by a directory name, use that directory's name
194-
let moduleSpecifier = getDirectoryOrExtensionlessFileName(moduleFileName);
197+
const moduleSpecifier = getDirectoryOrExtensionlessFileName(moduleFileName);
195198
// Get a path that's relative to node_modules or the importing file's path
196-
moduleSpecifier = getNodeResolvablePath(moduleSpecifier);
199+
// if node_modules folder is in this folder or any of its parent folders, no need to keep it.
200+
if (!startsWith(sourceDirectory, moduleSpecifier.substring(0, parts.topLevelNodeModulesIndex))) return undefined;
197201
// If the module was found in @types, get the actual Node package name
198-
return getPackageNameFromAtTypesDirectory(moduleSpecifier);
202+
return getPackageNameFromAtTypesDirectory(moduleSpecifier.substring(parts.topLevelPackageNameIndex + 1));
199203

200204
function getDirectoryOrExtensionlessFileName(path: string): string {
201205
// If the file is the main module, it can be imported by the package name
@@ -224,17 +228,6 @@ namespace ts.moduleSpecifiers {
224228

225229
return fullModulePathWithoutExtension;
226230
}
227-
228-
function getNodeResolvablePath(path: string): string {
229-
const basePath = path.substring(0, parts.topLevelNodeModulesIndex);
230-
if (sourceDirectory.indexOf(basePath) === 0) {
231-
// if node_modules folder is in this folder or any of its parent folders, no need to keep it.
232-
return path.substring(parts.topLevelPackageNameIndex + 1);
233-
}
234-
else {
235-
return ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, path, getCanonicalFileName));
236-
}
237-
}
238231
}
239232

240233
interface NodeModulePathParts {

tests/cases/fourslash/completionsImport_tsx.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// <reference path="fourslash.ts" />
22

33
// @noLib: true
4-
// @nolib: true
54
// @jsx: preserve
65

76
// @Filename: /a.tsx
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /a/index.ts
4+
// @Symlink: /b/node_modules/a/index.d.ts
5+
// @Symlink: /c/node_modules/a/index.d.ts
6+
////export const foo: number = 0;
7+
////export type int = 0;
8+
9+
// @Filename: /b/index.ts
10+
// @Symlink: /c/node_modules/b/index.d.ts
11+
////import { int } from 'a'
12+
////export const bConst: int;
13+
14+
// @Filename: /c/whatever.ts
15+
////import * as a from "a";
16+
17+
// @Filename: /c/foo.ts
18+
////[|import { bConst } from "b";
19+
////const x: int = 0;|]
20+
21+
goTo.file("/c/foo.ts");
22+
verify.importFixAtPosition([
23+
`import { bConst } from "b";
24+
import { int } from "a";
25+
const x: int = 0;`,
26+
]);

0 commit comments

Comments
 (0)