Skip to content

Commit 179a3e1

Browse files
author
Andy
authored
Handle depth in all readDirectory implementations (#16646)
1 parent 8b7402f commit 179a3e1

18 files changed

+88
-76
lines changed

src/compiler/commandLineParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ namespace ts {
19921992
}
19931993

19941994
if (include && include.length > 0) {
1995-
for (const file of host.readDirectory(basePath, supportedExtensions, exclude, include)) {
1995+
for (const file of host.readDirectory(basePath, supportedExtensions, exclude, include, /*depth*/ undefined)) {
19961996
// If we have already included a literal or wildcard path with a
19971997
// higher priority extension, we should skip this file.
19981998
//

src/compiler/core.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
/// <reference path="performance.ts" />
33

44
namespace ts {
5+
export const versionMajorMinor = "2.5";
56
/** The version of the TypeScript compiler release */
6-
export const version = "2.5.0";
7+
export const version = `${versionMajorMinor}.0`;
78
}
89

910
/* @internal */
@@ -2016,7 +2017,7 @@ namespace ts {
20162017
};
20172018
}
20182019

2019-
export function matchFiles(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string, getFileSystemEntries: (path: string) => FileSystemEntries): string[] {
2020+
export function matchFiles(path: string, extensions: string[], excludes: string[], includes: string[], useCaseSensitiveFileNames: boolean, currentDirectory: string, depth: number | undefined, getFileSystemEntries: (path: string) => FileSystemEntries): string[] {
20202021
path = normalizePath(path);
20212022
currentDirectory = normalizePath(currentDirectory);
20222023

@@ -2033,15 +2034,14 @@ namespace ts {
20332034

20342035
const comparer = useCaseSensitiveFileNames ? compareStrings : compareStringsCaseInsensitive;
20352036
for (const basePath of patterns.basePaths) {
2036-
visitDirectory(basePath, combinePaths(currentDirectory, basePath));
2037+
visitDirectory(basePath, combinePaths(currentDirectory, basePath), depth);
20372038
}
20382039

20392040
return flatten(results);
20402041

2041-
function visitDirectory(path: string, absolutePath: string) {
2042+
function visitDirectory(path: string, absolutePath: string, depth: number | undefined) {
20422043
let { files, directories } = getFileSystemEntries(path);
20432044
files = files.slice().sort(comparer);
2044-
directories = directories.slice().sort(comparer);
20452045

20462046
for (const current of files) {
20472047
const name = combinePaths(path, current);
@@ -2059,12 +2059,20 @@ namespace ts {
20592059
}
20602060
}
20612061

2062+
if (depth !== undefined) {
2063+
depth--;
2064+
if (depth === 0) {
2065+
return;
2066+
}
2067+
}
2068+
2069+
directories = directories.slice().sort(comparer);
20622070
for (const current of directories) {
20632071
const name = combinePaths(path, current);
20642072
const absoluteName = combinePaths(absolutePath, current);
20652073
if ((!includeDirectoryRegex || includeDirectoryRegex.test(absoluteName)) &&
20662074
(!excludeRegex || !excludeRegex.test(absoluteName))) {
2067-
visitDirectory(name, absoluteName);
2075+
visitDirectory(name, absoluteName, depth);
20682076
}
20692077
}
20702078
}

src/compiler/sys.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ namespace ts {
3333
getExecutingFilePath(): string;
3434
getCurrentDirectory(): string;
3535
getDirectories(path: string): string[];
36-
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[];
36+
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[];
3737
getModifiedTime?(path: string): Date;
3838
/**
3939
* This should be cryptographically secure.
@@ -281,8 +281,8 @@ namespace ts {
281281
}
282282
}
283283

284-
function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[]): string[] {
285-
return matchFiles(path, extensions, excludes, includes, useCaseSensitiveFileNames, process.cwd(), getAccessibleFileSystemEntries);
284+
function readDirectory(path: string, extensions?: string[], excludes?: string[], includes?: string[], depth?: number): string[] {
285+
return matchFiles(path, extensions, excludes, includes, useCaseSensitiveFileNames, process.cwd(), depth, getAccessibleFileSystemEntries);
286286
}
287287

288288
const enum FileSystemEntryKind {
@@ -475,7 +475,7 @@ namespace ts {
475475
getCurrentDirectory: () => ChakraHost.currentDirectory,
476476
getDirectories: ChakraHost.getDirectories,
477477
getEnvironmentVariable: ChakraHost.getEnvironmentVariable || (() => ""),
478-
readDirectory: (path: string, extensions?: string[], excludes?: string[], includes?: string[]) => {
478+
readDirectory(path, extensions, excludes, includes, _depth) {
479479
const pattern = getFileMatcherPatterns(path, excludes, includes, !!ChakraHost.useCaseSensitiveFileNames, ChakraHost.currentDirectory);
480480
return ChakraHost.readDirectory(path, extensions, pattern.basePaths, pattern.excludePattern, pattern.includeFilePattern, pattern.includeDirectoryPattern);
481481
},

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2390,7 +2390,7 @@ namespace ts {
23902390
export interface ParseConfigHost {
23912391
useCaseSensitiveFileNames: boolean;
23922392

2393-
readDirectory(rootDir: string, extensions: string[], excludes: string[], includes: string[]): string[];
2393+
readDirectory(rootDir: string, extensions: string[], excludes: string[], includes: string[], depth: number): string[];
23942394

23952395
/**
23962396
* Gets a value indicating whether the specified path exists and is a file.

src/harness/harness.ts

Lines changed: 4 additions & 4 deletions
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[]): string[];
496+
readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[], depth?: number): string[];
497497
tryEnableSourceMapsForHost?(): void;
498498
getEnvironmentVariable?(name: string): string;
499499
}
@@ -537,7 +537,7 @@ namespace Harness {
537537
ts.sys.tryEnableSourceMapsForHost();
538538
}
539539
}
540-
export const readDirectory: typeof IO.readDirectory = (path, extension, exclude, include) => ts.sys.readDirectory(path, extension, exclude, include);
540+
export const readDirectory: typeof IO.readDirectory = (path, extension, exclude, include, depth) => ts.sys.readDirectory(path, extension, exclude, include, depth);
541541

542542
export function createDirectory(path: string) {
543543
if (!directoryExists(path)) {
@@ -733,12 +733,12 @@ namespace Harness {
733733
Http.writeToServerSync(serverRoot + path, "WRITE", contents);
734734
}
735735

736-
export function readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[]) {
736+
export function readDirectory(path: string, extension?: string[], exclude?: string[], include?: string[], depth?: number) {
737737
const fs = new Utils.VirtualFileSystem(path, useCaseSensitiveFileNames());
738738
for (const file of listFiles(path)) {
739739
fs.addFile(file);
740740
}
741-
return ts.matchFiles(path, extension, exclude, include, useCaseSensitiveFileNames(), getCurrentDirectory(), path => {
741+
return ts.matchFiles(path, extension, exclude, include, useCaseSensitiveFileNames(), getCurrentDirectory(), depth, path => {
742742
const entry = fs.traversePath(path);
743743
if (entry && entry.isDirectory()) {
744744
const directory = <Utils.VirtualDirectory>entry;

src/harness/harnessLanguageService.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,11 @@ namespace Harness.LanguageService {
208208
const script = this.getScriptSnapshot(fileName);
209209
return script !== undefined;
210210
}
211-
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[] {
211+
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[] {
212212
return ts.matchFiles(path, extensions, exclude, include,
213213
/*useCaseSensitiveFileNames*/ false,
214214
this.getCurrentDirectory(),
215+
depth,
215216
(p) => this.virtualFileSystem.getAccessibleFileSystemEntries(p));
216217
}
217218
readFile(path: string): string {
@@ -312,9 +313,7 @@ namespace Harness.LanguageService {
312313
getScriptVersion(fileName: string): string { return this.nativeHost.getScriptVersion(fileName); }
313314
getLocalizedDiagnosticMessages(): string { return JSON.stringify({}); }
314315

315-
readDirectory(_rootDir: string, _extension: string): string {
316-
return ts.notImplemented();
317-
}
316+
readDirectory = ts.notImplemented;
318317
readDirectoryNames = ts.notImplemented;
319318
readFileNames = ts.notImplemented;
320319
fileExists(fileName: string) { return this.getScriptInfo(fileName) !== undefined; }
@@ -668,9 +667,7 @@ namespace Harness.LanguageService {
668667
return ts.sys.getEnvironmentVariable(name);
669668
}
670669

671-
readDirectory(_path: string, _extension?: string[], _exclude?: string[], _include?: string[]): string[] {
672-
return ts.notImplemented();
673-
}
670+
readDirectory() { return ts.notImplemented(); }
674671

675672
watchFile(): ts.FileWatcher {
676673
return { close: ts.noop };

src/harness/loggedIO.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ interface IOLog {
6767
extensions: string[],
6868
exclude: string[],
6969
include: string[],
70+
depth: number,
7071
result: string[]
7172
}[];
7273
}
@@ -220,10 +221,9 @@ namespace Playback {
220221
memoize(path => findFileByPath(replayLog.filesRead, path, /*throwFileNotFoundError*/ true).contents));
221222

222223
wrapper.readDirectory = recordReplay(wrapper.readDirectory, underlying)(
223-
(path, extensions, exclude, include) => {
224-
const result = (<ts.System>underlying).readDirectory(path, extensions, exclude, include);
225-
const logEntry = { path, extensions, exclude, include, result };
226-
recordLog.directoriesRead.push(logEntry);
224+
(path, extensions, exclude, include, depth) => {
225+
const result = (<ts.System>underlying).readDirectory(path, extensions, exclude, include, depth);
226+
recordLog.directoriesRead.push({ path, extensions, exclude, include, depth, result });
227227
return result;
228228
},
229229
path => {

src/harness/projectsRunner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ class ProjectRunner extends RunnerBase {
275275
: ts.normalizeSlashes(testCase.projectRoot) + "/" + ts.normalizeSlashes(fileName);
276276
}
277277

278-
function readDirectory(rootDir: string, extension: string[], exclude: string[], include: string[]): string[] {
279-
const harnessReadDirectoryResult = Harness.IO.readDirectory(getFileNameInTheProjectTest(rootDir), extension, exclude, include);
278+
function readDirectory(rootDir: string, extension: string[], exclude: string[], include: string[], depth: number): string[] {
279+
const harnessReadDirectoryResult = Harness.IO.readDirectory(getFileNameInTheProjectTest(rootDir), extension, exclude, include, depth);
280280
const result: string[] = [];
281281
for (let i = 0; i < harnessReadDirectoryResult.length; i++) {
282282
result[i] = ts.getRelativePathToDirectoryOrUrl(testCase.projectRoot, harnessReadDirectoryResult[i],

src/harness/unittests/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ts.server {
1919
getExecutingFilePath(): string { return void 0; },
2020
getCurrentDirectory(): string { return void 0; },
2121
getEnvironmentVariable(): string { return ""; },
22-
readDirectory(): string[] { return []; },
22+
readDirectory() { return []; },
2323
exit: noop,
2424
setTimeout() { return 0; },
2525
clearTimeout: noop,

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -438,14 +438,13 @@ namespace ts.projectSystem {
438438
}
439439
}
440440

441-
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[]): string[] {
442-
const that = this;
443-
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), (dir) => {
441+
readDirectory(path: string, extensions?: string[], exclude?: string[], include?: string[], depth?: number): string[] {
442+
return ts.matchFiles(path, extensions, exclude, include, this.useCaseSensitiveFileNames, this.getCurrentDirectory(), depth, (dir) => {
444443
const result: FileSystemEntries = {
445444
directories: [],
446445
files: []
447446
};
448-
const dirEntry = that.fs.get(that.toPath(dir));
447+
const dirEntry = this.fs.get(this.toPath(dir));
449448
if (isFolder(dirEntry)) {
450449
dirEntry.entries.forEach((entry) => {
451450
if (isFolder(entry)) {

0 commit comments

Comments
 (0)