From 543c5daf9c1ec5e63013e074df9a7e488bc9a15b Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Thu, 10 May 2018 18:10:05 -0700 Subject: [PATCH] Ignore case when sorting imports --- src/harness/unittests/organizeImports.ts | 16 ++++++++++++++++ src/services/organizeImports.ts | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/harness/unittests/organizeImports.ts b/src/harness/unittests/organizeImports.ts index ebd5a7925a..1012a7a362 100644 --- a/src/harness/unittests/organizeImports.ts +++ b/src/harness/unittests/organizeImports.ts @@ -23,6 +23,15 @@ namespace ts { `import x from "./lib";`); }); + it("Sort - case-insensitive", () => { + assertSortsBefore( + `import y from "a";`, + `import x from "Z";`); + assertSortsBefore( + `import y from "A";`, + `import x from "z";`); + }); + function assertSortsBefore(importString1: string, importString2: string) { const [{moduleSpecifier: moduleSpecifier1}, {moduleSpecifier: moduleSpecifier2}] = parseImports(importString1, importString2); assert.equal(OrganizeImports.compareModuleSpecifiers(moduleSpecifier1, moduleSpecifier2), Comparison.LessThan); @@ -42,6 +51,13 @@ namespace ts { assertListEqual(actualCoalescedImports, expectedCoalescedImports); }); + it("Sort specifiers - case-insensitive", () => { + const sortedImports = parseImports(`import { default as M, a as n, B, y, Z as O } from "lib";`); + const actualCoalescedImports = OrganizeImports.coalesceImports(sortedImports); + const expectedCoalescedImports = parseImports(`import { a as n, B, default as M, y, Z as O } from "lib";`); + assertListEqual(actualCoalescedImports, expectedCoalescedImports); + }); + it("Combine side-effect-only imports", () => { const sortedImports = parseImports( `import "lib";`, diff --git a/src/services/organizeImports.ts b/src/services/organizeImports.ts index 5109b050b6..2ff99326cb 100644 --- a/src/services/organizeImports.ts +++ b/src/services/organizeImports.ts @@ -254,7 +254,7 @@ namespace ts.OrganizeImports { } function compareIdentifiers(s1: Identifier, s2: Identifier) { - return compareStringsCaseSensitive(s1.text, s2.text); + return compareStringsCaseInsensitive(s1.text, s2.text); } } @@ -277,6 +277,6 @@ namespace ts.OrganizeImports { const name2 = getExternalModuleName(m2); return compareBooleans(name1 === undefined, name2 === undefined) || compareBooleans(isExternalModuleNameRelative(name1), isExternalModuleNameRelative(name2)) || - compareStringsCaseSensitive(name1, name2); + compareStringsCaseInsensitive(name1, name2); } } \ No newline at end of file