Skip to content

Commit 3f6f316

Browse files
authored
Remove stableSort, rename sort to toSorted (#55728)
1 parent ab7b624 commit 3f6f316

16 files changed

+41
-54
lines changed

src/compiler/core.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ export function sortAndDeduplicate(array: readonly string[]): SortedReadonlyArra
806806
export function sortAndDeduplicate<T>(array: readonly T[], comparer: Comparer<T>, equalityComparer?: EqualityComparer<T>): SortedReadonlyArray<T>;
807807
/** @internal */
808808
export function sortAndDeduplicate<T>(array: readonly T[], comparer?: Comparer<T>, equalityComparer?: EqualityComparer<T>): SortedReadonlyArray<T> {
809-
return deduplicateSorted(sort(array, comparer), equalityComparer ?? comparer ?? compareStringsCaseSensitive as any as Comparer<T>);
809+
return deduplicateSorted(toSorted(array, comparer), equalityComparer ?? comparer ?? compareStringsCaseSensitive as any as Comparer<T>);
810810
}
811811

812812
/** @internal */
@@ -1035,12 +1035,12 @@ function stableSortIndices<T>(array: readonly T[], indices: number[], comparer:
10351035
}
10361036

10371037
/**
1038-
* Returns a new sorted array.
1038+
* Returns a new sorted array. This sort is stable, meaning elements equal to each other maintain their relative position in the array.
10391039
*
10401040
* @internal
10411041
*/
1042-
export function sort<T>(array: readonly T[], comparer?: Comparer<T>): SortedReadonlyArray<T> {
1043-
return (array.length === 0 ? array : array.slice().sort(comparer)) as SortedReadonlyArray<T>;
1042+
export function toSorted<T>(array: readonly T[], comparer?: Comparer<T>): SortedReadonlyArray<T> {
1043+
return (array.length === 0 ? emptyArray : array.slice().sort(comparer)) as readonly T[] as SortedReadonlyArray<T>;
10441044
}
10451045

10461046
/** @internal */
@@ -1050,17 +1050,6 @@ export function* arrayReverseIterator<T>(array: readonly T[]) {
10501050
}
10511051
}
10521052

1053-
/**
1054-
* Stable sort of an array. Elements equal to each other maintain their relative position in the array.
1055-
*
1056-
* @internal
1057-
*/
1058-
export function stableSort<T>(array: readonly T[], comparer: Comparer<T>): SortedReadonlyArray<T> {
1059-
const indices = indicesOf(array);
1060-
stableSortIndices(array, indices, comparer);
1061-
return indices.map(i => array[i]) as SortedArray<T> as SortedReadonlyArray<T>;
1062-
}
1063-
10641053
/** @internal */
10651054
export function rangeEquals<T>(array1: readonly T[], array2: readonly T[], pos: number, end: number) {
10661055
while (pos < end) {

src/compiler/debug.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ import {
7979
SignatureFlags,
8080
SnippetKind,
8181
SortedReadonlyArray,
82-
stableSort,
8382
Symbol,
8483
SymbolFlags,
8584
symbolName,
8685
SyntaxKind,
86+
toSorted,
8787
TransformFlags,
8888
Type,
8989
TypeFacts,
@@ -436,7 +436,7 @@ export namespace Debug {
436436
}
437437
}
438438

439-
const sorted = stableSort<[number, string]>(result, (x, y) => compareValues(x[0], y[0]));
439+
const sorted = toSorted<[number, string]>(result, (x, y) => compareValues(x[0], y[0]));
440440
enumMemberCache.set(enumObject, sorted);
441441
return sorted;
442442
}

src/compiler/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ import {
374374
SourceMapSource,
375375
SpreadAssignment,
376376
SpreadElement,
377-
stableSort,
378377
Statement,
379378
StringLiteral,
380379
supportedJSExtensionsFlat,
@@ -394,6 +393,7 @@ import {
394393
ThrowStatement,
395394
TokenFlags,
396395
tokenToString,
396+
toSorted,
397397
tracing,
398398
TransformationResult,
399399
transformNodes,
@@ -2072,7 +2072,7 @@ export function createPrinter(printerOptions: PrinterOptions = {}, handlers: Pri
20722072

20732073
function getSortedEmitHelpers(node: Node) {
20742074
const helpers = getEmitHelpers(node);
2075-
return helpers && stableSort(helpers, compareEmitHelpers);
2075+
return helpers && toSorted(helpers, compareEmitHelpers);
20762076
}
20772077

20782078
//

src/compiler/executeCommandLine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ import {
7171
ReportEmitErrorSummary,
7272
SolutionBuilder,
7373
SolutionBuilderHostBase,
74-
sort,
7574
SourceFile,
7675
startsWith,
7776
startTracing,
@@ -80,6 +79,7 @@ import {
8079
sys,
8180
System,
8281
toPath,
82+
toSorted,
8383
tracing,
8484
validateLocaleAndSetLanguage,
8585
version,
@@ -170,7 +170,7 @@ function shouldBePretty(sys: System, options: CompilerOptions | BuildOptions) {
170170
function getOptionsForHelp(commandLine: ParsedCommandLine) {
171171
// Sort our options by their names, (e.g. "--noImplicitAny" comes before "--watch")
172172
return !!commandLine.options.all ?
173-
sort(optionDeclarations, (a, b) => compareStringsCaseInsensitive(a.name, b.name)) :
173+
toSorted(optionDeclarations, (a, b) => compareStringsCaseInsensitive(a.name, b.name)) :
174174
filter(optionDeclarations.slice(), v => !!v.showInSimplifiedHelpView);
175175
}
176176

src/compiler/moduleNameResolver.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ import {
9494
ResolvedTypeReferenceDirective,
9595
ResolvedTypeReferenceDirectiveWithFailedLookupLocations,
9696
some,
97-
sort,
9897
startsWith,
9998
supportedDeclarationExtensions,
10099
supportedJSExtensionsFlat,
101100
supportedTSImplementationExtensions,
102101
toPath,
102+
toSorted,
103103
tryExtractTSExtension,
104104
tryGetExtensionFromPath,
105105
tryParsePatterns,
@@ -2695,7 +2695,7 @@ function loadModuleFromImportsOrExports(extensions: Extensions, state: ModuleRes
26952695
const target = (lookupTable as { [idx: string]: unknown; })[moduleName];
26962696
return loadModuleFromTargetImportOrExport(target, /*subpath*/ "", /*pattern*/ false, moduleName);
26972697
}
2698-
const expandingKeys = sort(filter(getOwnKeys(lookupTable as MapLike<unknown>), k => hasOneAsterisk(k) || endsWith(k, "/")), comparePatternKeys);
2698+
const expandingKeys = toSorted(filter(getOwnKeys(lookupTable as MapLike<unknown>), k => hasOneAsterisk(k) || endsWith(k, "/")), comparePatternKeys);
26992699
for (const potentialTarget of expandingKeys) {
27002700
if (state.features & NodeResolutionFeatures.ExportsPatternTrailers && matchesPatternWithTrailer(potentialTarget, moduleName)) {
27012701
const target = (lookupTable as { [idx: string]: unknown; })[potentialTarget];

src/compiler/program.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,6 @@ import {
302302
sourceFileAffectingCompilerOptions,
303303
sourceFileMayBeEmitted,
304304
SourceOfProjectReferenceRedirect,
305-
stableSort,
306305
startsWith,
307306
Statement,
308307
StringLiteral,
@@ -316,6 +315,7 @@ import {
316315
toFileNameLowerCase,
317316
tokenToString,
318317
toPath as ts_toPath,
318+
toSorted,
319319
trace,
320320
tracing,
321321
tryCast,
@@ -1887,7 +1887,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
18871887
}
18881888
}
18891889

1890-
files = stableSort(processingDefaultLibFiles, compareDefaultLibFiles).concat(processingOtherFiles);
1890+
files = toSorted(processingDefaultLibFiles, compareDefaultLibFiles).concat(processingOtherFiles);
18911891
processingDefaultLibFiles = undefined;
18921892
processingOtherFiles = undefined;
18931893
filesWithReferencesProcessed = undefined;

src/compiler/utilities.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,6 @@ import {
510510
skipTrivia,
511511
SnippetKind,
512512
some,
513-
sort,
514513
SortedArray,
515514
SourceFile,
516515
SourceFileLike,
@@ -545,6 +544,7 @@ import {
545544
TokenFlags,
546545
tokenToString,
547546
toPath,
547+
toSorted,
548548
tracing,
549549
TransformFlags,
550550
TransientSymbol,
@@ -9596,7 +9596,7 @@ export function matchFiles(path: string, extensions: readonly string[] | undefin
95969596
visited.set(canonicalPath, true);
95979597
const { files, directories } = getFileSystemEntries(path);
95989598

9599-
for (const current of sort<string>(files, compareStringsCaseSensitive)) {
9599+
for (const current of toSorted<string>(files, compareStringsCaseSensitive)) {
96009600
const name = combinePaths(path, current);
96019601
const absoluteName = combinePaths(absolutePath, current);
96029602
if (extensions && !fileExtensionIsOneOf(name, extensions)) continue;
@@ -9619,7 +9619,7 @@ export function matchFiles(path: string, extensions: readonly string[] | undefin
96199619
}
96209620
}
96219621

9622-
for (const current of sort<string>(directories, compareStringsCaseSensitive)) {
9622+
for (const current of toSorted<string>(directories, compareStringsCaseSensitive)) {
96239623
const name = combinePaths(path, current);
96249624
const absoluteName = combinePaths(absolutePath, current);
96259625
if (

src/harness/fourslashImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,7 @@ export class TestState {
16211621
}
16221622
}
16231623
let pos = 0;
1624-
const sortedDetails = ts.stableSort(details, (a, b) => ts.compareValues(a.location, b.location));
1624+
const sortedDetails = ts.toSorted(details, (a, b) => ts.compareValues(a.location, b.location));
16251625
if (!canDetermineContextIdInline) {
16261626
// Assign contextIds
16271627
sortedDetails.forEach(({ span, type }) => {

src/harness/fourslashInterfaceImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ export namespace Completion {
11211121
].map(keywordEntry);
11221122

11231123
export function sorted(entries: readonly ExpectedCompletionEntry[]): readonly ExpectedCompletionEntry[] {
1124-
return ts.stableSort(entries, compareExpectedCompletionEntries);
1124+
return ts.toSorted(entries, compareExpectedCompletionEntries);
11251125
}
11261126

11271127
// If you want to use a function like `globalsPlus`, that function needs to sort

src/harness/harnessIO.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ export namespace Compiler {
547547
export const diagnosticSummaryMarker = "__diagnosticSummary";
548548
export const globalErrorsMarker = "__globalErrors";
549549
export function* iterateErrorBaseline(inputFiles: readonly TestFile[], diagnostics: readonly ts.Diagnostic[], options?: { pretty?: boolean; caseSensitive?: boolean; currentDirectory?: string; }): IterableIterator<[string, string, number]> {
550-
diagnostics = ts.sort(diagnostics, ts.compareDiagnostics);
550+
diagnostics = ts.toSorted(diagnostics, ts.compareDiagnostics);
551551
let outputLines = "";
552552
// Count up all errors that were found in files other than lib.d.ts so we don't miss any
553553
let totalErrorsReportedInNonLibraryNonTsconfigFiles = 0;

0 commit comments

Comments
 (0)