Skip to content

Commit e41b10b

Browse files
committed
add test and spit commandLineParser changes to another PR
1 parent 4383f1a commit e41b10b

File tree

7 files changed

+65
-69
lines changed

7 files changed

+65
-69
lines changed

src/compiler/commandLineParser.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -727,22 +727,10 @@ namespace ts {
727727
const supportedExtensions = getSupportedExtensions(options);
728728
Debug.assert(indexOf(supportedExtensions, ".ts") < indexOf(supportedExtensions, ".d.ts"), "Changed priority of extensions to pick");
729729

730-
const potentialFiles: string[] = [];
731-
if (host.readDirectoryWithMultipleExtensions) {
732-
addRange(potentialFiles, host.readDirectoryWithMultipleExtensions(basePath, supportedExtensions, exclude));
733-
}
734-
else {
735-
for (const extension of supportedExtensions) {
736-
addRange(potentialFiles, host.readDirectory(basePath, extension, exclude));
737-
}
738-
}
739730
// Get files of supported extensions in their order of resolution
740731
for (const extension of supportedExtensions) {
741-
for (const fileName of potentialFiles) {
742-
if (!fileExtensionIs(fileName, extension)) {
743-
continue;
744-
}
745-
732+
const filesInDirWithExtension = host.readDirectory(basePath, extension, exclude);
733+
for (const fileName of filesInDirWithExtension) {
746734
// .ts extension would read the .d.ts extension files too but since .d.ts is lower priority extension,
747735
// lets pick them when its turn comes up
748736
if (extension === ".ts" && fileExtensionIs(fileName, ".d.ts")) {

src/compiler/sys.ts

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ namespace ts {
2727
getCurrentDirectory(): string;
2828
getDirectories(path: string): string[];
2929
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
30-
readDirectoryWithMultipleExtensions?(path: string, extensions: string[], exclude?: string[]): string[];
3130
getModifiedTime?(path: string): Date;
3231
createHash?(data: string): string;
3332
getMemoryUsage?(): number;
@@ -416,64 +415,39 @@ namespace ts {
416415
return filter<string>(_fs.readdirSync(path), p => fileSystemEntryExists(combinePaths(path, p), FileSystemEntryKind.Directory));
417416
}
418417

419-
function visitDirectory(path: string, result: string[], extension: string | string[], exclude: string[]) {
420-
const files = _fs.readdirSync(path || ".").sort();
421-
const directories: string[] = [];
422-
for (const current of files) {
418+
function readDirectory(path: string, extension?: string, exclude?: string[]): string[] {
419+
const result: string[] = [];
420+
exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s)));
421+
visitDirectory(path);
422+
return result;
423+
function visitDirectory(path: string) {
424+
const files = _fs.readdirSync(path || ".").sort();
425+
const directories: string[] = [];
426+
for (const current of files) {
423427
// This is necessary because on some file system node fails to exclude
424428
// "." and "..". See https://github.com/nodejs/node/issues/4002
425429
if (current === "." || current === "..") {
426430
continue;
427431
}
428-
const name = combinePaths(path, current);
429-
if (!contains(exclude, getCanonicalPath(name))) {
430-
// fs.statSync would throw an exception if the file is a symlink
431-
// whose linked file doesn't exist.
432-
try {
432+
const name = combinePaths(path, current);
433+
if (!contains(exclude, getCanonicalPath(name))) {
433434
const stat = _fs.statSync(name);
434435
if (stat.isFile()) {
435-
if (checkExtension(name)) {
436+
if (!extension || fileExtensionIs(name, extension)) {
436437
result.push(name);
437438
}
438439
}
439440
else if (stat.isDirectory()) {
440441
directories.push(name);
441442
}
442443
}
443-
catch (e) { }
444-
}
445-
}
446-
for (const current of directories) {
447-
visitDirectory(current, result, extension, exclude);
448-
}
449-
450-
function checkExtension(name: string) {
451-
if (!extension) {
452-
return true;
453444
}
454-
if (typeof extension === "string") {
455-
return fileExtensionIs(name, extension);
456-
}
457-
else {
458-
return forEach(extension, ext => fileExtensionIs(name, ext));
445+
for (const current of directories) {
446+
visitDirectory(current);
459447
}
460448
}
461449
}
462450

463-
function readDirectoryWithMultipleExtensions(path: string, extensions: string[], exclude?: string[]): string[] {
464-
const result: string[] = [];
465-
exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s)));
466-
visitDirectory(path, result, extensions, exclude);
467-
return result;
468-
}
469-
470-
function readDirectory(path: string, extension?: string, exclude?: string[]): string[] {
471-
const result: string[] = [];
472-
exclude = map(exclude, s => getCanonicalPath(combinePaths(path, s)));
473-
visitDirectory(path, result, extension, exclude);
474-
return result;
475-
}
476-
477451
return {
478452
args: process.argv.slice(2),
479453
newLine: _os.EOL,
@@ -548,7 +522,6 @@ namespace ts {
548522
},
549523
getDirectories,
550524
readDirectory,
551-
readDirectoryWithMultipleExtensions,
552525
getModifiedTime(path) {
553526
try {
554527
return _fs.statSync(path).mtime;

src/compiler/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1670,7 +1670,6 @@ namespace ts {
16701670

16711671
export interface ParseConfigHost {
16721672
readDirectory(rootDir: string, extension: string, exclude: string[]): string[];
1673-
readDirectoryWithMultipleExtensions?(rootDir: string, extensions: string[], exclude: string[]): string[];
16741673
}
16751674

16761675
export interface WriteFileCallback {

src/server/editorServices.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,12 +1184,10 @@ namespace ts.server {
11841184
* @param fileContent is a known version of the file content that is more up to date than the one on disk
11851185
*/
11861186
openClientFile(fileName: string, fileContent?: string, scriptKind?: ScriptKind): { configFileName?: string, configFileErrors?: Diagnostic[] } {
1187-
this.log("start openClientFile: " + new Date().getTime());
11881187
const { configFileName, configFileErrors } = this.openOrUpdateConfiguredProjectForFile(fileName);
11891188
const info = this.openFile(fileName, /*openedByClient*/ true, fileContent, scriptKind);
11901189
this.addOpenFile(info);
11911190
this.printProjects();
1192-
this.log("end openClientFile: " + new Date().getTime());
11931191
return { configFileName, configFileErrors };
11941192
}
11951193

@@ -1199,7 +1197,6 @@ namespace ts.server {
11991197
* the tsconfig file content and update the project; otherwise we create a new one.
12001198
*/
12011199
openOrUpdateConfiguredProjectForFile(fileName: string): { configFileName?: string, configFileErrors?: Diagnostic[] } {
1202-
this.log("start openOrUpdateConfiguredProjectForFile: " + new Date().getTime());
12031200
const searchPath = ts.normalizePath(getDirectoryPath(fileName));
12041201
this.log("Search path: " + searchPath, "Info");
12051202
const configFileName = this.findConfigFile(searchPath);
@@ -1228,7 +1225,6 @@ namespace ts.server {
12281225
else {
12291226
this.log("No config files found.");
12301227
}
1231-
this.log("end openOrUpdateConfiguredProjectForFile: " + new Date().getTime());
12321228
return configFileName ? { configFileName } : {};
12331229
}
12341230

@@ -1368,11 +1364,8 @@ namespace ts.server {
13681364
}
13691365

13701366
openConfigFile(configFilename: string, clientFileName?: string): { success: boolean, project?: Project, errors?: Diagnostic[] } {
1371-
this.log("start openConfigFile: " + new Date().getTime());
13721367
const { succeeded, projectOptions, errors } = this.configFileToProjectOptions(configFilename);
1373-
this.log("finish reading config file: " + new Date().getTime());
13741368
if (!succeeded) {
1375-
this.log("finish openConfigFile: " + new Date().getTime());
13761369
return { success: false, errors };
13771370
}
13781371
else {
@@ -1408,7 +1401,6 @@ namespace ts.server {
14081401
path => this.directoryWatchedForSourceFilesChanged(project, path),
14091402
/*recursive*/ true
14101403
);
1411-
this.log("finish openConfigFile: " + new Date().getTime());
14121404
return { success: true, project: project, errors };
14131405
}
14141406
}

src/server/protocol.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ declare namespace ts.server.protocol {
124124
*/
125125
fileNames?: string[];
126126
/**
127-
* Indicates if the project has a active language service instance
128-
*/
127+
* Indicates if the project has a active language service instance
128+
*/
129129
languageServiceDisabled?: boolean;
130130
}
131131

src/server/session.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ namespace ts.server {
421421
}
422422

423423
private getProjectInfo(fileName: string, needFileNameList: boolean): protocol.ProjectInfo {
424-
this.logger.info("start getProjectInfo:" + new Date().getTime());
425424
fileName = ts.normalizePath(fileName);
426425
const project = this.projectService.getProjectForFile(fileName);
427426
if (!project) {
@@ -436,7 +435,6 @@ namespace ts.server {
436435
if (needFileNameList) {
437436
projectInfo.fileNames = project.getFileNames();
438437
}
439-
this.logger.info("end getProjectInfo:" + new Date().getTime());
440438
return projectInfo;
441439
}
442440

tests/cases/unittests/tsserverProjectSystem.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace ts {
2525
interface FileOrFolder {
2626
path: string;
2727
content?: string;
28+
fileSize?: number;
2829
}
2930

3031
interface FSEntry {
@@ -34,6 +35,7 @@ namespace ts {
3435

3536
interface File extends FSEntry {
3637
content: string;
38+
fileSize?: number;
3739
}
3840

3941
interface Folder extends FSEntry {
@@ -157,7 +159,7 @@ namespace ts {
157159
const path = this.toPath(fileOrFolder.path);
158160
const fullPath = getNormalizedAbsolutePath(fileOrFolder.path, this.currentDirectory);
159161
if (typeof fileOrFolder.content === "string") {
160-
const entry = { path, content: fileOrFolder.content, fullPath };
162+
const entry = { path, content: fileOrFolder.content, fullPath, fileSize: fileOrFolder.fileSize };
161163
this.fs.set(path, entry);
162164
addFolder(getDirectoryPath(fullPath), this.toPath, this.fs).entries.push(entry);
163165
}
@@ -172,6 +174,17 @@ namespace ts {
172174
return this.fs.contains(path) && isFile(this.fs.get(path));
173175
};
174176

177+
getFileSize(s: string) {
178+
const path = this.toPath(s);
179+
if (this.fs.contains(path)) {
180+
const entry = this.fs.get(path);
181+
if (isFile(entry)) {
182+
return entry.fileSize ? entry.fileSize : entry.content.length;
183+
}
184+
}
185+
return undefined;
186+
}
187+
175188
directoryExists(s: string) {
176189
const path = this.toPath(s);
177190
return this.fs.contains(path) && isFolder(this.fs.get(path));
@@ -567,5 +580,38 @@ namespace ts {
567580
checkConfiguredProjectActualFiles(project, [file1.path, classicModuleFile.path]);
568581
checkNumberOfInferredProjects(projectService, 1);
569582
});
583+
584+
it("should disable language service for project with too many non-ts files", () => {
585+
const jsFiles: FileOrFolder[] = [];
586+
const configFile: FileOrFolder = {
587+
path: `/a/b/jsconfig.json`,
588+
content: "{}"
589+
};
590+
jsFiles.push(configFile);
591+
for (let i = 0; i < 1000; i++) {
592+
jsFiles.push({
593+
path: `/a/b/file${i}.js`,
594+
content: "",
595+
fileSize: 50000
596+
});
597+
}
598+
599+
const host = new TestServerHost(/*useCaseSensitiveFileNames*/ false, getExecutingFilePathFromLibFile(libFile), "/", jsFiles);
600+
const projectService = new server.ProjectService(host, nullLogger);
601+
projectService.openClientFile(jsFiles[1].path);
602+
projectService.openClientFile(jsFiles[2].path);
603+
checkNumberOfConfiguredProjects(projectService, 1);
604+
checkNumberOfInferredProjects(projectService, 0);
605+
606+
const project = projectService.configuredProjects[0];
607+
assert(project.languageServiceDiabled, "the project's language service is expected to be disabled");
608+
609+
configFile.content = `{
610+
"files": ["/a/b/file1.js", "/a/b/file2.js", "/a/b/file3.js"]
611+
}`;
612+
host.reloadFS(jsFiles);
613+
host.triggerFileWatcherCallback(configFile.path);
614+
assert(!project.languageServiceDiabled, "after the config file change, the project's language service is expected to be enabled.");
615+
});
570616
});
571617
}

0 commit comments

Comments
 (0)