Skip to content

Commit 8ac22ec

Browse files
committed
Change the design to track addRoot and removeRoot
1 parent 09fc3b3 commit 8ac22ec

File tree

4 files changed

+66
-17
lines changed

4 files changed

+66
-17
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 29 additions & 2 deletions
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

@@ -3053,7 +3053,7 @@ namespace ts.projectSystem {
30533053
});
30543054

30553055
describe("maxNodeModuleJsDepth for inferred projects", () => {
3056-
it("should be set by default", () => {
3056+
it("should be set to 2 if the project has js root files", () => {
30573057
const file1: FileOrFolder = {
30583058
path: "/a/b/file1.js",
30593059
content: `var t = require("test"); t.`
@@ -3077,6 +3077,33 @@ namespace ts.projectSystem {
30773077
options = project.getCompilerOptions();
30783078
assert.isTrue(options.maxNodeModuleJsDepth === 2);
30793079
});
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+
});
30803107
});
30813108

30823109
}

src/server/editorServices.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,10 +1072,6 @@ namespace ts.server {
10721072
? this.inferredProjects[0]
10731073
: new InferredProject(this, this.documentRegistry, this.compilerOptionsForInferredProjects);
10741074

1075-
if (root.scriptKind === ScriptKind.JS || root.scriptKind === ScriptKind.JSX) {
1076-
project.setAsJsInferredProject();
1077-
}
1078-
10791075
project.addRoot(root);
10801076

10811077
this.directoryWatchers.startWatchingContainingDirectoriesForFile(

src/server/project.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,9 @@ namespace ts.server {
395395
}
396396

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

@@ -693,11 +695,9 @@ namespace ts.server {
693695
}
694696

695697
// remove a root file from project
696-
private removeRootFileIfNecessary(info: ScriptInfo): void {
697-
if (this.isRoot(info)) {
698-
remove(this.rootFiles, info);
699-
this.rootFilesMap.remove(info.path);
700-
}
698+
protected removeRoot(info: ScriptInfo): void {
699+
remove(this.rootFiles, info);
700+
this.rootFilesMap.remove(info.path);
701701
}
702702
}
703703

@@ -714,20 +714,26 @@ namespace ts.server {
714714

715715
private _isJsInferredProject = false;
716716

717-
setAsJsInferredProject() {
718-
this._isJsInferredProject = true;
719-
this.setCompilerOptions();
717+
toggleJsInferredProject(isJsInferredProject: boolean) {
718+
if (isJsInferredProject !== this._isJsInferredProject) {
719+
this._isJsInferredProject = isJsInferredProject;
720+
this.setCompilerOptions();
721+
}
720722
}
721723

722-
setCompilerOptions(newOptions?: CompilerOptions) {
723-
newOptions = newOptions ? newOptions : this.getCompilerOptions();
724+
setCompilerOptions(options?: CompilerOptions) {
725+
// Avoid manipulating the given options directly
726+
const newOptions = options ? clone(options) : this.getCompilerOptions();
724727
if (!newOptions) {
725728
return;
726729
}
727730

728731
if (this._isJsInferredProject && typeof newOptions.maxNodeModuleJsDepth !== "number") {
729732
newOptions.maxNodeModuleJsDepth = 2;
730733
}
734+
else if (!this._isJsInferredProject) {
735+
newOptions.maxNodeModuleJsDepth = undefined;
736+
}
731737
newOptions.allowJs = true;
732738
super.setCompilerOptions(newOptions);
733739
}
@@ -746,6 +752,22 @@ namespace ts.server {
746752
/*compileOnSaveEnabled*/ false);
747753
}
748754

755+
addRoot(info: ScriptInfo) {
756+
if (!this._isJsInferredProject && info.isJavaScript()) {
757+
this.toggleJsInferredProject(/*isJsInferredProject*/ true);
758+
}
759+
super.addRoot(info);
760+
}
761+
762+
removeRoot(info: ScriptInfo) {
763+
if (this._isJsInferredProject && info.isJavaScript()) {
764+
if (filter(this.getRootScriptInfos(), info => info.isJavaScript()).length === 0) {
765+
this.toggleJsInferredProject(/*isJsInferredProject*/ false);
766+
}
767+
}
768+
super.removeRoot(info);
769+
}
770+
749771
getProjectRootPath() {
750772
// Single inferred project does not have a project root.
751773
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)