Change the design to track addRoot and removeRoot

This commit is contained in:
zhengbli 2016-12-29 17:00:04 -08:00
parent 09fc3b3a18
commit 8ac22ecbb0
4 changed files with 66 additions and 17 deletions

View File

@ -1620,7 +1620,7 @@ namespace ts.projectSystem {
const configureHostRequest = makeSessionRequest<protocol.ConfigureRequestArguments>(CommandNames.Configure, { extraFileExtensions });
session.executeCommand(configureHostRequest).response;
// HTML file still not included in the project as it is closed
// HTML file still not included in the project as it is closed
checkNumberOfProjects(projectService, { configuredProjects: 1 });
checkProjectActualFiles(projectService.configuredProjects[0], [file1.path]);
@ -3053,7 +3053,7 @@ namespace ts.projectSystem {
});
describe("maxNodeModuleJsDepth for inferred projects", () => {
it("should be set by default", () => {
it("should be set to 2 if the project has js root files", () => {
const file1: FileOrFolder = {
path: "/a/b/file1.js",
content: `var t = require("test"); t.`
@ -3077,6 +3077,33 @@ namespace ts.projectSystem {
options = project.getCompilerOptions();
assert.isTrue(options.maxNodeModuleJsDepth === 2);
});
it("should return to normal state when all js root files are removed from project", () => {
const file1 = {
path: "/a/file1.ts",
content: "let x =1;"
};
const file2 = {
path: "/a/file2.js",
content: "let x =1;"
};
const host = createServerHost([file1, file2, libFile]);
const projectService = createProjectService(host, { useSingleInferredProject: true });
projectService.openClientFile(file1.path);
checkNumberOfInferredProjects(projectService, 1);
let project = projectService.inferredProjects[0];
assert.isUndefined(project.getCompilerOptions().maxNodeModuleJsDepth);
projectService.openClientFile(file2.path);
project = projectService.inferredProjects[0];
assert.isTrue(project.getCompilerOptions().maxNodeModuleJsDepth === 2);
projectService.closeClientFile(file2.path);
project = projectService.inferredProjects[0];
assert.isUndefined(project.getCompilerOptions().maxNodeModuleJsDepth);
});
});
}

View File

@ -1072,10 +1072,6 @@ namespace ts.server {
? this.inferredProjects[0]
: new InferredProject(this, this.documentRegistry, this.compilerOptionsForInferredProjects);
if (root.scriptKind === ScriptKind.JS || root.scriptKind === ScriptKind.JSX) {
project.setAsJsInferredProject();
}
project.addRoot(root);
this.directoryWatchers.startWatchingContainingDirectoriesForFile(

View File

@ -395,7 +395,9 @@ namespace ts.server {
}
removeFile(info: ScriptInfo, detachFromProject = true) {
this.removeRootFileIfNecessary(info);
if (this.isRoot(info)) {
this.removeRoot(info);
}
this.lsHost.notifyFileRemoved(info);
this.cachedUnresolvedImportsPerFile.remove(info.path);
@ -693,11 +695,9 @@ namespace ts.server {
}
// remove a root file from project
private removeRootFileIfNecessary(info: ScriptInfo): void {
if (this.isRoot(info)) {
remove(this.rootFiles, info);
this.rootFilesMap.remove(info.path);
}
protected removeRoot(info: ScriptInfo): void {
remove(this.rootFiles, info);
this.rootFilesMap.remove(info.path);
}
}
@ -714,13 +714,16 @@ namespace ts.server {
private _isJsInferredProject = false;
setAsJsInferredProject() {
this._isJsInferredProject = true;
this.setCompilerOptions();
toggleJsInferredProject(isJsInferredProject: boolean) {
if (isJsInferredProject !== this._isJsInferredProject) {
this._isJsInferredProject = isJsInferredProject;
this.setCompilerOptions();
}
}
setCompilerOptions(newOptions?: CompilerOptions) {
newOptions = newOptions ? newOptions : this.getCompilerOptions();
setCompilerOptions(options?: CompilerOptions) {
// Avoid manipulating the given options directly
const newOptions = options ? clone(options) : this.getCompilerOptions();
if (!newOptions) {
return;
}
@ -728,6 +731,9 @@ namespace ts.server {
if (this._isJsInferredProject && typeof newOptions.maxNodeModuleJsDepth !== "number") {
newOptions.maxNodeModuleJsDepth = 2;
}
else if (!this._isJsInferredProject) {
newOptions.maxNodeModuleJsDepth = undefined;
}
newOptions.allowJs = true;
super.setCompilerOptions(newOptions);
}
@ -746,6 +752,22 @@ namespace ts.server {
/*compileOnSaveEnabled*/ false);
}
addRoot(info: ScriptInfo) {
if (!this._isJsInferredProject && info.isJavaScript()) {
this.toggleJsInferredProject(/*isJsInferredProject*/ true);
}
super.addRoot(info);
}
removeRoot(info: ScriptInfo) {
if (this._isJsInferredProject && info.isJavaScript()) {
if (filter(this.getRootScriptInfos(), info => info.isJavaScript()).length === 0) {
this.toggleJsInferredProject(/*isJsInferredProject*/ false);
}
}
super.removeRoot(info);
}
getProjectRootPath() {
// Single inferred project does not have a project root.
if (this.projectService.useSingleInferredProject) {

View File

@ -355,5 +355,9 @@ namespace ts.server {
positionToLineOffset(position: number): ILineInfo {
return this.textStorage.positionToLineOffset(position);
}
public isJavaScript() {
return this.scriptKind === ScriptKind.JS || this.scriptKind === ScriptKind.JSX;
}
}
}