Skip to content

Commit dfa1ffe

Browse files
committed
Cleanup and reordering
1 parent c0ed26e commit dfa1ffe

6 files changed

Lines changed: 43 additions & 59 deletions

File tree

src/compiler/core.ts

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,16 @@ namespace ts {
14901490
return a === b;
14911491
}
14921492

1493+
/**
1494+
* Compare equality between two strings using an ordinal comparison.
1495+
*
1496+
* Case-insensitive comparisons compare both strings after applying `toUpperCase` to
1497+
* each string.
1498+
*/
1499+
export function equateStrings(a: string, b: string, ignoreCase: boolean) {
1500+
return ignoreCase ? equateStringsCaseInsensitive(a, b) : equateStringsCaseSensitive(a, b);
1501+
}
1502+
14931503
export function equateStringsCaseInsensitive(a: string, b: string) {
14941504
return a === b
14951505
|| a !== undefined
@@ -1501,16 +1511,6 @@ namespace ts {
15011511
return equateValues(a, b);
15021512
}
15031513

1504-
/**
1505-
* Compare equality between two strings using an ordinal comparison.
1506-
*
1507-
* Case-insensitive comparisons compare both strings after applying `toUpperCase` to
1508-
* each string.
1509-
*/
1510-
export function equateStrings(a: string, b: string, ignoreCase: boolean) {
1511-
return ignoreCase ? equateStringsCaseInsensitive(a, b) : equateStringsCaseSensitive(a, b);
1512-
}
1513-
15141514
export function getStringEqualityComparer(ignoreCase: boolean) {
15151515
return ignoreCase ? equateStringsCaseInsensitive : equateStringsCaseSensitive;
15161516
}
@@ -1557,6 +1557,20 @@ namespace ts {
15571557
return ignoreCase ? compareStringsCaseInsensitive : compareStringsCaseSensitive;
15581558
}
15591559

1560+
/**
1561+
* Compare two strings using the sort behavior of the UI locale.
1562+
*
1563+
* Ordering is not predictable between different host locales, but is best for displaying
1564+
* ordered data for UI presentation. Characters with multiple unicode representations may
1565+
* be considered equal.
1566+
*
1567+
* Case-insensitive comparisons compare strings that differ in only base characters or
1568+
* accents/diacritic marks as unequal.
1569+
*/
1570+
export function compareStringsUI(a: string, b: string, ignoreCase: boolean) {
1571+
return ignoreCase ? compareStringsCaseInsensitiveUI(a, b) : compareStringsCaseSensitiveUI(a, b);
1572+
}
1573+
15601574
/**
15611575
* Creates a string comparer for use with string collation in the UI.
15621576
*/
@@ -1660,24 +1674,17 @@ namespace ts {
16601674
return comparer(a, b);
16611675
}
16621676

1663-
/**
1664-
* Compare two strings using the sort behavior of the UI locale.
1665-
*
1666-
* Ordering is not predictable between different host locales, but is best for displaying
1667-
* ordered data for UI presentation. Characters with multiple unicode representations may
1668-
* be considered equal.
1669-
*
1670-
* Case-insensitive comparisons compare strings that differ in only base characters or
1671-
* accents/diacritic marks as unequal.
1672-
*/
1673-
export function compareStringsUI(a: string, b: string, ignoreCase: boolean) {
1674-
return ignoreCase ? compareStringsCaseInsensitiveUI(a, b) : compareStringsCaseSensitiveUI(a, b);
1675-
}
1676-
16771677
export function getStringComparerUI(ignoreCase: boolean) {
16781678
return ignoreCase ? compareStringsCaseInsensitiveUI : compareStringsCaseSensitiveUI;
16791679
}
16801680

1681+
export function compareProperties<T>(a: T, b: T, key: keyof T) {
1682+
return a === b ? Comparison.EqualTo :
1683+
a === undefined ? Comparison.LessThan :
1684+
b === undefined ? Comparison.GreaterThan :
1685+
compareValues(a[key], b[key]);
1686+
}
1687+
16811688
function getDiagnosticFileName(diagnostic: Diagnostic): string {
16821689
return diagnostic.file ? diagnostic.file.fileName : undefined;
16831690
}

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3949,7 +3949,7 @@ namespace ts {
39493949
trySetLanguageAndTerritory(language, /*territory*/ undefined, errors);
39503950
}
39513951

3952-
// Set the locale for UI collation
3952+
// Set the UI locale for string collation
39533953
setUILocale(locale);
39543954

39553955
function trySetLanguageAndTerritory(language: string, territory: string, errors?: Push<Diagnostic>): boolean {

src/harness/unittests/compileOnSave.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ namespace ts.projectSystem {
1313
describe("CompileOnSave affected list", () => {
1414
function sendAffectedFileRequestAndCheckResult(session: server.Session, request: server.protocol.Request, expectedFileList: { projectFileName: string, files: FileOrFolder[] }[]) {
1515
const response = session.executeCommand(request).response as server.protocol.CompileOnSaveAffectedFileListSingleProject[];
16-
// File-system ordering should use a predictable order
1716
const comparer = getStringComparer(/*ignoreCase*/ false);
1817
const actualResult = response.sort((list1, list2) => comparer(list1.projectFileName, list2.projectFileName));
1918
expectedFileList = expectedFileList.sort((list1, list2) => comparer(list1.projectFileName, list2.projectFileName));

src/services/navigateTo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ namespace ts.NavigateTo {
173173
return bestMatchKind;
174174
}
175175

176-
function compareNavigateToItems(i1: RawNavigateToItem, i2: RawNavigateToItem): number {
176+
function compareNavigateToItems(i1: RawNavigateToItem, i2: RawNavigateToItem) {
177177
// TODO(cyrusn): get the gamut of comparisons that VS already uses here.
178-
return i1.matchKind - i2.matchKind ||
179-
compareStringsCaseSensitiveUI(i1.name, i2.name);
178+
return compareValues(i1.matchKind, i2.matchKind)
179+
|| compareStringsCaseSensitiveUI(i1.name, i2.name);
180180
}
181181

182182
function createNavigateToItem(rawItem: RawNavigateToItem): NavigateToItem {

src/services/navigationBar.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,9 @@ namespace ts.NavigationBar {
366366
children.sort(compareChildren);
367367
}
368368

369-
function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number {
370-
const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node);
371-
return compareStringsCaseInsensitiveUI(name1, name2)
372-
|| navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
369+
function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode) {
370+
return compareStringsCaseInsensitiveUI(tryGetName(child1.node), tryGetName(child2.node))
371+
|| compareValues(navigationBarNodeKind(child1), navigationBarNodeKind(child2));
373372
}
374373

375374
/**

src/services/refactors/extractSymbol.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,32 +1137,11 @@ namespace ts.refactor.extractSymbol {
11371137
{type: type1, declaration: declaration1}: {type: Type, declaration?: Declaration},
11381138
{type: type2, declaration: declaration2}: {type: Type, declaration?: Declaration}) {
11391139

1140-
if (declaration1) {
1141-
if (declaration2) {
1142-
const positionDiff = declaration1.pos - declaration2.pos;
1143-
if (positionDiff !== 0) {
1144-
return positionDiff;
1145-
}
1146-
}
1147-
else {
1148-
return 1; // Sort undeclared type parameters to the front.
1149-
}
1150-
}
1151-
else if (declaration2) {
1152-
return -1; // Sort undeclared type parameters to the front.
1153-
}
1154-
1155-
const name1 = type1.symbol ? type1.symbol.getName() : "";
1156-
const name2 = type2.symbol ? type2.symbol.getName() : "";
1157-
1158-
// This is for code generation, use a predictable comparer.
1159-
const nameDiff = compareStringsCaseSensitive(name1, name2);
1160-
if (nameDiff !== 0) {
1161-
return nameDiff;
1162-
}
1163-
1164-
// IDs are guaranteed to be unique, so this ensures a total ordering.
1165-
return type1.id - type2.id;
1140+
return compareProperties(declaration1, declaration2, "pos")
1141+
|| compareStringsCaseSensitive(
1142+
type1.symbol ? type1.symbol.getName() : "",
1143+
type2.symbol ? type2.symbol.getName() : "")
1144+
|| compareValues(type1.id, type2.id);
11661145
}
11671146

11681147
function getCalledExpression(scope: Node, range: TargetRange, functionNameText: string): Expression {

0 commit comments

Comments
 (0)