Skip to content

Commit 28c2084

Browse files
authored
Deprioritize import paths made up of only dots and slashes (#47432)
* Deprioritize import paths made up of only dots and slashes * Fix test * Hoist regex * Yaaaay RegExp state
1 parent 16bbddb commit 28c2084

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/services/codefixes/importFixes.ts

+6
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,17 @@ namespace ts.codefix {
678678
if (a.kind !== ImportFixKind.UseNamespace && b.kind !== ImportFixKind.UseNamespace) {
679679
return compareBooleans(allowsImportingSpecifier(b.moduleSpecifier), allowsImportingSpecifier(a.moduleSpecifier))
680680
|| compareNodeCoreModuleSpecifiers(a.moduleSpecifier, b.moduleSpecifier, importingFile, program)
681+
|| compareBooleans(isOnlyDotsAndSlashes(a.moduleSpecifier), isOnlyDotsAndSlashes(b.moduleSpecifier))
681682
|| compareNumberOfDirectorySeparators(a.moduleSpecifier, b.moduleSpecifier);
682683
}
683684
return Comparison.EqualTo;
684685
}
685686

687+
const notDotOrSlashPattern = /[^.\/]/;
688+
function isOnlyDotsAndSlashes(path: string) {
689+
return !notDotOrSlashPattern.test(path);
690+
}
691+
686692
function compareNodeCoreModuleSpecifiers(a: string, b: string, importingFile: SourceFile, program: Program): Comparison {
687693
if (startsWith(a, "node:") && !startsWith(b, "node:")) return shouldUseUriStyleNodeCoreModules(importingFile, program) ? Comparison.LessThan : Comparison.GreaterThan;
688694
if (startsWith(b, "node:") && !startsWith(a, "node:")) return shouldUseUriStyleNodeCoreModules(importingFile, program) ? Comparison.GreaterThan : Comparison.LessThan;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: commonjs
4+
5+
// @Filename: /foo/a.ts
6+
//// export const A = 0;
7+
8+
// @Filename: /foo/b.ts
9+
//// export {};
10+
//// A/*sibling*/
11+
12+
// @Filename: /foo/index.ts
13+
//// export * from "./a";
14+
//// export * from "./b";
15+
16+
// @Filename: /index.ts
17+
//// export * from "./foo";
18+
//// export * from "./src";
19+
20+
// @Filename: /src/a.ts
21+
//// export {};
22+
//// A/*parent*/
23+
24+
// @Filename: /src/index.ts
25+
//// export * from "./a";
26+
27+
// Module specifiers made up of only "." and ".." components are deprioritized
28+
verify.importFixModuleSpecifiers("sibling", ["./a", ".", ".."]);
29+
verify.importFixModuleSpecifiers("parent", ["../foo", "../foo/a", ".."]);

0 commit comments

Comments
 (0)