Skip to content

Commit e2141ad

Browse files
author
Andy
authored
Mark some arrays as readonly (#17725)
* Mark some arrays as readonly * Avoid unnecessary allocations and style changes * Fix lint
1 parent 2fede09 commit e2141ad

24 files changed

+133
-124
lines changed

src/compiler/comments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace ts {
2121
let declarationListContainerEnd = -1;
2222
let currentSourceFile: SourceFile;
2323
let currentText: string;
24-
let currentLineMap: number[];
24+
let currentLineMap: ReadonlyArray<number>;
2525
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[];
2626
let hasWrittenComment = false;
2727
let disabled: boolean = printerOptions.removeComments;

src/compiler/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ namespace ts {
715715
return result;
716716
}
717717

718-
export function sum<T extends Record<K, number>, K extends string>(array: T[], prop: K): number {
718+
export function sum<T extends Record<K, number>, K extends string>(array: ReadonlyArray<T>, prop: K): number {
719719
let result = 0;
720720
for (const v of array) {
721721
// Note: we need the following type assertion because of GH #17069

src/compiler/declarationEmitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace ts {
6060
let enclosingDeclaration: Node;
6161
let resultHasExternalModuleIndicator: boolean;
6262
let currentText: string;
63-
let currentLineMap: number[];
63+
let currentLineMap: ReadonlyArray<number>;
6464
let currentIdentifiers: Map<string>;
6565
let isCurrentFileExternalModule: boolean;
6666
let reportedDeclarationError = false;

src/compiler/factory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,13 +2339,13 @@ namespace ts {
23392339
: node;
23402340
}
23412341

2342-
export function createBundle(sourceFiles: SourceFile[]) {
2342+
export function createBundle(sourceFiles: ReadonlyArray<SourceFile>) {
23432343
const node = <Bundle>createNode(SyntaxKind.Bundle);
23442344
node.sourceFiles = sourceFiles;
23452345
return node;
23462346
}
23472347

2348-
export function updateBundle(node: Bundle, sourceFiles: SourceFile[]) {
2348+
export function updateBundle(node: Bundle, sourceFiles: ReadonlyArray<SourceFile>) {
23492349
if (node.sourceFiles !== sourceFiles) {
23502350
return createBundle(sourceFiles);
23512351
}

src/compiler/program.ts

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,15 @@ namespace ts {
205205
}
206206

207207
export function getPreEmitDiagnostics(program: Program, sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[] {
208-
let diagnostics = program.getOptionsDiagnostics(cancellationToken).concat(
209-
program.getSyntacticDiagnostics(sourceFile, cancellationToken),
210-
program.getGlobalDiagnostics(cancellationToken),
211-
program.getSemanticDiagnostics(sourceFile, cancellationToken));
208+
const diagnostics = [
209+
...program.getOptionsDiagnostics(cancellationToken),
210+
...program.getSyntacticDiagnostics(sourceFile, cancellationToken),
211+
...program.getGlobalDiagnostics(cancellationToken),
212+
...program.getSemanticDiagnostics(sourceFile, cancellationToken)
213+
];
212214

213215
if (program.getCompilerOptions().declaration) {
214-
diagnostics = diagnostics.concat(program.getDeclarationDiagnostics(sourceFile, cancellationToken));
216+
addRange(diagnostics, program.getDeclarationDiagnostics(sourceFile, cancellationToken));
215217
}
216218

217219
return sortAndDeduplicateDiagnostics(diagnostics);
@@ -223,7 +225,7 @@ namespace ts {
223225
getNewLine(): string;
224226
}
225227

226-
export function formatDiagnostics(diagnostics: Diagnostic[], host: FormatDiagnosticsHost): string {
228+
export function formatDiagnostics(diagnostics: ReadonlyArray<Diagnostic>, host: FormatDiagnosticsHost): string {
227229
let output = "";
228230

229231
for (const diagnostic of diagnostics) {
@@ -399,7 +401,7 @@ namespace ts {
399401
* @param oldProgram - Reuses an old program structure.
400402
* @returns A 'Program' object.
401403
*/
402-
export function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program {
404+
export function createProgram(rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: Program): Program {
403405
let program: Program;
404406
let files: SourceFile[] = [];
405407
let commonSourceDirectory: string;
@@ -996,7 +998,7 @@ namespace ts {
996998
}
997999

9981000
function emitWorker(program: Program, sourceFile: SourceFile, writeFileCallback: WriteFileCallback, cancellationToken: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult {
999-
let declarationDiagnostics: Diagnostic[] = [];
1001+
let declarationDiagnostics: ReadonlyArray<Diagnostic> = [];
10001002

10011003
if (options.noEmit) {
10021004
return { diagnostics: declarationDiagnostics, sourceMaps: undefined, emittedFiles: undefined, emitSkipped: true };
@@ -1006,10 +1008,12 @@ namespace ts {
10061008
// immediately bail out. Note that we pass 'undefined' for 'sourceFile' so that we
10071009
// get any preEmit diagnostics, not just the ones
10081010
if (options.noEmitOnError) {
1009-
const diagnostics = program.getOptionsDiagnostics(cancellationToken).concat(
1010-
program.getSyntacticDiagnostics(sourceFile, cancellationToken),
1011-
program.getGlobalDiagnostics(cancellationToken),
1012-
program.getSemanticDiagnostics(sourceFile, cancellationToken));
1011+
const diagnostics = [
1012+
...program.getOptionsDiagnostics(cancellationToken),
1013+
...program.getSyntacticDiagnostics(sourceFile, cancellationToken),
1014+
...program.getGlobalDiagnostics(cancellationToken),
1015+
...program.getSemanticDiagnostics(sourceFile, cancellationToken)
1016+
];
10131017

10141018
if (diagnostics.length === 0 && program.getCompilerOptions().declaration) {
10151019
declarationDiagnostics = program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken);
@@ -1060,8 +1064,8 @@ namespace ts {
10601064

10611065
function getDiagnosticsHelper(
10621066
sourceFile: SourceFile,
1063-
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => Diagnostic[],
1064-
cancellationToken: CancellationToken): Diagnostic[] {
1067+
getDiagnostics: (sourceFile: SourceFile, cancellationToken: CancellationToken) => ReadonlyArray<Diagnostic>,
1068+
cancellationToken: CancellationToken): ReadonlyArray<Diagnostic> {
10651069
if (sourceFile) {
10661070
return getDiagnostics(sourceFile, cancellationToken);
10671071
}
@@ -1073,15 +1077,15 @@ namespace ts {
10731077
}));
10741078
}
10751079

1076-
function getSyntacticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
1080+
function getSyntacticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray<Diagnostic> {
10771081
return getDiagnosticsHelper(sourceFile, getSyntacticDiagnosticsForFile, cancellationToken);
10781082
}
10791083

1080-
function getSemanticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
1084+
function getSemanticDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray<Diagnostic> {
10811085
return getDiagnosticsHelper(sourceFile, getSemanticDiagnosticsForFile, cancellationToken);
10821086
}
10831087

1084-
function getDeclarationDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
1088+
function getDeclarationDiagnostics(sourceFile: SourceFile, cancellationToken: CancellationToken): ReadonlyArray<Diagnostic> {
10851089
const options = program.getCompilerOptions();
10861090
// collect diagnostics from the program only once if either no source file was specified or out/outFile is set (bundled emit)
10871091
if (!sourceFile || options.out || options.outFile) {
@@ -1092,7 +1096,7 @@ namespace ts {
10921096
}
10931097
}
10941098

1095-
function getSyntacticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
1099+
function getSyntacticDiagnosticsForFile(sourceFile: SourceFile): ReadonlyArray<Diagnostic> {
10961100
// For JavaScript files, we report semantic errors for using TypeScript-only
10971101
// constructs from within a JavaScript file as syntactic errors.
10981102
if (isSourceFileJavaScript(sourceFile)) {

src/compiler/scanner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ namespace ts {
343343
}
344344

345345
/* @internal */
346-
export function getLineStarts(sourceFile: SourceFileLike): number[] {
346+
export function getLineStarts(sourceFile: SourceFileLike): ReadonlyArray<number> {
347347
return sourceFile.lineMap || (sourceFile.lineMap = computeLineStarts(sourceFile.text));
348348
}
349349

src/compiler/transformer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace ts {
9292
* @param transforms An array of `TransformerFactory` callbacks.
9393
* @param allowDtsFiles A value indicating whether to allow the transformation of .d.ts files.
9494
*/
95-
export function transformNodes<T extends Node>(resolver: EmitResolver, host: EmitHost, options: CompilerOptions, nodes: T[], transformers: TransformerFactory<T>[], allowDtsFiles: boolean): TransformationResult<T> {
95+
export function transformNodes<T extends Node>(resolver: EmitResolver, host: EmitHost, options: CompilerOptions, nodes: ReadonlyArray<T>, transformers: ReadonlyArray<TransformerFactory<T>>, allowDtsFiles: boolean): TransformationResult<T> {
9696
const enabledSyntaxKindFeatures = new Array<SyntaxKindFeatureFlags>(SyntaxKind.Count);
9797
let lexicalEnvironmentVariableDeclarations: VariableDeclaration[];
9898
let lexicalEnvironmentFunctionDeclarations: FunctionDeclaration[];

src/compiler/tsc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,21 +469,21 @@ namespace ts {
469469
let diagnostics: Diagnostic[];
470470

471471
// First get and report any syntactic errors.
472-
diagnostics = program.getSyntacticDiagnostics();
472+
diagnostics = program.getSyntacticDiagnostics().slice();
473473

474474
// If we didn't have any syntactic errors, then also try getting the global and
475475
// semantic errors.
476476
if (diagnostics.length === 0) {
477477
diagnostics = program.getOptionsDiagnostics().concat(program.getGlobalDiagnostics());
478478

479479
if (diagnostics.length === 0) {
480-
diagnostics = program.getSemanticDiagnostics();
480+
diagnostics = program.getSemanticDiagnostics().slice();
481481
}
482482
}
483483

484484
// Otherwise, emit and report any errors we ran into.
485485
const emitOutput = program.emit();
486-
diagnostics = diagnostics.concat(emitOutput.diagnostics);
486+
addRange(diagnostics, emitOutput.diagnostics);
487487

488488
reportDiagnostics(sortAndDeduplicateDiagnostics(diagnostics), compilerHost);
489489

src/compiler/types.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2254,7 +2254,7 @@ namespace ts {
22542254
*/
22552255
export interface SourceFileLike {
22562256
readonly text: string;
2257-
lineMap: number[];
2257+
lineMap: ReadonlyArray<number>;
22582258
}
22592259

22602260

@@ -2286,16 +2286,16 @@ namespace ts {
22862286
*/
22872287
/* @internal */ redirectInfo?: RedirectInfo | undefined;
22882288

2289-
amdDependencies: AmdDependency[];
2289+
amdDependencies: ReadonlyArray<AmdDependency>;
22902290
moduleName: string;
2291-
referencedFiles: FileReference[];
2292-
typeReferenceDirectives: FileReference[];
2291+
referencedFiles: ReadonlyArray<FileReference>;
2292+
typeReferenceDirectives: ReadonlyArray<FileReference>;
22932293
languageVariant: LanguageVariant;
22942294
isDeclarationFile: boolean;
22952295

22962296
// this map is used by transpiler to supply alternative names for dependencies (i.e. in case of bundling)
22972297
/* @internal */
2298-
renamedDependencies?: Map<string>;
2298+
renamedDependencies?: ReadonlyMap<string>;
22992299

23002300
/**
23012301
* lib.d.ts should have a reference comment like
@@ -2331,12 +2331,12 @@ namespace ts {
23312331
/* @internal */ jsDocDiagnostics?: Diagnostic[];
23322332

23332333
// Stores additional file-level diagnostics reported by the program
2334-
/* @internal */ additionalSyntacticDiagnostics?: Diagnostic[];
2334+
/* @internal */ additionalSyntacticDiagnostics?: ReadonlyArray<Diagnostic>;
23352335

23362336
// Stores a line map for the file.
23372337
// This field should never be used directly to obtain line map, use getLineMap function instead.
2338-
/* @internal */ lineMap: number[];
2339-
/* @internal */ classifiableNames?: UnderscoreEscapedMap<true>;
2338+
/* @internal */ lineMap: ReadonlyArray<number>;
2339+
/* @internal */ classifiableNames?: ReadonlyUnderscoreEscapedMap<true>;
23402340
// Stores a mapping 'external module reference text' -> 'resolved file name' | undefined
23412341
// It is used to resolve module names in the checker.
23422342
// Content of this field should never be used directly - use getResolvedModuleFileName/setResolvedModuleFileName functions instead
@@ -2351,7 +2351,7 @@ namespace ts {
23512351

23522352
export interface Bundle extends Node {
23532353
kind: SyntaxKind.Bundle;
2354-
sourceFiles: SourceFile[];
2354+
sourceFiles: ReadonlyArray<SourceFile>;
23552355
}
23562356

23572357
export interface JsonSourceFile extends SourceFile {
@@ -2398,19 +2398,19 @@ namespace ts {
23982398
/**
23992399
* Get a list of root file names that were passed to a 'createProgram'
24002400
*/
2401-
getRootFileNames(): string[];
2401+
getRootFileNames(): ReadonlyArray<string>;
24022402

24032403
/**
24042404
* Get a list of files in the program
24052405
*/
2406-
getSourceFiles(): SourceFile[];
2406+
getSourceFiles(): ReadonlyArray<SourceFile>;
24072407

24082408
/**
24092409
* Get a list of file names that were passed to 'createProgram' or referenced in a
24102410
* program source file but could not be located.
24112411
*/
24122412
/* @internal */
2413-
getMissingFilePaths(): Path[];
2413+
getMissingFilePaths(): ReadonlyArray<Path>;
24142414

24152415
/**
24162416
* Emits the JavaScript and declaration files. If targetSourceFile is not specified, then
@@ -2424,11 +2424,11 @@ namespace ts {
24242424
*/
24252425
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
24262426

2427-
getOptionsDiagnostics(cancellationToken?: CancellationToken): Diagnostic[];
2428-
getGlobalDiagnostics(cancellationToken?: CancellationToken): Diagnostic[];
2429-
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
2430-
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
2431-
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): Diagnostic[];
2427+
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
2428+
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
2429+
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
2430+
getSemanticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
2431+
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
24322432

24332433
/**
24342434
* Gets a type checker that can be used to semantically analyze source files in the program.
@@ -2522,7 +2522,7 @@ namespace ts {
25222522
export interface EmitResult {
25232523
emitSkipped: boolean;
25242524
/** Contains declaration emit diagnostics */
2525-
diagnostics: Diagnostic[];
2525+
diagnostics: ReadonlyArray<Diagnostic>;
25262526
emittedFiles: string[]; // Array of files the compiler wrote to disk
25272527
/* @internal */ sourceMaps: SourceMapData[]; // Array of sourceMapData if compiler emitted sourcemaps
25282528
}
@@ -2531,9 +2531,9 @@ namespace ts {
25312531
export interface TypeCheckerHost {
25322532
getCompilerOptions(): CompilerOptions;
25332533

2534-
getSourceFiles(): SourceFile[];
2534+
getSourceFiles(): ReadonlyArray<SourceFile>;
25352535
getSourceFile(fileName: string): SourceFile;
2536-
getResolvedTypeReferenceDirectives(): Map<ResolvedTypeReferenceDirective>;
2536+
getResolvedTypeReferenceDirectives(): ReadonlyMap<ResolvedTypeReferenceDirective>;
25372537
}
25382538

25392539
export interface TypeChecker {
@@ -4137,7 +4137,7 @@ namespace ts {
41374137
export interface SourceMapSource {
41384138
fileName: string;
41394139
text: string;
4140-
/* @internal */ lineMap: number[];
4140+
/* @internal */ lineMap: ReadonlyArray<number>;
41414141
skipTrivia?: (pos: number) => number;
41424142
}
41434143

@@ -4244,7 +4244,7 @@ namespace ts {
42444244

42454245
/* @internal */
42464246
export interface EmitHost extends ScriptReferenceHost {
4247-
getSourceFiles(): SourceFile[];
4247+
getSourceFiles(): ReadonlyArray<SourceFile>;
42484248

42494249
/* @internal */
42504250
isSourceFileFromExternalLibrary(file: SourceFile): boolean;

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2572,7 +2572,7 @@ namespace ts {
25722572
* @param host An EmitHost.
25732573
* @param targetSourceFile An optional target source file to emit.
25742574
*/
2575-
export function getSourceFilesToEmit(host: EmitHost, targetSourceFile?: SourceFile): SourceFile[] {
2575+
export function getSourceFilesToEmit(host: EmitHost, targetSourceFile?: SourceFile): ReadonlyArray<SourceFile> {
25762576
const options = host.getCompilerOptions();
25772577
const isSourceFileFromExternalLibrary = (file: SourceFile) => host.isSourceFileFromExternalLibrary(file);
25782578
if (options.outFile || options.out) {

0 commit comments

Comments
 (0)