Skip to content

Commit 7c515bf

Browse files
author
Andy
authored
Remove toSortedArray and toDeduplicatedSortedArray, use sort and sortAndDeduplicate (#28214)
1 parent 513c6af commit 7c515bf

File tree

14 files changed

+40
-56
lines changed

14 files changed

+40
-56
lines changed

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ namespace ts {
11461146
}
11471147

11481148
/* @internal */
1149-
export function printHelp(optionsList: CommandLineOption[], syntaxPrefix = "") {
1149+
export function printHelp(optionsList: ReadonlyArray<CommandLineOption>, syntaxPrefix = "") {
11501150
const output: string[] = [];
11511151

11521152
// We want to align our "syntax" and "examples" commands to a certain margin.

src/compiler/core.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ namespace ts {
1616
[index: string]: T;
1717
}
1818

19+
export interface SortedReadonlyArray<T> extends ReadonlyArray<T> {
20+
" __sortedArrayBrand": any;
21+
}
22+
1923
export interface SortedArray<T> extends Array<T> {
2024
" __sortedArrayBrand": any;
2125
}
@@ -815,8 +819,8 @@ namespace ts {
815819
/**
816820
* Deduplicates an array that has already been sorted.
817821
*/
818-
function deduplicateSorted<T>(array: ReadonlyArray<T>, comparer: EqualityComparer<T> | Comparer<T>): T[] {
819-
if (array.length === 0) return [];
822+
function deduplicateSorted<T>(array: SortedReadonlyArray<T>, comparer: EqualityComparer<T> | Comparer<T>): SortedReadonlyArray<T> {
823+
if (array.length === 0) return emptyArray as any as SortedReadonlyArray<T>;
820824

821825
let last = array[0];
822826
const deduplicated: T[] = [last];
@@ -838,7 +842,7 @@ namespace ts {
838842
deduplicated.push(last = next);
839843
}
840844

841-
return deduplicated;
845+
return deduplicated as any as SortedReadonlyArray<T>;
842846
}
843847

844848
export function insertSorted<T>(array: SortedArray<T>, insert: T, compare: Comparer<T>): void {
@@ -853,8 +857,10 @@ namespace ts {
853857
}
854858
}
855859

856-
export function sortAndDeduplicate<T>(array: ReadonlyArray<T>, comparer: Comparer<T>, equalityComparer?: EqualityComparer<T>) {
857-
return deduplicateSorted(sort(array, comparer), equalityComparer || comparer);
860+
export function sortAndDeduplicate<T>(array: ReadonlyArray<string>): SortedReadonlyArray<string>;
861+
export function sortAndDeduplicate<T>(array: ReadonlyArray<T>, comparer: Comparer<T>, equalityComparer?: EqualityComparer<T>): SortedReadonlyArray<T>;
862+
export function sortAndDeduplicate<T>(array: ReadonlyArray<T>, comparer?: Comparer<T>, equalityComparer?: EqualityComparer<T>): SortedReadonlyArray<T> {
863+
return deduplicateSorted(sort(array, comparer), equalityComparer || comparer || compareStringsCaseSensitive as any as Comparer<T>);
858864
}
859865

860866
export function arrayIsEqualTo<T>(array1: ReadonlyArray<T> | undefined, array2: ReadonlyArray<T> | undefined, equalityComparer: (a: T, b: T, index: number) => boolean = equateValues): boolean {
@@ -1035,8 +1041,8 @@ namespace ts {
10351041
/**
10361042
* Returns a new sorted array.
10371043
*/
1038-
export function sort<T>(array: ReadonlyArray<T>, comparer: Comparer<T>): T[] {
1039-
return array.slice().sort(comparer);
1044+
export function sort<T>(array: ReadonlyArray<T>, comparer?: Comparer<T>): SortedReadonlyArray<T> {
1045+
return (array.length === 0 ? array : array.slice().sort(comparer)) as SortedReadonlyArray<T>;
10401046
}
10411047

10421048
export function arrayIterator<T>(array: ReadonlyArray<T>): Iterator<T> {

src/compiler/program.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ namespace ts {
198198
};
199199
}
200200

201-
export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[] {
201+
export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic> {
202202
const diagnostics = [
203203
...program.getConfigFileParsingDiagnostics(),
204204
...program.getOptionsDiagnostics(cancellationToken),
@@ -1817,7 +1817,7 @@ namespace ts {
18171817
return sourceFile.isDeclarationFile ? [] : getDeclarationDiagnosticsWorker(sourceFile, cancellationToken);
18181818
}
18191819

1820-
function getOptionsDiagnostics(): Diagnostic[] {
1820+
function getOptionsDiagnostics(): SortedReadonlyArray<Diagnostic> {
18211821
return sortAndDeduplicateDiagnostics(concatenate(
18221822
fileProcessingDiagnostics.getGlobalDiagnostics(),
18231823
concatenate(
@@ -1838,7 +1838,7 @@ namespace ts {
18381838
return diagnostics;
18391839
}
18401840

1841-
function getGlobalDiagnostics(): Diagnostic[] {
1841+
function getGlobalDiagnostics(): SortedReadonlyArray<Diagnostic> {
18421842
return sortAndDeduplicateDiagnostics(getDiagnosticsProducingTypeChecker().getGlobalDiagnostics().slice());
18431843
}
18441844

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace ts {
77
return pathIsRelative(moduleName) || isRootedDiskPath(moduleName);
88
}
99

10-
export function sortAndDeduplicateDiagnostics<T extends Diagnostic>(diagnostics: ReadonlyArray<T>): T[] {
10+
export function sortAndDeduplicateDiagnostics<T extends Diagnostic>(diagnostics: ReadonlyArray<T>): SortedReadonlyArray<T> {
1111
return sortAndDeduplicate<T>(diagnostics, compareDiagnostics);
1212
}
1313
}

src/harness/compiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ namespace compiler {
5858
private _inputs: documents.TextDocument[] = [];
5959
private _inputsAndOutputs: collections.SortedMap<string, CompilationOutput>;
6060

61-
constructor(host: fakes.CompilerHost, options: ts.CompilerOptions, program: ts.Program | undefined, result: ts.EmitResult | undefined, diagnostics: ts.Diagnostic[]) {
61+
constructor(host: fakes.CompilerHost, options: ts.CompilerOptions, program: ts.Program | undefined, result: ts.EmitResult | undefined, diagnostics: ReadonlyArray<ts.Diagnostic>) {
6262
this.host = host;
6363
this.program = program;
6464
this.result = result;

src/jsTyping/types.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ declare namespace ts.server {
88
export type EventEndInstallTypes = "event::endInstallTypes";
99
export type EventInitializationFailed = "event::initializationFailed";
1010

11-
export interface SortedReadonlyArray<T> extends ReadonlyArray<T> {
12-
" __sortedArrayBrand": any;
13-
}
14-
1511
export interface TypingInstallerResponse {
1612
readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed;
1713
}

src/server/project.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ namespace ts.server {
554554
}
555555

556556
getExternalFiles(): SortedReadonlyArray<string> {
557-
return toSortedArray(flatMapToMutable(this.plugins, plugin => {
557+
return sort(flatMap(this.plugins, plugin => {
558558
if (typeof plugin.module.getExternalFiles !== "function") return;
559559
try {
560560
return plugin.module.getExternalFiles(this);
@@ -871,7 +871,7 @@ namespace ts.server {
871871
(result || (result = [])).push(...unResolved);
872872
}
873873
}
874-
this.lastCachedUnresolvedImportsList = result ? toDeduplicatedSortedArray(result) : emptyArray;
874+
this.lastCachedUnresolvedImportsList = result ? sortAndDeduplicate(result) : emptyArray;
875875
}
876876

877877
this.projectService.typingsCache.enqueueInstallTypingsForProject(this, this.lastCachedUnresolvedImportsList, hasAddedorRemovedFiles);
@@ -888,7 +888,7 @@ namespace ts.server {
888888

889889
/*@internal*/
890890
updateTypingFiles(typingFiles: SortedReadonlyArray<string>) {
891-
enumerateInsertsAndDeletes(typingFiles, this.typingFiles, getStringComparer(!this.useCaseSensitiveFileNames()),
891+
enumerateInsertsAndDeletes<string, string>(typingFiles, this.typingFiles, getStringComparer(!this.useCaseSensitiveFileNames()),
892892
/*inserted*/ noop,
893893
removed => this.detachScriptInfoFromProject(removed)
894894
);
@@ -959,7 +959,7 @@ namespace ts.server {
959959

960960
const oldExternalFiles = this.externalFiles || emptyArray as SortedReadonlyArray<string>;
961961
this.externalFiles = this.getExternalFiles();
962-
enumerateInsertsAndDeletes(this.externalFiles, oldExternalFiles, getStringComparer(!this.useCaseSensitiveFileNames()),
962+
enumerateInsertsAndDeletes<string, string>(this.externalFiles, oldExternalFiles, getStringComparer(!this.useCaseSensitiveFileNames()),
963963
// Ensure a ScriptInfo is created for new external files. This is performed indirectly
964964
// by the LSHost for files in the program when the program is retrieved above but
965965
// the program doesn't contain external files so this must be done explicitly.

src/server/typingsCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace ts.server {
130130
}
131131

132132
updateTypingsForProject(projectName: string, compilerOptions: CompilerOptions, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>, newTypings: string[]) {
133-
const typings = toSortedArray(newTypings);
133+
const typings = sort(newTypings);
134134
this.perProjectCache.set(projectName, {
135135
compilerOptions,
136136
typeAcquisition,

src/server/utilities.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,6 @@ namespace ts.server {
206206
}
207207
}
208208

209-
export function toSortedArray(arr: string[]): SortedArray<string>;
210-
export function toSortedArray<T>(arr: T[], comparer: Comparer<T>): SortedArray<T>;
211-
export function toSortedArray<T>(arr: T[], comparer?: Comparer<T>): SortedArray<T> {
212-
arr.sort(comparer);
213-
return arr as SortedArray<T>;
214-
}
215-
216-
export function toDeduplicatedSortedArray(arr: string[]): SortedArray<string> {
217-
arr.sort();
218-
filterMutate(arr, isNonDuplicateInSortedArray);
219-
return arr as SortedArray<string>;
220-
}
221-
function isNonDuplicateInSortedArray<T>(value: T, index: number, array: T[]) {
222-
return index === 0 || value !== array[index - 1];
223-
}
224-
225209
const indentStr = "\n ";
226210

227211
/* @internal */

src/testRunner/projectsRunner.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,7 @@ namespace project {
310310
const program = ts.createProgram(getInputFiles(), compilerOptions, compilerHost);
311311
const errors = ts.getPreEmitDiagnostics(program);
312312

313-
const emitResult = program.emit();
314-
ts.addRange(errors, emitResult.diagnostics);
315-
const sourceMapData = emitResult.sourceMaps;
313+
const { sourceMaps: sourceMapData, diagnostics: emitDiagnostics } = program.emit();
316314

317315
// Clean up source map data that will be used in baselining
318316
if (sourceMapData) {
@@ -329,7 +327,7 @@ namespace project {
329327
configFileSourceFiles,
330328
moduleKind,
331329
program,
332-
errors,
330+
errors: ts.concatenate(errors, emitDiagnostics),
333331
sourceMapData
334332
};
335333
}

src/testRunner/unittests/tsserverProjectSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ namespace ts.projectSystem {
118118
this.projectService.updateTypingsForProject(response);
119119
}
120120

121-
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray<string>) {
121+
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>) {
122122
const request = server.createInstallTypingsRequest(project, typeAcquisition, unresolvedImports, this.globalTypingsCacheLocation);
123123
this.install(request);
124124
}

src/testRunner/unittests/typingsInstaller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ namespace ts.projectSystem {
310310
constructor() {
311311
super(host, { typesRegistry: createTypesRegistry("jquery") });
312312
}
313-
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray<string>) {
313+
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>) {
314314
enqueueIsCalled = true;
315315
super.enqueueInstallTypingsRequest(project, typeAcquisition, unresolvedImports);
316316
}
@@ -409,7 +409,7 @@ namespace ts.projectSystem {
409409
constructor() {
410410
super(host, { typesRegistry: createTypesRegistry("jquery") });
411411
}
412-
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray<string>) {
412+
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>) {
413413
super.enqueueInstallTypingsRequest(project, typeAcquisition, unresolvedImports);
414414
}
415415
installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction): void {
@@ -451,7 +451,7 @@ namespace ts.projectSystem {
451451
constructor() {
452452
super(host, { typesRegistry: createTypesRegistry("jquery") });
453453
}
454-
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray<string>) {
454+
enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray<string>) {
455455
super.enqueueInstallTypingsRequest(project, typeAcquisition, unresolvedImports);
456456
}
457457
installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction): void {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ declare namespace ts {
2727
interface MapLike<T> {
2828
[index: string]: T;
2929
}
30+
interface SortedReadonlyArray<T> extends ReadonlyArray<T> {
31+
" __sortedArrayBrand": any;
32+
}
3033
interface SortedArray<T> extends Array<T> {
3134
" __sortedArrayBrand": any;
3235
}
@@ -3126,7 +3129,7 @@ declare namespace ts {
31263129
/** Non-internal stuff goes here */
31273130
declare namespace ts {
31283131
function isExternalModuleNameRelative(moduleName: string): boolean;
3129-
function sortAndDeduplicateDiagnostics<T extends Diagnostic>(diagnostics: ReadonlyArray<T>): T[];
3132+
function sortAndDeduplicateDiagnostics<T extends Diagnostic>(diagnostics: ReadonlyArray<T>): SortedReadonlyArray<T>;
31303133
}
31313134
declare namespace ts {
31323135
function getDefaultLibFileName(options: CompilerOptions): string;
@@ -4153,7 +4156,7 @@ declare namespace ts {
41534156
function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string | undefined;
41544157
function resolveTripleslashReference(moduleName: string, containingFile: string): string;
41554158
function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost;
4156-
function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
4159+
function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
41574160
interface FormatDiagnosticsHost {
41584161
getCurrentDirectory(): string;
41594162
getCanonicalFileName(fileName: string): string;
@@ -4457,9 +4460,6 @@ declare namespace ts.server {
44574460
type EventBeginInstallTypes = "event::beginInstallTypes";
44584461
type EventEndInstallTypes = "event::endInstallTypes";
44594462
type EventInitializationFailed = "event::initializationFailed";
4460-
interface SortedReadonlyArray<T> extends ReadonlyArray<T> {
4461-
" __sortedArrayBrand": any;
4462-
}
44634463
interface TypingInstallerResponse {
44644464
readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed;
44654465
}

tests/baselines/reference/api/typescript.d.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ declare namespace ts {
2727
interface MapLike<T> {
2828
[index: string]: T;
2929
}
30+
interface SortedReadonlyArray<T> extends ReadonlyArray<T> {
31+
" __sortedArrayBrand": any;
32+
}
3033
interface SortedArray<T> extends Array<T> {
3134
" __sortedArrayBrand": any;
3235
}
@@ -3126,7 +3129,7 @@ declare namespace ts {
31263129
/** Non-internal stuff goes here */
31273130
declare namespace ts {
31283131
function isExternalModuleNameRelative(moduleName: string): boolean;
3129-
function sortAndDeduplicateDiagnostics<T extends Diagnostic>(diagnostics: ReadonlyArray<T>): T[];
3132+
function sortAndDeduplicateDiagnostics<T extends Diagnostic>(diagnostics: ReadonlyArray<T>): SortedReadonlyArray<T>;
31303133
}
31313134
declare namespace ts {
31323135
function getDefaultLibFileName(options: CompilerOptions): string;
@@ -4153,7 +4156,7 @@ declare namespace ts {
41534156
function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string | undefined;
41544157
function resolveTripleslashReference(moduleName: string, containingFile: string): string;
41554158
function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost;
4156-
function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
4159+
function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
41574160
interface FormatDiagnosticsHost {
41584161
getCurrentDirectory(): string;
41594162
getCanonicalFileName(fileName: string): string;
@@ -4457,9 +4460,6 @@ declare namespace ts.server {
44574460
type EventBeginInstallTypes = "event::beginInstallTypes";
44584461
type EventEndInstallTypes = "event::endInstallTypes";
44594462
type EventInitializationFailed = "event::initializationFailed";
4460-
interface SortedReadonlyArray<T> extends ReadonlyArray<T> {
4461-
" __sortedArrayBrand": any;
4462-
}
44634463
interface TypingInstallerResponse {
44644464
readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed;
44654465
}

0 commit comments

Comments
 (0)