Skip to content

Commit 9e12796

Browse files
authored
Merge pull request microsoft#11603 from zhengbli/11116
Set maxNodeModuleJsDepth for inferred projects
2 parents 0df7ba0 + 8ac22ec commit 9e12796

File tree

3 files changed

+108
-10
lines changed

3 files changed

+108
-10
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1620,7 +1620,7 @@ namespace ts.projectSystem {
16201620
const configureHostRequest = makeSessionRequest<protocol.ConfigureRequestArguments>(CommandNames.Configure, { extraFileExtensions });
16211621
session.executeCommand(configureHostRequest).response;
16221622

1623-
// HTML file still not included in the project as it is closed
1623+
// HTML file still not included in the project as it is closed
16241624
checkNumberOfProjects(projectService, { configuredProjects: 1 });
16251625
checkProjectActualFiles(projectService.configuredProjects[0], [file1.path]);
16261626

@@ -3051,4 +3051,59 @@ namespace ts.projectSystem {
30513051
service.checkNumberOfProjects({ externalProjects: 1 });
30523052
});
30533053
});
3054+
3055+
describe("maxNodeModuleJsDepth for inferred projects", () => {
3056+
it("should be set to 2 if the project has js root files", () => {
3057+
const file1: FileOrFolder = {
3058+
path: "/a/b/file1.js",
3059+
content: `var t = require("test"); t.`
3060+
};
3061+
const moduleFile: FileOrFolder = {
3062+
path: "/a/b/node_modules/test/index.js",
3063+
content: `var v = 10; module.exports = v;`
3064+
};
3065+
3066+
const host = createServerHost([file1, moduleFile]);
3067+
const projectService = createProjectService(host);
3068+
projectService.openClientFile(file1.path);
3069+
3070+
let project = projectService.inferredProjects[0];
3071+
let options = project.getCompilerOptions();
3072+
assert.isTrue(options.maxNodeModuleJsDepth === 2);
3073+
3074+
// Assert the option sticks
3075+
projectService.setCompilerOptionsForInferredProjects({ target: ScriptTarget.ES2016 });
3076+
project = projectService.inferredProjects[0];
3077+
options = project.getCompilerOptions();
3078+
assert.isTrue(options.maxNodeModuleJsDepth === 2);
3079+
});
3080+
3081+
it("should return to normal state when all js root files are removed from project", () => {
3082+
const file1 = {
3083+
path: "/a/file1.ts",
3084+
content: "let x =1;"
3085+
};
3086+
const file2 = {
3087+
path: "/a/file2.js",
3088+
content: "let x =1;"
3089+
};
3090+
3091+
const host = createServerHost([file1, file2, libFile]);
3092+
const projectService = createProjectService(host, { useSingleInferredProject: true });
3093+
3094+
projectService.openClientFile(file1.path);
3095+
checkNumberOfInferredProjects(projectService, 1);
3096+
let project = projectService.inferredProjects[0];
3097+
assert.isUndefined(project.getCompilerOptions().maxNodeModuleJsDepth);
3098+
3099+
projectService.openClientFile(file2.path);
3100+
project = projectService.inferredProjects[0];
3101+
assert.isTrue(project.getCompilerOptions().maxNodeModuleJsDepth === 2);
3102+
3103+
projectService.closeClientFile(file2.path);
3104+
project = projectService.inferredProjects[0];
3105+
assert.isUndefined(project.getCompilerOptions().maxNodeModuleJsDepth);
3106+
});
3107+
});
3108+
30543109
}

src/server/project.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@ namespace ts.server {
396396
}
397397

398398
removeFile(info: ScriptInfo, detachFromProject = true) {
399-
this.removeRootFileIfNecessary(info);
399+
if (this.isRoot(info)) {
400+
this.removeRoot(info);
401+
}
400402
this.lsHost.notifyFileRemoved(info);
401403
this.cachedUnresolvedImportsPerFile.remove(info.path);
402404

@@ -567,9 +569,6 @@ namespace ts.server {
567569

568570
setCompilerOptions(compilerOptions: CompilerOptions) {
569571
if (compilerOptions) {
570-
if (this.projectKind === ProjectKind.Inferred) {
571-
compilerOptions.allowJs = true;
572-
}
573572
compilerOptions.allowNonTsExtensions = true;
574573
if (changesAffectModuleResolution(this.compilerOptions, compilerOptions)) {
575574
// reset cached unresolved imports if changes in compiler options affected module resolution
@@ -698,11 +697,9 @@ namespace ts.server {
698697
}
699698

700699
// remove a root file from project
701-
private removeRootFileIfNecessary(info: ScriptInfo): void {
702-
if (this.isRoot(info)) {
703-
remove(this.rootFiles, info);
704-
this.rootFilesMap.remove(info.path);
705-
}
700+
protected removeRoot(info: ScriptInfo): void {
701+
remove(this.rootFiles, info);
702+
this.rootFilesMap.remove(info.path);
706703
}
707704
}
708705

@@ -717,6 +714,32 @@ namespace ts.server {
717714
}
718715
})();
719716

717+
private _isJsInferredProject = false;
718+
719+
toggleJsInferredProject(isJsInferredProject: boolean) {
720+
if (isJsInferredProject !== this._isJsInferredProject) {
721+
this._isJsInferredProject = isJsInferredProject;
722+
this.setCompilerOptions();
723+
}
724+
}
725+
726+
setCompilerOptions(options?: CompilerOptions) {
727+
// Avoid manipulating the given options directly
728+
const newOptions = options ? clone(options) : this.getCompilerOptions();
729+
if (!newOptions) {
730+
return;
731+
}
732+
733+
if (this._isJsInferredProject && typeof newOptions.maxNodeModuleJsDepth !== "number") {
734+
newOptions.maxNodeModuleJsDepth = 2;
735+
}
736+
else if (!this._isJsInferredProject) {
737+
newOptions.maxNodeModuleJsDepth = undefined;
738+
}
739+
newOptions.allowJs = true;
740+
super.setCompilerOptions(newOptions);
741+
}
742+
720743
// Used to keep track of what directories are watched for this project
721744
directoriesWatchedForTsconfig: string[] = [];
722745

@@ -731,6 +754,22 @@ namespace ts.server {
731754
/*compileOnSaveEnabled*/ false);
732755
}
733756

757+
addRoot(info: ScriptInfo) {
758+
if (!this._isJsInferredProject && info.isJavaScript()) {
759+
this.toggleJsInferredProject(/*isJsInferredProject*/ true);
760+
}
761+
super.addRoot(info);
762+
}
763+
764+
removeRoot(info: ScriptInfo) {
765+
if (this._isJsInferredProject && info.isJavaScript()) {
766+
if (filter(this.getRootScriptInfos(), info => info.isJavaScript()).length === 0) {
767+
this.toggleJsInferredProject(/*isJsInferredProject*/ false);
768+
}
769+
}
770+
super.removeRoot(info);
771+
}
772+
734773
getProjectRootPath() {
735774
// Single inferred project does not have a project root.
736775
if (this.projectService.useSingleInferredProject) {

src/server/scriptInfo.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,5 +355,9 @@ namespace ts.server {
355355
positionToLineOffset(position: number): ILineInfo {
356356
return this.textStorage.positionToLineOffset(position);
357357
}
358+
359+
public isJavaScript() {
360+
return this.scriptKind === ScriptKind.JS || this.scriptKind === ScriptKind.JSX;
361+
}
358362
}
359363
}

0 commit comments

Comments
 (0)