Skip to content

Commit 08030c7

Browse files
author
Andy
authored
Convert most of core.ts to accept ReadonlyArray (microsoft#17092)
* Convert most of core.ts to accept ReadonlyArray * Fix lint * Fix isArray
1 parent 25f4e46 commit 08030c7

21 files changed

+117
-106
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ namespace ts {
20102010
}
20112011

20122012
function getAccessibleSymbolChainFromSymbolTableWorker(symbols: SymbolTable, visitedSymbolTables: SymbolTable[]): Symbol[] {
2013-
if (contains(visitedSymbolTables, symbols)) {
2013+
if (contains<SymbolTable>(visitedSymbolTables, symbols)) {
20142014
return undefined;
20152015
}
20162016
visitedSymbolTables.push(symbols);
@@ -20918,7 +20918,7 @@ namespace ts {
2091820918
}
2091920919
}
2092020920

20921-
function areTypeParametersIdentical(declarations: (ClassDeclaration | InterfaceDeclaration)[], typeParameters: TypeParameter[]) {
20921+
function areTypeParametersIdentical(declarations: ReadonlyArray<ClassDeclaration | InterfaceDeclaration>, typeParameters: TypeParameter[]) {
2092220922
const maxTypeArgumentCount = length(typeParameters);
2092320923
const minTypeArgumentCount = getMinTypeArgumentCount(typeParameters);
2092420924

src/compiler/commandLineParser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,7 @@ namespace ts {
21362136
* @param extensionPriority The priority of the extension.
21372137
* @param context The expansion context.
21382138
*/
2139-
function hasFileWithHigherPriorityExtension(file: string, literalFiles: Map<string>, wildcardFiles: Map<string>, extensions: string[], keyMapper: (value: string) => string) {
2139+
function hasFileWithHigherPriorityExtension(file: string, literalFiles: Map<string>, wildcardFiles: Map<string>, extensions: ReadonlyArray<string>, keyMapper: (value: string) => string) {
21402140
const extensionPriority = getExtensionPriority(file, extensions);
21412141
const adjustedExtensionPriority = adjustExtensionPriority(extensionPriority, extensions);
21422142
for (let i = ExtensionPriority.Highest; i < adjustedExtensionPriority; i++) {
@@ -2158,7 +2158,7 @@ namespace ts {
21582158
* @param extensionPriority The priority of the extension.
21592159
* @param context The expansion context.
21602160
*/
2161-
function removeWildcardFilesWithLowerPriorityExtension(file: string, wildcardFiles: Map<string>, extensions: string[], keyMapper: (value: string) => string) {
2161+
function removeWildcardFilesWithLowerPriorityExtension(file: string, wildcardFiles: Map<string>, extensions: ReadonlyArray<string>, keyMapper: (value: string) => string) {
21622162
const extensionPriority = getExtensionPriority(file, extensions);
21632163
const nextExtensionPriority = getNextLowestExtensionPriority(extensionPriority, extensions);
21642164
for (let i = nextExtensionPriority; i < extensions.length; i++) {

src/compiler/core.ts

Lines changed: 80 additions & 67 deletions
Large diffs are not rendered by default.

src/compiler/sys.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace ts {
3939
getExecutingFilePath(): string;
4040
getCurrentDirectory(): string;
4141
getDirectories(path: string): string[];
42-
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[];
42+
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
4343
getModifiedTime?(path: string): Date;
4444
/**
4545
* This should be cryptographically secure.
@@ -100,7 +100,7 @@ namespace ts {
100100
readFile(path: string): string;
101101
writeFile(path: string, contents: string): void;
102102
getDirectories(path: string): string[];
103-
readDirectory(path: string, extensions?: string[], basePaths?: string[], excludeEx?: string, includeFileEx?: string, includeDirEx?: string): string[];
103+
readDirectory(path: string, extensions?: ReadonlyArray<string>, basePaths?: ReadonlyArray<string>, excludeEx?: string, includeFileEx?: string, includeDirEx?: string): string[];
104104
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
105105
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher;
106106
realpath(path: string): string;
@@ -287,7 +287,7 @@ namespace ts {
287287
}
288288
}
289289

290-
function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[], depth?: number): string[] {
290+
function readDirectory(path: string, extensions?: ReadonlyArray<string>, excludes?: ReadonlyArray<string>, includes?: ReadonlyArray<string>, depth?: number): string[] {
291291
return matchFiles(path, extensions, excludes, includes, useCaseSensitiveFileNames, process.cwd(), depth, getAccessibleFileSystemEntries);
292292
}
293293

src/compiler/transformers/es2015.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3579,7 +3579,7 @@ namespace ts {
35793579
// Map spans of spread expressions into their expressions and spans of other
35803580
// expressions into an array literal.
35813581
const numElements = elements.length;
3582-
const segments = flatten(
3582+
const segments = flatten<Expression>(
35833583
spanMap(elements, partitionSpread, (partition, visitPartition, _start, end) =>
35843584
visitPartition(partition, multiLine, hasTrailingComma && end === numElements)
35853585
)

src/compiler/transformers/jsx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace ts {
8888
else {
8989
// Map spans of JsxAttribute nodes into object literals and spans
9090
// of JsxSpreadAttribute nodes into expressions.
91-
const segments = flatten(
91+
const segments = flatten<Expression | ObjectLiteralExpression>(
9292
spanMap(attrs, isJsxSpreadAttribute, (attrs, isSpread) => isSpread
9393
? map(attrs, transformJsxSpreadAttributeToExpression)
9494
: createObjectLiteral(map(attrs, transformJsxAttributeToObjectLiteralElement))

src/compiler/transformers/ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ namespace ts {
13771377

13781378
const decoratorExpressions: Expression[] = [];
13791379
addRange(decoratorExpressions, map(allDecorators.decorators, transformDecorator));
1380-
addRange(decoratorExpressions, flatMap(allDecorators.parameters, transformDecoratorsOfParameter));
1380+
addRange(decoratorExpressions, flatMap<Decorator[], Expression>(allDecorators.parameters, transformDecoratorsOfParameter));
13811381
addTypeMetadata(node, container, decoratorExpressions);
13821382
return decoratorExpressions;
13831383
}

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2406,7 +2406,7 @@ namespace ts {
24062406
export interface ParseConfigHost {
24072407
useCaseSensitiveFileNames: boolean;
24082408

2409-
readDirectory(rootDir: string, extensions: string[], excludes: string[], includes: string[], depth: number): string[];
2409+
readDirectory(rootDir: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string>, includes: ReadonlyArray<string>, depth: number): string[];
24102410

24112411
/**
24122412
* Gets a value indicating whether the specified path exists and is a file.

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3334,7 +3334,7 @@ namespace ts {
33343334
}
33353335
}
33363336

3337-
return stableSort(result, (x, y) => compareValues(x[0], y[0]));
3337+
return stableSort<[number, string]>(result, (x, y) => compareValues(x[0], y[0]));
33383338
}
33393339

33403340
export function formatSyntaxKind(kind: SyntaxKind): string {

src/harness/fourslash.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ namespace FourSlash {
219219

220220
// Add input file which has matched file name with the given reference-file path.
221221
// This is necessary when resolveReference flag is specified
222-
private addMatchedInputFile(referenceFilePath: string, extensions: string[]) {
222+
private addMatchedInputFile(referenceFilePath: string, extensions: ReadonlyArray<string>) {
223223
const inputFiles = this.inputFiles;
224224
const languageServiceAdapterHost = this.languageServiceAdapterHost;
225225
if (!extensions) {
@@ -605,7 +605,7 @@ namespace FourSlash {
605605
this.verifyGoToXPlain(arg0, endMarkerNames, getDefs);
606606
}
607607
else if (ts.isArray(arg0)) {
608-
const pairs: [string | string[], string | string[]][] = arg0;
608+
const pairs: ReadonlyArray<[string | string[], string | string[]]> = arg0;
609609
for (const [start, end] of pairs) {
610610
this.verifyGoToXPlain(start, end, getDefs);
611611
}

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ namespace Harness {
493493
args(): string[];
494494
getExecutingFilePath(): string;
495495
exit(exitCode?: number): void;
496-
readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[], depth?: number): string[];
496+
readDirectory(path: string, extension?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
497497
tryEnableSourceMapsForHost?(): void;
498498
getEnvironmentVariable?(name: string): string;
499499
}

src/harness/harnessLanguageService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace Harness.LanguageService {
208208
const script = this.getScriptSnapshot(fileName);
209209
return script !== undefined;
210210
}
211-
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[] {
211+
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
212212
return ts.matchFiles(path, extensions, exclude, include,
213213
/*useCaseSensitiveFileNames*/ false,
214214
this.getCurrentDirectory(),

src/harness/loggedIO.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ interface IOLog {
6464
}[];
6565
directoriesRead: {
6666
path: string,
67-
extensions: string[],
68-
exclude: string[],
69-
include: string[],
67+
extensions: ReadonlyArray<string>,
68+
exclude: ReadonlyArray<string>,
69+
include: ReadonlyArray<string>,
7070
depth: number,
71-
result: string[]
71+
result: ReadonlyArray<string>,
7272
}[];
7373
}
7474

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -438,24 +438,22 @@ namespace ts.projectSystem {
438438
}
439439
}
440440

441-
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[] {
441+
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
442442
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), depth, (dir) => {
443-
const result: FileSystemEntries = {
444-
directories: [],
445-
files: []
446-
};
443+
const directories: string[] = [];
444+
const files: string[] = [];
447445
const dirEntry = this.fs.get(this.toPath(dir));
448446
if (isFolder(dirEntry)) {
449447
dirEntry.entries.forEach((entry) => {
450448
if (isFolder(entry)) {
451-
result.directories.push(entry.fullPath);
449+
directories.push(entry.fullPath);
452450
}
453451
else if (isFile(entry)) {
454-
result.files.push(entry.fullPath);
452+
files.push(entry.fullPath);
455453
}
456454
});
457455
}
458-
return result;
456+
return { directories, files };
459457
});
460458
}
461459

src/harness/virtualFileSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ namespace Utils {
216216
}
217217
}
218218

219-
readDirectory(path: string, extensions: string[], excludes: string[], includes: string[], depth: number) {
219+
readDirectory(path: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string>, includes: ReadonlyArray<string>, depth: number) {
220220
return ts.matchFiles(path, extensions, excludes, includes, this.useCaseSensitiveFileNames, this.currentDirectory, depth, (path: string) => this.getAccessibleFileSystemEntries(path));
221221
}
222222
}

src/server/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ namespace ts.server {
277277

278278
const referencedFilePaths = this.project.getReferencedFiles(fileInfo.scriptInfo.path);
279279
if (referencedFilePaths.length > 0) {
280-
return map(referencedFilePaths, f => this.getOrCreateFileInfo(f)).sort(ModuleBuilderFileInfo.compareFileInfos);
280+
return map<Path, ModuleBuilderFileInfo>(referencedFilePaths, f => this.getOrCreateFileInfo(f)).sort(ModuleBuilderFileInfo.compareFileInfos);
281281
}
282282
return [];
283283
}

src/server/lsHost.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ namespace ts.server {
225225
return this.host.directoryExists(path);
226226
}
227227

228-
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[] {
228+
readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[] {
229229
return this.host.readDirectory(path, extensions, exclude, include, depth);
230230
}
231231

src/services/jsTyping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts.JsTyping {
1212
directoryExists: (path: string) => boolean;
1313
fileExists: (fileName: string) => boolean;
1414
readFile: (path: string, encoding?: string) => string;
15-
readDirectory: (rootDir: string, extensions: string[], excludes: string[], includes: string[], depth?: number) => string[];
15+
readDirectory: (rootDir: string, extensions: ReadonlyArray<string>, excludes: ReadonlyArray<string>, includes: ReadonlyArray<string>, depth?: number) => string[];
1616
}
1717

1818
interface PackageJson {

src/services/pathCompletions.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ namespace ts.Completions.PathCompletions {
4747
return deduplicate(map(rootDirs, rootDirectory => combinePaths(rootDirectory, relativeDirectory)));
4848
}
4949

50-
function getCompletionEntriesForDirectoryFragmentWithRootDirs(rootDirs: string[], fragment: string, scriptPath: string, extensions: string[], includeExtensions: boolean, span: TextSpan, compilerOptions: CompilerOptions, host: LanguageServiceHost, exclude?: string): CompletionEntry[] {
50+
function getCompletionEntriesForDirectoryFragmentWithRootDirs(rootDirs: string[], fragment: string, scriptPath: string, extensions: ReadonlyArray<string>, includeExtensions: boolean, span: TextSpan, compilerOptions: CompilerOptions, host: LanguageServiceHost, exclude?: string): CompletionEntry[] {
5151
const basePath = compilerOptions.project || host.getCurrentDirectory();
5252
const ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames());
5353
const baseDirectories = getBaseDirectoriesFromRootDirs(rootDirs, basePath, scriptPath, ignoreCase);
@@ -64,7 +64,7 @@ namespace ts.Completions.PathCompletions {
6464
/**
6565
* Given a path ending at a directory, gets the completions for the path, and filters for those entries containing the basename.
6666
*/
67-
function getCompletionEntriesForDirectoryFragment(fragment: string, scriptPath: string, extensions: string[], includeExtensions: boolean, span: TextSpan, host: LanguageServiceHost, exclude?: string, result: CompletionEntry[] = []): CompletionEntry[] {
67+
function getCompletionEntriesForDirectoryFragment(fragment: string, scriptPath: string, extensions: ReadonlyArray<string>, includeExtensions: boolean, span: TextSpan, host: LanguageServiceHost, exclude?: string, result: CompletionEntry[] = []): CompletionEntry[] {
6868
if (fragment === undefined) {
6969
fragment = "";
7070
}
@@ -185,7 +185,7 @@ namespace ts.Completions.PathCompletions {
185185
return result;
186186
}
187187

188-
function getModulesForPathsPattern(fragment: string, baseUrl: string, pattern: string, fileExtensions: string[], host: LanguageServiceHost): string[] {
188+
function getModulesForPathsPattern(fragment: string, baseUrl: string, pattern: string, fileExtensions: ReadonlyArray<string>, host: LanguageServiceHost): string[] {
189189
if (host.readDirectory) {
190190
const parsed = hasZeroOrOneAsteriskCharacter(pattern) ? tryParsePattern(pattern) : undefined;
191191
if (parsed) {
@@ -500,7 +500,7 @@ namespace ts.Completions.PathCompletions {
500500
return tryIOAndConsumeErrors(host, host.getDirectories, directoryName);
501501
}
502502

503-
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: string[], exclude?: string[], include?: string[]): string[] {
503+
function tryReadDirectory(host: LanguageServiceHost, path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>): string[] {
504504
return tryIOAndConsumeErrors(host, host.readDirectory, path, extensions, exclude, include);
505505
}
506506

src/services/shims.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ namespace ts {
444444
return this.shimHost.getDefaultLibFileName(JSON.stringify(options));
445445
}
446446

447-
public readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[] {
447+
public readDirectory(path: string, extensions?: ReadonlyArray<string>, exclude?: string[], include?: string[], depth?: number): string[] {
448448
const pattern = getFileMatcherPatterns(path, exclude, include,
449449
this.shimHost.useCaseSensitiveFileNames(), this.shimHost.getCurrentDirectory());
450450
return JSON.parse(this.shimHost.readDirectory(
@@ -483,7 +483,7 @@ namespace ts {
483483
}
484484
}
485485

486-
public readDirectory(rootDir: string, extensions: string[], exclude: string[], include: string[], depth?: number): string[] {
486+
public readDirectory(rootDir: string, extensions: ReadonlyArray<string>, exclude: ReadonlyArray<string>, include: ReadonlyArray<string>, depth?: number): string[] {
487487
const pattern = getFileMatcherPatterns(rootDir, exclude, include,
488488
this.shimHost.useCaseSensitiveFileNames(), this.shimHost.getCurrentDirectory());
489489
return JSON.parse(this.shimHost.readDirectory(

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ namespace ts {
164164
* LS host can optionally implement these methods to support completions for module specifiers.
165165
* Without these methods, only completions for ambient modules will be provided.
166166
*/
167-
readDirectory?(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[];
167+
readDirectory?(path: string, extensions?: ReadonlyArray<string>, exclude?: ReadonlyArray<string>, include?: ReadonlyArray<string>, depth?: number): string[];
168168
readFile?(path: string, encoding?: string): string;
169169
fileExists?(path: string): boolean;
170170

0 commit comments

Comments
 (0)