Skip to content

Commit 19d2d9e

Browse files
authored
Don’t let unsorted import groups eagerly derail sort detection (microsoft#52332)
1 parent f526e16 commit 19d2d9e

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/services/organizeImports.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -626,14 +626,21 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
626626
const collateCaseSensitive = getOrganizeImportsComparer(preferences, /*ignoreCase*/ false);
627627
const collateCaseInsensitive = getOrganizeImportsComparer(preferences, /*ignoreCase*/ true);
628628
let sortState = SortKind.Both;
629+
let seenUnsortedGroup = false;
629630
for (const importGroup of importGroups) {
630631
// Check module specifiers
631632
if (importGroup.length > 1) {
632-
sortState &= detectSortCaseSensitivity(
633+
const moduleSpecifierSort = detectSortCaseSensitivity(
633634
importGroup,
634635
i => tryCast(i.moduleSpecifier, isStringLiteral)?.text ?? "",
635636
collateCaseSensitive,
636637
collateCaseInsensitive);
638+
if (moduleSpecifierSort) {
639+
// Don't let a single unsorted group of module specifiers make the whole algorithm detect unsorted.
640+
// If other things are sorted consistently, that's a stronger indicator than unsorted module specifiers.
641+
sortState &= moduleSpecifierSort;
642+
seenUnsortedGroup = true;
643+
}
637644
if (!sortState) {
638645
return sortState;
639646
}
@@ -644,7 +651,13 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
644651
importGroup,
645652
i => tryCast(i.importClause?.namedBindings, isNamedImports)?.elements.length! > 1);
646653
if (declarationWithNamedImports) {
647-
sortState &= detectImportSpecifierSorting((declarationWithNamedImports.importClause!.namedBindings as NamedImports).elements, preferences);
654+
const namedImportSort = detectImportSpecifierSorting((declarationWithNamedImports.importClause!.namedBindings as NamedImports).elements, preferences);
655+
if (namedImportSort) {
656+
// Don't let a single unsorted group of named imports make the whole algorithm detect unsorted.
657+
// If other things are sorted consistently, that's a stronger indicator than unsorted named imports.
658+
sortState &= namedImportSort;
659+
seenUnsortedGroup = true;
660+
}
648661
if (!sortState) {
649662
return sortState;
650663
}
@@ -657,7 +670,7 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
657670
return sortState;
658671
}
659672
}
660-
return sortState;
673+
return seenUnsortedGroup ? SortKind.None : sortState;
661674
}
662675

663676
/** @internal */
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// import { Both } from "module-specifiers-unsorted";
4+
//// import { case, Insensitively, sorted } from "aardvark";
5+
6+
verify.organizeImports(
7+
`import { case, Insensitively, sorted } from "aardvark";
8+
import { Both } from "module-specifiers-unsorted";
9+
`,
10+
ts.OrganizeImportsMode.SortAndCombine,
11+
{
12+
organizeImportsIgnoreCase: "auto",
13+
}
14+
);

0 commit comments

Comments
 (0)