Skip to content

Ensure that Comparers only return -1, 0, 1 so results can be equated #52897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/compiler/corePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export interface ReadonlyCollection<K> {
/** @internal */
export type EqualityComparer<T> = (a: T, b: T) => boolean;

// The values of Comparison are specified here to ensure that a function like
// `(a: T, b: T) => number` is not assignable; while many algorithms are okay
// with values other than these three, some are not (like binarySearch).
/** @internal */
export type Comparer<T> = (a: T, b: T) => Comparison;
export type Comparer<T> = (a: T, b: T) => -1 | 0 | 1;

/** @internal */
export const enum Comparison {
Expand Down
6 changes: 3 additions & 3 deletions src/harness/collectionsImpl.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as ts from "./_namespaces/ts.js";

export interface SortOptions<T> {
comparer: (a: T, b: T) => number;
comparer: ts.Comparer<T>;
sort: "insertion" | "comparison";
}

export class SortedMap<K, V> {
private _comparer: (a: K, b: K) => number;
private _comparer: ts.Comparer<K>;
private _keys: K[] = [];
private _values: V[] = [];
private _order: number[] | undefined;
private _version = 0;
private _copyOnWrite = false;

constructor(comparer: ((a: K, b: K) => number) | SortOptions<K>, iterable?: Iterable<[K, V]>) {
constructor(comparer: ts.Comparer<K> | SortOptions<K>, iterable?: Iterable<[K, V]>) {
this._comparer = typeof comparer === "object" ? comparer.comparer : comparer;
this._order = typeof comparer === "object" && comparer.sort === "insertion" ? [] : undefined;
if (iterable) {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/vfsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class FileSystem {
public readonly ignoreCase: boolean;

/** Gets the comparison function used to compare two paths. */
public readonly stringComparer: (a: string, b: string) => number;
public readonly stringComparer: ts.Comparer<string>;

// lazy-initialized state that should be mutable even if the FileSystem is frozen.
private _lazy: {
Expand Down
3 changes: 1 addition & 2 deletions src/services/organizeImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,8 +853,7 @@ function getOrganizeImportsUnicodeStringComparer(ignoreCase: boolean, preference
numeric,
});

// `compare` is a bound method, so we do not need to close over `collator`.
return collator.compare;
return (a, b) => Math.sign(collator.compare(a, b)) as -1 | 0 | 1;
}

function getOrganizeImportsLocale(preferences: UserPreferences): string {
Expand Down
3 changes: 2 additions & 1 deletion src/services/textChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ClassExpression,
ClassLikeDeclaration,
CommentRange,
compareValues,
concatenate,
ConstructorDeclaration,
contains,
Expand Down Expand Up @@ -1264,7 +1265,7 @@ namespace changesToText {
const sourceFile = changesInFile[0].sourceFile;
// order changes by start position
// If the start position is the same, put the shorter range first, since an empty range (x, x) may precede (x, y) but not vice-versa.
const normalized = toSorted(changesInFile, (a, b) => (a.range.pos - b.range.pos) || (a.range.end - b.range.end));
const normalized = toSorted(changesInFile, (a, b) => compareValues(a.range.pos, b.range.pos) || compareValues(a.range.end, b.range.end));
// verify that change intervals do not overlap, except possibly at end points.
for (let i = 0; i < normalized.length - 1; i++) {
Debug.assert(normalized[i].range.end <= normalized[i + 1].range.pos, "Changes overlap", () => `${JSON.stringify(normalized[i].range)} and ${JSON.stringify(normalized[i + 1].range)}`);
Expand Down
Loading