From ef91fdd5e97e9c5ff026c13332acd6a1fca8fe59 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 13 Jan 2022 16:22:57 -0800 Subject: [PATCH 1/4] Deprioritize import paths made up of only dots and slashes --- src/services/codefixes/importFixes.ts | 5 ++++ .../importNameCodeFix_barrelExport.ts | 29 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/cases/fourslash/importNameCodeFix_barrelExport.ts diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 6284cb6775569..fe7d21d06e529 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -678,11 +678,16 @@ namespace ts.codefix { if (a.kind !== ImportFixKind.UseNamespace && b.kind !== ImportFixKind.UseNamespace) { return compareBooleans(allowsImportingSpecifier(b.moduleSpecifier), allowsImportingSpecifier(a.moduleSpecifier)) || compareNodeCoreModuleSpecifiers(a.moduleSpecifier, b.moduleSpecifier, importingFile, program) + || compareBooleans(isOnlyDotsAndSlashes(a.moduleSpecifier), isOnlyDotsAndSlashes(b.moduleSpecifier)) || compareNumberOfDirectorySeparators(a.moduleSpecifier, b.moduleSpecifier); } return Comparison.EqualTo; } + function isOnlyDotsAndSlashes(path: string) { + return !/[^.\/]/g.test(path); + } + function compareNodeCoreModuleSpecifiers(a: string, b: string, importingFile: SourceFile, program: Program): Comparison { if (startsWith(a, "node:") && !startsWith(b, "node:")) return shouldUseUriStyleNodeCoreModules(importingFile, program) ? Comparison.LessThan : Comparison.GreaterThan; if (startsWith(b, "node:") && !startsWith(a, "node:")) return shouldUseUriStyleNodeCoreModules(importingFile, program) ? Comparison.GreaterThan : Comparison.LessThan; diff --git a/tests/cases/fourslash/importNameCodeFix_barrelExport.ts b/tests/cases/fourslash/importNameCodeFix_barrelExport.ts new file mode 100644 index 0000000000000..f39640929e6ae --- /dev/null +++ b/tests/cases/fourslash/importNameCodeFix_barrelExport.ts @@ -0,0 +1,29 @@ +/// + +// @module: commonjs + +// @Filename: /foo/a.ts +//// export const A = 0; + +// @Filename: /foo/b.ts +//// export {}; +//// A/*sibling*/ + +// @Filename: /foo/index.ts +//// export * from "./a"; +//// export * from "./b"; + +// @Filename: /index.ts +//// export * from "./foo"; +//// export * from "./src"; + +// @Filename: /src/a.ts +//// export {}; +//// A/*parent*/ + +// @Filename: /src/index.ts +//// export * from "./a"; + +// Module specifiers made up of only "." and ".." components are deprioritized +verify.importFixModuleSpecifiers("sibling", ["./a", "."]); +verify.importFixModuleSpecifiers("parent", ["../foo", "../foo/a", ".."]); From 1830a49d175746364d38d80fc80d268bc0d8d5b1 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Thu, 13 Jan 2022 17:19:58 -0800 Subject: [PATCH 2/4] Fix test --- tests/cases/fourslash/importNameCodeFix_barrelExport.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cases/fourslash/importNameCodeFix_barrelExport.ts b/tests/cases/fourslash/importNameCodeFix_barrelExport.ts index f39640929e6ae..de5fe32726944 100644 --- a/tests/cases/fourslash/importNameCodeFix_barrelExport.ts +++ b/tests/cases/fourslash/importNameCodeFix_barrelExport.ts @@ -25,5 +25,5 @@ //// export * from "./a"; // Module specifiers made up of only "." and ".." components are deprioritized -verify.importFixModuleSpecifiers("sibling", ["./a", "."]); +verify.importFixModuleSpecifiers("sibling", ["./a", ".", ".."]); verify.importFixModuleSpecifiers("parent", ["../foo", "../foo/a", ".."]); From f54f3a2ebec1dd5ff1b2738969c73311964a74b9 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 14 Jan 2022 09:32:06 -0800 Subject: [PATCH 3/4] Hoist regex --- src/services/codefixes/importFixes.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index fe7d21d06e529..2390fe39efa2d 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -684,8 +684,9 @@ namespace ts.codefix { return Comparison.EqualTo; } + const notDotOrSlashPattern = /[^.\/]/g; function isOnlyDotsAndSlashes(path: string) { - return !/[^.\/]/g.test(path); + return !notDotOrSlashPattern.test(path); } function compareNodeCoreModuleSpecifiers(a: string, b: string, importingFile: SourceFile, program: Program): Comparison { From 801a3388969d29a8b79290f2b8c8dfd640c9036b Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 14 Jan 2022 09:51:44 -0800 Subject: [PATCH 4/4] Yaaaay RegExp state --- src/services/codefixes/importFixes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/codefixes/importFixes.ts b/src/services/codefixes/importFixes.ts index 2390fe39efa2d..64010a69af551 100644 --- a/src/services/codefixes/importFixes.ts +++ b/src/services/codefixes/importFixes.ts @@ -684,7 +684,7 @@ namespace ts.codefix { return Comparison.EqualTo; } - const notDotOrSlashPattern = /[^.\/]/g; + const notDotOrSlashPattern = /[^.\/]/; function isOnlyDotsAndSlashes(path: string) { return !notDotOrSlashPattern.test(path); }