Skip to content

Commit a2b13d0

Browse files
authored
pass project name as a constructor parameter (#12333)
1 parent c90a40c commit a2b13d0

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,7 @@ namespace ts.projectSystem {
16141614
return;
16151615
}
16161616
assert.equal(e.eventName, server.ProjectLanguageServiceStateEvent);
1617+
assert.equal(e.data.project.getProjectName(), config.path, "project name");
16171618
lastEvent = <server.ProjectLanguageServiceStateEvent>e;
16181619
});
16191620
session.executeCommand(<protocol.OpenRequest>{
@@ -1628,6 +1629,7 @@ namespace ts.projectSystem {
16281629
assert.isFalse(project.languageServiceEnabled, "Language service enabled");
16291630
assert.isTrue(!!lastEvent, "should receive event");
16301631
assert.equal(lastEvent.data.project, project, "project name");
1632+
assert.equal(lastEvent.data.project.getProjectName(), config.path, "config path");
16311633
assert.isFalse(lastEvent.data.languageServiceEnabled, "Language service state");
16321634

16331635
host.reloadFS([f1, f2, configWithExclude]);

src/server/editorServices.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ namespace ts.server {
470470

471471
private onTypeRootFileChanged(project: ConfiguredProject, fileName: string) {
472472
this.logger.info(`Type root file ${fileName} changed`);
473-
this.throttledOperations.schedule(project.configFileName + " * type root", /*delay*/ 250, () => {
473+
this.throttledOperations.schedule(project.getConfigFilePath() + " * type root", /*delay*/ 250, () => {
474474
project.updateTypes();
475475
this.updateConfiguredProject(project); // TODO: Figure out why this is needed (should be redundant?)
476476
this.refreshInferredProjects();
@@ -492,13 +492,13 @@ namespace ts.server {
492492

493493
this.logger.info(`Detected source file changes: ${fileName}`);
494494
this.throttledOperations.schedule(
495-
project.configFileName,
495+
project.getConfigFilePath(),
496496
/*delay*/250,
497497
() => this.handleChangeInSourceFileForConfiguredProject(project, fileName));
498498
}
499499

500500
private handleChangeInSourceFileForConfiguredProject(project: ConfiguredProject, triggerFile: string) {
501-
const { projectOptions, configFileErrors } = this.convertConfigFileContentToProjectOptions(project.configFileName);
501+
const { projectOptions, configFileErrors } = this.convertConfigFileContentToProjectOptions(project.getConfigFilePath());
502502
this.reportConfigFileDiagnostics(project.getProjectName(), configFileErrors, triggerFile);
503503

504504
const newRootFiles = projectOptions.files.map((f => this.getCanonicalFileName(f)));
@@ -520,7 +520,7 @@ namespace ts.server {
520520
}
521521

522522
private onConfigChangedForConfiguredProject(project: ConfiguredProject) {
523-
this.logger.info(`Config file changed: ${project.configFileName}`);
523+
this.logger.info(`Config file changed: ${project.getConfigFilePath()}`);
524524
this.updateConfiguredProject(project);
525525
this.refreshInferredProjects();
526526
}
@@ -1009,13 +1009,13 @@ namespace ts.server {
10091009
}
10101010

10111011
private updateConfiguredProject(project: ConfiguredProject) {
1012-
if (!this.host.fileExists(project.configFileName)) {
1012+
if (!this.host.fileExists(project.getConfigFilePath())) {
10131013
this.logger.info("Config file deleted");
10141014
this.removeProject(project);
10151015
return;
10161016
}
10171017

1018-
const { success, projectOptions, configFileErrors } = this.convertConfigFileContentToProjectOptions(project.configFileName);
1018+
const { success, projectOptions, configFileErrors } = this.convertConfigFileContentToProjectOptions(project.getConfigFilePath());
10191019
if (!success) {
10201020
// reset project settings to default
10211021
this.updateNonInferredProject(project, [], fileNamePropertyReader, {}, {}, /*compileOnSave*/false, configFileErrors);

src/server/project.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ namespace ts.server {
229229
}
230230

231231
constructor(
232+
private readonly projectName: string,
232233
readonly projectKind: ProjectKind,
233234
readonly projectService: ProjectService,
234235
private documentRegistry: ts.DocumentRegistry,
@@ -307,7 +308,9 @@ namespace ts.server {
307308
this.projectService.onUpdateLanguageServiceStateForProject(this, /*languageServiceEnabled*/ false);
308309
}
309310

310-
abstract getProjectName(): string;
311+
getProjectName() {
312+
return this.projectName;
313+
}
311314
abstract getProjectRootPath(): string | undefined;
312315
abstract getTypingOptions(): TypingOptions;
313316

@@ -759,31 +762,27 @@ namespace ts.server {
759762

760763
export class InferredProject extends Project {
761764

762-
private static NextId = 1;
763-
764-
/**
765-
* Unique name that identifies this particular inferred project
766-
*/
767-
private readonly inferredProjectName: string;
765+
private static newName = (() => {
766+
let nextId = 1;
767+
return () => {
768+
const id = nextId;
769+
nextId++;
770+
return makeInferredProjectName(id);
771+
}
772+
})();
768773

769774
// Used to keep track of what directories are watched for this project
770775
directoriesWatchedForTsconfig: string[] = [];
771776

772777
constructor(projectService: ProjectService, documentRegistry: ts.DocumentRegistry, compilerOptions: CompilerOptions) {
773-
super(ProjectKind.Inferred,
778+
super(InferredProject.newName(),
779+
ProjectKind.Inferred,
774780
projectService,
775781
documentRegistry,
776782
/*files*/ undefined,
777783
/*languageServiceEnabled*/ true,
778784
compilerOptions,
779785
/*compileOnSaveEnabled*/ false);
780-
781-
this.inferredProjectName = makeInferredProjectName(InferredProject.NextId);
782-
InferredProject.NextId++;
783-
}
784-
785-
getProjectName() {
786-
return this.inferredProjectName;
787786
}
788787

789788
getProjectRootPath() {
@@ -822,19 +821,23 @@ namespace ts.server {
822821
/** Used for configured projects which may have multiple open roots */
823822
openRefCount = 0;
824823

825-
constructor(readonly configFileName: NormalizedPath,
824+
constructor(configFileName: NormalizedPath,
826825
projectService: ProjectService,
827826
documentRegistry: ts.DocumentRegistry,
828827
hasExplicitListOfFiles: boolean,
829828
compilerOptions: CompilerOptions,
830829
private wildcardDirectories: Map<WatchDirectoryFlags>,
831830
languageServiceEnabled: boolean,
832831
public compileOnSaveEnabled: boolean) {
833-
super(ProjectKind.Configured, projectService, documentRegistry, hasExplicitListOfFiles, languageServiceEnabled, compilerOptions, compileOnSaveEnabled);
832+
super(configFileName, ProjectKind.Configured, projectService, documentRegistry, hasExplicitListOfFiles, languageServiceEnabled, compilerOptions, compileOnSaveEnabled);
833+
}
834+
835+
getConfigFilePath() {
836+
return this.getProjectName();
834837
}
835838

836839
getProjectRootPath() {
837-
return getDirectoryPath(this.configFileName);
840+
return getDirectoryPath(this.getConfigFilePath());
838841
}
839842

840843
setProjectErrors(projectErrors: Diagnostic[]) {
@@ -849,12 +852,8 @@ namespace ts.server {
849852
return this.typingOptions;
850853
}
851854

852-
getProjectName() {
853-
return this.configFileName;
854-
}
855-
856855
watchConfigFile(callback: (project: ConfiguredProject) => void) {
857-
this.projectFileWatcher = this.projectService.host.watchFile(this.configFileName, _ => callback(this));
856+
this.projectFileWatcher = this.projectService.host.watchFile(this.getConfigFilePath(), _ => callback(this));
858857
}
859858

860859
watchTypeRoots(callback: (project: ConfiguredProject, path: string) => void) {
@@ -872,7 +871,7 @@ namespace ts.server {
872871
return;
873872
}
874873

875-
const directoryToWatch = getDirectoryPath(this.configFileName);
874+
const directoryToWatch = getDirectoryPath(this.getConfigFilePath());
876875
this.projectService.logger.info(`Add recursive watcher for: ${directoryToWatch}`);
877876
this.directoryWatcher = this.projectService.host.watchDirectory(directoryToWatch, path => callback(this, path), /*recursive*/ true);
878877
}
@@ -881,7 +880,7 @@ namespace ts.server {
881880
if (!this.wildcardDirectories) {
882881
return;
883882
}
884-
const configDirectoryPath = getDirectoryPath(this.configFileName);
883+
const configDirectoryPath = getDirectoryPath(this.getConfigFilePath());
885884
this.directoriesWatchedForWildcards = reduceProperties(this.wildcardDirectories, (watchers, flag, directory) => {
886885
if (comparePaths(configDirectoryPath, directory, ".", !this.projectService.host.useCaseSensitiveFileNames) !== Comparison.EqualTo) {
887886
const recursive = (flag & WatchDirectoryFlags.Recursive) !== 0;
@@ -941,14 +940,14 @@ namespace ts.server {
941940

942941
export class ExternalProject extends Project {
943942
private typingOptions: TypingOptions;
944-
constructor(readonly externalProjectName: string,
943+
constructor(externalProjectName: string,
945944
projectService: ProjectService,
946945
documentRegistry: ts.DocumentRegistry,
947946
compilerOptions: CompilerOptions,
948947
languageServiceEnabled: boolean,
949948
public compileOnSaveEnabled: boolean,
950949
private readonly projectFilePath?: string) {
951-
super(ProjectKind.External, projectService, documentRegistry, /*hasExplicitListOfFiles*/ true, languageServiceEnabled, compilerOptions, compileOnSaveEnabled);
950+
super(externalProjectName, ProjectKind.External, projectService, documentRegistry, /*hasExplicitListOfFiles*/ true, languageServiceEnabled, compilerOptions, compileOnSaveEnabled);
952951
}
953952

954953
getProjectRootPath() {
@@ -958,7 +957,7 @@ namespace ts.server {
958957
// if the projectFilePath is not given, we make the assumption that the project name
959958
// is the path of the project file. AS the project name is provided by VS, we need to
960959
// normalize slashes before using it as a file name.
961-
return getDirectoryPath(normalizeSlashes(this.externalProjectName));
960+
return getDirectoryPath(normalizeSlashes(this.getProjectName()));
962961
}
963962

964963
getTypingOptions() {
@@ -992,9 +991,5 @@ namespace ts.server {
992991
}
993992
this.typingOptions = newTypingOptions;
994993
}
995-
996-
getProjectName() {
997-
return this.externalProjectName;
998-
}
999994
}
1000995
}

0 commit comments

Comments
 (0)