mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
Don’t let unsorted import groups eagerly derail sort detection (#52332)
This commit is contained in:
parent
f526e16b2d
commit
19d2d9ec01
@ -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 */
|
||||
|
||||
14
tests/cases/fourslash/organizeImports17.ts
Normal file
14
tests/cases/fourslash/organizeImports17.ts
Normal 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",
|
||||
}
|
||||
);
|
||||
Loading…
x
Reference in New Issue
Block a user