Skip to content

Commit cd04aa9

Browse files
authored
Merge pull request #11541 from Microsoft/master_11339
[Master] Fix 11339; using localeCompare cause exception in window 2012 dueto Intl
2 parents a09f00b + 99e2219 commit cd04aa9

File tree

5 files changed

+11
-12
lines changed

5 files changed

+11
-12
lines changed

src/compiler/core.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ namespace ts {
2020

2121
const createObject = Object.create;
2222

23+
// More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times.
24+
export const collator: { compare(a: string, b: string): number } = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined;
25+
2326
export function createMap<T>(template?: MapLike<T>): Map<T> {
2427
const map: Map<T> = createObject(null); // tslint:disable-line:no-null-keyword
2528

@@ -1016,7 +1019,8 @@ namespace ts {
10161019
if (a === undefined) return Comparison.LessThan;
10171020
if (b === undefined) return Comparison.GreaterThan;
10181021
if (ignoreCase) {
1019-
if (String.prototype.localeCompare) {
1022+
if (collator && String.prototype.localeCompare) {
1023+
// accent means a ≠ b, a ≠ á, a = A
10201024
const result = a.localeCompare(b, /*locales*/ undefined, { usage: "sort", sensitivity: "accent" });
10211025
return result < 0 ? Comparison.LessThan : result > 0 ? Comparison.GreaterThan : Comparison.EqualTo;
10221026
}

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ namespace Harness {
16341634

16351635
export function collateOutputs(outputFiles: Harness.Compiler.GeneratedFile[]): string {
16361636
// Collect, test, and sort the fileNames
1637-
outputFiles.sort((a, b) => cleanName(a.fileName).localeCompare(cleanName(b.fileName)));
1637+
outputFiles.sort((a, b) => ts.compareStrings(cleanName(a.fileName), cleanName(b.fileName)));
16381638

16391639
// Emit them
16401640
let result = "";

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ namespace ts.server {
967967
result.push({ name, kind, kindModifiers, sortText, replacementSpan: convertedSpan });
968968
}
969969
return result;
970-
}, []).sort((a, b) => a.name.localeCompare(b.name));
970+
}, []).sort((a, b) => ts.compareStrings(a.name, b.name));
971971
}
972972
else {
973973
return completions;

src/services/navigateTo.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ namespace ts.NavigateTo {
66
const patternMatcher = createPatternMatcher(searchValue);
77
let rawItems: RawNavigateToItem[] = [];
88

9-
// This means "compare in a case insensitive manner."
10-
const baseSensitivity: Intl.CollatorOptions = { sensitivity: "base" };
11-
129
// Search the declarations in all files and output matched NavigateToItem into array of NavigateToItem[]
1310
forEach(sourceFiles, sourceFile => {
1411
cancellationToken.throwIfCancellationRequested();
@@ -188,8 +185,8 @@ namespace ts.NavigateTo {
188185
// We first sort case insensitively. So "Aaa" will come before "bar".
189186
// Then we sort case sensitively, so "aaa" will come before "Aaa".
190187
return i1.matchKind - i2.matchKind ||
191-
i1.name.localeCompare(i2.name, undefined, baseSensitivity) ||
192-
i1.name.localeCompare(i2.name);
188+
ts.compareStringsCaseInsensitive(i1.name, i2.name) ||
189+
ts.compareStrings(i1.name, i2.name);
193190
}
194191

195192
function createNavigateToItem(rawItem: RawNavigateToItem): NavigateToItem {

src/services/navigationBar.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,8 @@ namespace ts.NavigationBar {
330330
}
331331
}
332332

333-
// More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times.
334-
const collator: { compare(a: string, b: string): number } = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined;
335333
// Intl is missing in Safari, and node 0.10 treats "a" as greater than "B".
336-
const localeCompareIsCorrect = collator && collator.compare("a", "B") < 0;
334+
const localeCompareIsCorrect = ts.collator && ts.collator.compare("a", "B") < 0;
337335
const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) {
338336
// This isn't perfect, but it passes all of our tests.
339337
for (let i = 0; i < Math.min(a.length, b.length); i++) {
@@ -344,7 +342,7 @@ namespace ts.NavigationBar {
344342
if (chA === "'" && chB === "\"") {
345343
return -1;
346344
}
347-
const cmp = chA.toLocaleLowerCase().localeCompare(chB.toLocaleLowerCase());
345+
const cmp = ts.compareStrings(chA.toLocaleLowerCase(), chB.toLocaleLowerCase());
348346
if (cmp !== 0) {
349347
return cmp;
350348
}

0 commit comments

Comments
 (0)