diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 968e5bb756eaa..782f76b3d1fa5 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -1146,7 +1146,7 @@ namespace ts { } /* @internal */ - export function printHelp(optionsList: CommandLineOption[], syntaxPrefix = "") { + export function printHelp(optionsList: ReadonlyArray, syntaxPrefix = "") { const output: string[] = []; // We want to align our "syntax" and "examples" commands to a certain margin. diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 6288f30b14472..8c976cee9ee56 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -16,6 +16,10 @@ namespace ts { [index: string]: T; } + export interface SortedReadonlyArray extends ReadonlyArray { + " __sortedArrayBrand": any; + } + export interface SortedArray extends Array { " __sortedArrayBrand": any; } @@ -815,8 +819,8 @@ namespace ts { /** * Deduplicates an array that has already been sorted. */ - function deduplicateSorted(array: ReadonlyArray, comparer: EqualityComparer | Comparer): T[] { - if (array.length === 0) return []; + function deduplicateSorted(array: SortedReadonlyArray, comparer: EqualityComparer | Comparer): SortedReadonlyArray { + if (array.length === 0) return emptyArray as any as SortedReadonlyArray; let last = array[0]; const deduplicated: T[] = [last]; @@ -838,7 +842,7 @@ namespace ts { deduplicated.push(last = next); } - return deduplicated; + return deduplicated as any as SortedReadonlyArray; } export function insertSorted(array: SortedArray, insert: T, compare: Comparer): void { @@ -853,8 +857,10 @@ namespace ts { } } - export function sortAndDeduplicate(array: ReadonlyArray, comparer: Comparer, equalityComparer?: EqualityComparer) { - return deduplicateSorted(sort(array, comparer), equalityComparer || comparer); + export function sortAndDeduplicate(array: ReadonlyArray): SortedReadonlyArray; + export function sortAndDeduplicate(array: ReadonlyArray, comparer: Comparer, equalityComparer?: EqualityComparer): SortedReadonlyArray; + export function sortAndDeduplicate(array: ReadonlyArray, comparer?: Comparer, equalityComparer?: EqualityComparer): SortedReadonlyArray { + return deduplicateSorted(sort(array, comparer), equalityComparer || comparer || compareStringsCaseSensitive as any as Comparer); } export function arrayIsEqualTo(array1: ReadonlyArray | undefined, array2: ReadonlyArray | undefined, equalityComparer: (a: T, b: T, index: number) => boolean = equateValues): boolean { @@ -1035,8 +1041,8 @@ namespace ts { /** * Returns a new sorted array. */ - export function sort(array: ReadonlyArray, comparer: Comparer): T[] { - return array.slice().sort(comparer); + export function sort(array: ReadonlyArray, comparer?: Comparer): SortedReadonlyArray { + return (array.length === 0 ? array : array.slice().sort(comparer)) as SortedReadonlyArray; } export function arrayIterator(array: ReadonlyArray): Iterator { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 85c6b27f4ec7d..997bdaa80c726 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -198,7 +198,7 @@ namespace ts { }; } - export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[] { + export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray { const diagnostics = [ ...program.getConfigFileParsingDiagnostics(), ...program.getOptionsDiagnostics(cancellationToken), @@ -1817,7 +1817,7 @@ namespace ts { return sourceFile.isDeclarationFile ? [] : getDeclarationDiagnosticsWorker(sourceFile, cancellationToken); } - function getOptionsDiagnostics(): Diagnostic[] { + function getOptionsDiagnostics(): SortedReadonlyArray { return sortAndDeduplicateDiagnostics(concatenate( fileProcessingDiagnostics.getGlobalDiagnostics(), concatenate( @@ -1838,7 +1838,7 @@ namespace ts { return diagnostics; } - function getGlobalDiagnostics(): Diagnostic[] { + function getGlobalDiagnostics(): SortedReadonlyArray { return sortAndDeduplicateDiagnostics(getDiagnosticsProducingTypeChecker().getGlobalDiagnostics().slice()); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 96b9c3c2f67ef..a969d580b86a9 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -7,7 +7,7 @@ namespace ts { return pathIsRelative(moduleName) || isRootedDiskPath(moduleName); } - export function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): T[] { + export function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): SortedReadonlyArray { return sortAndDeduplicate(diagnostics, compareDiagnostics); } } diff --git a/src/harness/compiler.ts b/src/harness/compiler.ts index 7d41545708ba7..532a2d655789a 100644 --- a/src/harness/compiler.ts +++ b/src/harness/compiler.ts @@ -58,7 +58,7 @@ namespace compiler { private _inputs: documents.TextDocument[] = []; private _inputsAndOutputs: collections.SortedMap; - constructor(host: fakes.CompilerHost, options: ts.CompilerOptions, program: ts.Program | undefined, result: ts.EmitResult | undefined, diagnostics: ts.Diagnostic[]) { + constructor(host: fakes.CompilerHost, options: ts.CompilerOptions, program: ts.Program | undefined, result: ts.EmitResult | undefined, diagnostics: ReadonlyArray) { this.host = host; this.program = program; this.result = result; diff --git a/src/jsTyping/types.ts b/src/jsTyping/types.ts index 408d30a65118a..6bc3e667f8766 100644 --- a/src/jsTyping/types.ts +++ b/src/jsTyping/types.ts @@ -8,10 +8,6 @@ declare namespace ts.server { export type EventEndInstallTypes = "event::endInstallTypes"; export type EventInitializationFailed = "event::initializationFailed"; - export interface SortedReadonlyArray extends ReadonlyArray { - " __sortedArrayBrand": any; - } - export interface TypingInstallerResponse { readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; } diff --git a/src/server/project.ts b/src/server/project.ts index fb6ec4b80fc74..ff2e3539b6bbc 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -554,7 +554,7 @@ namespace ts.server { } getExternalFiles(): SortedReadonlyArray { - return toSortedArray(flatMapToMutable(this.plugins, plugin => { + return sort(flatMap(this.plugins, plugin => { if (typeof plugin.module.getExternalFiles !== "function") return; try { return plugin.module.getExternalFiles(this); @@ -871,7 +871,7 @@ namespace ts.server { (result || (result = [])).push(...unResolved); } } - this.lastCachedUnresolvedImportsList = result ? toDeduplicatedSortedArray(result) : emptyArray; + this.lastCachedUnresolvedImportsList = result ? sortAndDeduplicate(result) : emptyArray; } this.projectService.typingsCache.enqueueInstallTypingsForProject(this, this.lastCachedUnresolvedImportsList, hasAddedorRemovedFiles); @@ -888,7 +888,7 @@ namespace ts.server { /*@internal*/ updateTypingFiles(typingFiles: SortedReadonlyArray) { - enumerateInsertsAndDeletes(typingFiles, this.typingFiles, getStringComparer(!this.useCaseSensitiveFileNames()), + enumerateInsertsAndDeletes(typingFiles, this.typingFiles, getStringComparer(!this.useCaseSensitiveFileNames()), /*inserted*/ noop, removed => this.detachScriptInfoFromProject(removed) ); @@ -959,7 +959,7 @@ namespace ts.server { const oldExternalFiles = this.externalFiles || emptyArray as SortedReadonlyArray; this.externalFiles = this.getExternalFiles(); - enumerateInsertsAndDeletes(this.externalFiles, oldExternalFiles, getStringComparer(!this.useCaseSensitiveFileNames()), + enumerateInsertsAndDeletes(this.externalFiles, oldExternalFiles, getStringComparer(!this.useCaseSensitiveFileNames()), // Ensure a ScriptInfo is created for new external files. This is performed indirectly // by the LSHost for files in the program when the program is retrieved above but // the program doesn't contain external files so this must be done explicitly. diff --git a/src/server/typingsCache.ts b/src/server/typingsCache.ts index 64a117bbe258e..d3b6e21a21c94 100644 --- a/src/server/typingsCache.ts +++ b/src/server/typingsCache.ts @@ -130,7 +130,7 @@ namespace ts.server { } updateTypingsForProject(projectName: string, compilerOptions: CompilerOptions, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray, newTypings: string[]) { - const typings = toSortedArray(newTypings); + const typings = sort(newTypings); this.perProjectCache.set(projectName, { compilerOptions, typeAcquisition, diff --git a/src/server/utilities.ts b/src/server/utilities.ts index 6dcfff6f7e22a..4eac1730b7704 100644 --- a/src/server/utilities.ts +++ b/src/server/utilities.ts @@ -206,22 +206,6 @@ namespace ts.server { } } - export function toSortedArray(arr: string[]): SortedArray; - export function toSortedArray(arr: T[], comparer: Comparer): SortedArray; - export function toSortedArray(arr: T[], comparer?: Comparer): SortedArray { - arr.sort(comparer); - return arr as SortedArray; - } - - export function toDeduplicatedSortedArray(arr: string[]): SortedArray { - arr.sort(); - filterMutate(arr, isNonDuplicateInSortedArray); - return arr as SortedArray; - } - function isNonDuplicateInSortedArray(value: T, index: number, array: T[]) { - return index === 0 || value !== array[index - 1]; - } - const indentStr = "\n "; /* @internal */ diff --git a/src/testRunner/projectsRunner.ts b/src/testRunner/projectsRunner.ts index 3dcf1911dde80..2d15133f399af 100644 --- a/src/testRunner/projectsRunner.ts +++ b/src/testRunner/projectsRunner.ts @@ -310,9 +310,7 @@ namespace project { const program = ts.createProgram(getInputFiles(), compilerOptions, compilerHost); const errors = ts.getPreEmitDiagnostics(program); - const emitResult = program.emit(); - ts.addRange(errors, emitResult.diagnostics); - const sourceMapData = emitResult.sourceMaps; + const { sourceMaps: sourceMapData, diagnostics: emitDiagnostics } = program.emit(); // Clean up source map data that will be used in baselining if (sourceMapData) { @@ -329,7 +327,7 @@ namespace project { configFileSourceFiles, moduleKind, program, - errors, + errors: ts.concatenate(errors, emitDiagnostics), sourceMapData }; } diff --git a/src/testRunner/unittests/tsserverProjectSystem.ts b/src/testRunner/unittests/tsserverProjectSystem.ts index 38d2d4867a0ed..3ffd28702a2db 100644 --- a/src/testRunner/unittests/tsserverProjectSystem.ts +++ b/src/testRunner/unittests/tsserverProjectSystem.ts @@ -118,7 +118,7 @@ namespace ts.projectSystem { this.projectService.updateTypingsForProject(response); } - enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray) { + enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray) { const request = server.createInstallTypingsRequest(project, typeAcquisition, unresolvedImports, this.globalTypingsCacheLocation); this.install(request); } diff --git a/src/testRunner/unittests/typingsInstaller.ts b/src/testRunner/unittests/typingsInstaller.ts index a718a12f5484f..b06c22d5d7912 100644 --- a/src/testRunner/unittests/typingsInstaller.ts +++ b/src/testRunner/unittests/typingsInstaller.ts @@ -310,7 +310,7 @@ namespace ts.projectSystem { constructor() { super(host, { typesRegistry: createTypesRegistry("jquery") }); } - enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray) { + enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray) { enqueueIsCalled = true; super.enqueueInstallTypingsRequest(project, typeAcquisition, unresolvedImports); } @@ -409,7 +409,7 @@ namespace ts.projectSystem { constructor() { super(host, { typesRegistry: createTypesRegistry("jquery") }); } - enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray) { + enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray) { super.enqueueInstallTypingsRequest(project, typeAcquisition, unresolvedImports); } installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction): void { @@ -451,7 +451,7 @@ namespace ts.projectSystem { constructor() { super(host, { typesRegistry: createTypesRegistry("jquery") }); } - enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: server.SortedReadonlyArray) { + enqueueInstallTypingsRequest(project: server.Project, typeAcquisition: TypeAcquisition, unresolvedImports: SortedReadonlyArray) { super.enqueueInstallTypingsRequest(project, typeAcquisition, unresolvedImports); } installWorker(_requestId: number, _args: string[], _cwd: string, cb: TI.RequestCompletedAction): void { diff --git a/tests/baselines/reference/api/tsserverlibrary.d.ts b/tests/baselines/reference/api/tsserverlibrary.d.ts index ae04e45229a85..2ea0d6777095b 100644 --- a/tests/baselines/reference/api/tsserverlibrary.d.ts +++ b/tests/baselines/reference/api/tsserverlibrary.d.ts @@ -27,6 +27,9 @@ declare namespace ts { interface MapLike { [index: string]: T; } + interface SortedReadonlyArray extends ReadonlyArray { + " __sortedArrayBrand": any; + } interface SortedArray extends Array { " __sortedArrayBrand": any; } @@ -3126,7 +3129,7 @@ declare namespace ts { /** Non-internal stuff goes here */ declare namespace ts { function isExternalModuleNameRelative(moduleName: string): boolean; - function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): T[]; + function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): SortedReadonlyArray; } declare namespace ts { function getDefaultLibFileName(options: CompilerOptions): string; @@ -4153,7 +4156,7 @@ declare namespace ts { function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string | undefined; function resolveTripleslashReference(moduleName: string, containingFile: string): string; function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost; - function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; + function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; interface FormatDiagnosticsHost { getCurrentDirectory(): string; getCanonicalFileName(fileName: string): string; @@ -4457,9 +4460,6 @@ declare namespace ts.server { type EventBeginInstallTypes = "event::beginInstallTypes"; type EventEndInstallTypes = "event::endInstallTypes"; type EventInitializationFailed = "event::initializationFailed"; - interface SortedReadonlyArray extends ReadonlyArray { - " __sortedArrayBrand": any; - } interface TypingInstallerResponse { readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; } diff --git a/tests/baselines/reference/api/typescript.d.ts b/tests/baselines/reference/api/typescript.d.ts index fd4f575261400..91c8c3975017f 100644 --- a/tests/baselines/reference/api/typescript.d.ts +++ b/tests/baselines/reference/api/typescript.d.ts @@ -27,6 +27,9 @@ declare namespace ts { interface MapLike { [index: string]: T; } + interface SortedReadonlyArray extends ReadonlyArray { + " __sortedArrayBrand": any; + } interface SortedArray extends Array { " __sortedArrayBrand": any; } @@ -3126,7 +3129,7 @@ declare namespace ts { /** Non-internal stuff goes here */ declare namespace ts { function isExternalModuleNameRelative(moduleName: string): boolean; - function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): T[]; + function sortAndDeduplicateDiagnostics(diagnostics: ReadonlyArray): SortedReadonlyArray; } declare namespace ts { function getDefaultLibFileName(options: CompilerOptions): string; @@ -4153,7 +4156,7 @@ declare namespace ts { function findConfigFile(searchPath: string, fileExists: (fileName: string) => boolean, configName?: string): string | undefined; function resolveTripleslashReference(moduleName: string, containingFile: string): string; function createCompilerHost(options: CompilerOptions, setParentNodes?: boolean): CompilerHost; - function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[]; + function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray; interface FormatDiagnosticsHost { getCurrentDirectory(): string; getCanonicalFileName(fileName: string): string; @@ -4457,9 +4460,6 @@ declare namespace ts.server { type EventBeginInstallTypes = "event::beginInstallTypes"; type EventEndInstallTypes = "event::endInstallTypes"; type EventInitializationFailed = "event::initializationFailed"; - interface SortedReadonlyArray extends ReadonlyArray { - " __sortedArrayBrand": any; - } interface TypingInstallerResponse { readonly kind: ActionSet | ActionInvalidate | EventTypesRegistry | ActionPackageInstalled | ActionValueInspected | EventBeginInstallTypes | EventEndInstallTypes | EventInitializationFailed; }