Skip to content

Commit 8f662a9

Browse files
committed
Extract sorting helper
1 parent 43e1edf commit 8f662a9

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/services/organizeImports.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,7 @@ namespace ts.OrganizeImports {
204204

205205
newImportSpecifiers.push(...flatMap(namedImports, i => (i.importClause.namedBindings as NamedImports).elements));
206206

207-
const sortedImportSpecifiers = stableSort(newImportSpecifiers, (s1, s2) =>
208-
compareIdentifiers(s1.propertyName || s1.name, s2.propertyName || s2.name) ||
209-
compareIdentifiers(s1.name, s2.name));
207+
const sortedImportSpecifiers = sortSpecifiers(newImportSpecifiers);
210208

211209
const importDecl = defaultImports.length > 0
212210
? defaultImports[0]
@@ -295,9 +293,7 @@ namespace ts.OrganizeImports {
295293
const newExportSpecifiers: ExportSpecifier[] = [];
296294
newExportSpecifiers.push(...flatMap(namedExports, i => (i.exportClause).elements));
297295

298-
const sortedExportSpecifiers = stableSort(newExportSpecifiers, (s1, s2) =>
299-
compareIdentifiers(s1.propertyName || s1.name, s2.propertyName || s2.name) ||
300-
compareIdentifiers(s1.name, s2.name));
296+
const sortedExportSpecifiers = sortSpecifiers(newExportSpecifiers);
301297

302298
const exportDecl = namedExports[0];
303299
coalescedExports.push(
@@ -350,6 +346,12 @@ namespace ts.OrganizeImports {
350346
importDeclaration.moduleSpecifier);
351347
}
352348

349+
function sortSpecifiers<T extends ImportOrExportSpecifier>(specifiers: ReadonlyArray<T>) {
350+
return stableSort(specifiers, (s1, s2) =>
351+
compareIdentifiers(s1.propertyName || s1.name, s2.propertyName || s2.name) ||
352+
compareIdentifiers(s1.name, s2.name));
353+
}
354+
353355
/* internal */ // Exported for testing
354356
export function compareModuleSpecifiers(m1: Expression, m2: Expression) {
355357
const name1 = getExternalModuleName(m1);

0 commit comments

Comments
 (0)