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

This commit is contained in:
Andrew Branch 2023-01-20 11:10:41 -08:00 committed by GitHub
parent f526e16b2d
commit 19d2d9ec01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 3 deletions

View File

@ -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;
}
@ -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;
}
@ -657,7 +670,7 @@ function detectSortingWorker(importGroups: ImportDeclaration[][], preferences: U
return sortState;
}
}
return sortState;
return seenUnsortedGroup ? SortKind.None : sortState;
}
/** @internal */

View File

@ -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",
}
);