Skip to content

Don’t let unsorted import groups eagerly derail sort detection #52332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2023
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",
}
);