Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,14 +626,21 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
const collateCaseSensitive = getOrganizeImportsComparer(preferences, /*ignoreCase*/ false);
const collateCaseInsensitive = getOrganizeImportsComparer(preferences, /*ignoreCase*/ true);
let sortState = SortKind.Both;
let seenUnsortedGroup = false;
for (const importGroup of importGroups) {
// Check module specifiers
if (importGroup.length > 1) {
sortState &= detectSortCaseSensitivity(
const moduleSpecifierSort = detectSortCaseSensitivity(
importGroup,
i => tryCast(i.moduleSpecifier, isStringLiteral)?.text ?? "",
collateCaseSensitive,
collateCaseInsensitive);
if (moduleSpecifierSort) {
// Don't let a single unsorted group of module specifiers make the whole algorithm detect unsorted.
// If other things are sorted consistently, that's a stronger indicator than unsorted module specifiers.
sortState &= moduleSpecifierSort;
seenUnsortedGroup = true;
}
if (!sortState) {
return sortState;
}
Expand All @@ -644,7 +651,13 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
importGroup,
i => tryCast(i.importClause?.namedBindings, isNamedImports)?.elements.length! > 1);
if (declarationWithNamedImports) {
sortState &= detectImportSpecifierSorting((declarationWithNamedImports.importClause!.namedBindings as NamedImports).elements, preferences);
const namedImportSort = detectImportSpecifierSorting((declarationWithNamedImports.importClause!.namedBindings as NamedImports).elements, preferences);
if (namedImportSort) {
// Don't let a single unsorted group of named imports make the whole algorithm detect unsorted.
// If other things are sorted consistently, that's a stronger indicator than unsorted named imports.
sortState &= namedImportSort;
seenUnsortedGroup = true;
}
if (!sortState) {
return sortState;
}
Expand All @@ -657,7 +670,7 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
return sortState;
}
}
return sortState;
return seenUnsortedGroup ? SortKind.None : sortState;
}

/** @internal */
Expand Down
14 changes: 14 additions & 0 deletions tests/cases/fourslash/organizeImports17.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// <reference path="fourslash.ts" />

//// import { Both } from "module-specifiers-unsorted";
//// import { case, Insensitively, sorted } from "aardvark";

verify.organizeImports(
`import { case, Insensitively, sorted } from "aardvark";
import { Both } from "module-specifiers-unsorted";
`,
ts.OrganizeImportsMode.SortAndCombine,
{
organizeImportsIgnoreCase: "auto",
}
);