Skip to content

Commit 56a39b7

Browse files
committed
Keep alive declaration script info and map file info if source file info is alive
1 parent 0aa4da4 commit 56a39b7

7 files changed

Lines changed: 47 additions & 27 deletions

File tree

src/server/editorServices.ts

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,8 +2197,8 @@ namespace ts.server {
21972197
}
21982198

21992199
/*@internal*/
2200-
getDocumentPositionMapper(fileName: string, project: Project): DocumentPositionMapper | undefined {
2201-
const declarationInfo = this.getOrCreateScriptInfoNotOpenedByClient(fileName, project.currentDirectory, project.directoryStructureHost);
2200+
getDocumentPositionMapper(project: Project, generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined {
2201+
const declarationInfo = this.getOrCreateScriptInfoNotOpenedByClient(generatedFileName, project.currentDirectory, project.directoryStructureHost);
22022202
if (!declarationInfo) return undefined;
22032203

22042204
declarationInfo.getSnapshot(); // Ensure synchronized
@@ -2210,7 +2210,6 @@ namespace ts.server {
22102210
// Create the mapper
22112211
declarationInfo.mapInfo = undefined;
22122212

2213-
// TODO: shkamat Lifetime of declarationInfo and mapInfo
22142213
let readMapFile: ((fileName: string) => string | undefined) | undefined = fileName => {
22152214
const mapInfo = this.getOrCreateScriptInfoNotOpenedByClient(fileName, project.currentDirectory, project.directoryStructureHost);
22162215
if (!mapInfo) return undefined;
@@ -2227,6 +2226,11 @@ namespace ts.server {
22272226
);
22282227
readMapFile = undefined; // Remove ref to project
22292228
declarationInfo.mapper = mapper || false;
2229+
if (sourceFileName && mapper) {
2230+
// Attach as source
2231+
const sourceInfo = this.getOrCreateScriptInfoNotOpenedByClient(sourceFileName, project.currentDirectory, project.directoryStructureHost)!;
2232+
(declarationInfo.mapInfo!.sourceInfos || (declarationInfo.mapInfo!.sourceInfos = createMap())).set(sourceInfo.path, true);
2233+
}
22302234
return mapper;
22312235
}
22322236

@@ -2609,13 +2613,25 @@ namespace ts.server {
26092613
private removeOrphanScriptInfos() {
26102614
const toRemoveScriptInfos = cloneMap(this.filenameToScriptInfo);
26112615
this.filenameToScriptInfo.forEach(info => {
2612-
if (info.isScriptOpen() || !info.isOrphan()) {
2613-
toRemoveScriptInfos.delete(info.path);
2614-
if (info.mapInfo) {
2615-
toRemoveScriptInfos.delete(info.mapInfo.path);
2616-
if (info.mapInfo.sourceInfos) {
2617-
info.mapInfo.sourceInfos.forEach((_value, path) => toRemoveScriptInfos.delete(path));
2618-
}
2616+
// If script info is open or orphan, retain it and its dependencies
2617+
if (!info.isScriptOpen() && info.isOrphan()) {
2618+
// Otherwise if there is any source info that is alive, this alive too
2619+
if (!info.mapInfo || !info.mapInfo.sourceInfos) return;
2620+
if (!forEachKey(info.mapInfo.sourceInfos, path => {
2621+
const info = this.getScriptInfoForPath(path as Path)!;
2622+
return info.isScriptOpen() || !info.isOrphan();
2623+
})) {
2624+
return;
2625+
}
2626+
}
2627+
2628+
// Retain this script info
2629+
toRemoveScriptInfos.delete(info.path);
2630+
if (info.mapInfo) {
2631+
// And map file info and source infos
2632+
toRemoveScriptInfos.delete(info.mapInfo.path);
2633+
if (info.mapInfo.sourceInfos) {
2634+
info.mapInfo.sourceInfos.forEach((_value, path) => toRemoveScriptInfos.delete(path));
26192635
}
26202636
}
26212637
});

src/server/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,8 @@ namespace ts.server {
504504
}
505505

506506
/*@internal*/
507-
getDocumentPositionMapper(fileName: string): DocumentPositionMapper | undefined {
508-
return this.projectService.getDocumentPositionMapper(fileName, this);
507+
getDocumentPositionMapper(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined {
508+
return this.projectService.getDocumentPositionMapper(this, generatedFileName, sourceFileName);
509509
}
510510

511511
/*@internal*/

src/server/scriptInfo.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,20 @@ namespace ts.server {
6464
this.switchToScriptVersionCache();
6565
}
6666

67+
private resetSourceMapInfo() {
68+
this.info.mapper = undefined;
69+
this.info.sourceFileLike = undefined;
70+
this.info.mapInfo = undefined;
71+
this.info.sourceInfos = undefined;
72+
}
73+
6774
/** Public for testing */
6875
public useText(newText?: string) {
6976
this.svc = undefined;
7077
this.text = newText;
7178
this.lineMap = undefined;
7279
this.fileSize = undefined;
73-
this.info.mapper = undefined;
74-
this.info.sourceFileLike = undefined;
80+
this.resetSourceMapInfo();
7581
this.version.text++;
7682
}
7783

@@ -81,8 +87,7 @@ namespace ts.server {
8187
this.text = undefined;
8288
this.lineMap = undefined;
8389
this.fileSize = undefined;
84-
this.info.mapper = undefined;
85-
this.info.sourceFileLike = undefined;
90+
this.resetSourceMapInfo();
8691
}
8792

8893
/**
@@ -304,7 +309,7 @@ namespace ts.server {
304309
/*@internal*/
305310
sourceInfos?: Map<true>;
306311
/*@internal*/
307-
mapper: DocumentPositionMapper | false | undefined = false;
312+
mapper?: DocumentPositionMapper | false;
308313
/*@internal*/
309314
sourceFileLike: SourceFileLike | undefined;
310315

src/services/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ namespace ts {
11451145
getProgram,
11461146
fileExists: host.fileExists && (f => host.fileExists!(f)),
11471147
readFile: host.readFile && ((f, encoding) => host.readFile!(f, encoding)),
1148-
getDocumentPositionMapper: host.getDocumentPositionMapper && (f => host.getDocumentPositionMapper!(f)),
1148+
getDocumentPositionMapper: host.getDocumentPositionMapper && ((generatedFileName, sourceFileName) => host.getDocumentPositionMapper!(generatedFileName, sourceFileName)),
11491149
getSourceFileLike: host.getSourceFileLike && (f => host.getSourceFileLike!(f)),
11501150
log
11511151
});

src/services/sourcemaps.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ts {
1616
fileExists?(path: string): boolean;
1717
readFile?(path: string, encoding?: string): string | undefined;
1818
getSourceFileLike?(fileName: string): SourceFileLike | undefined;
19-
getDocumentPositionMapper?(fileName: string): DocumentPositionMapper | undefined;
19+
getDocumentPositionMapper?(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined;
2020
log(s: string): void;
2121
}
2222

@@ -31,20 +31,20 @@ namespace ts {
3131
return ts.toPath(fileName, currentDirectory, getCanonicalFileName);
3232
}
3333

34-
function getDocumentPositionMapper(fileName: string) {
35-
const path = toPath(fileName);
34+
function getDocumentPositionMapper(generatedFileName: string, sourceFileName?: string) {
35+
const path = toPath(generatedFileName);
3636
const value = documentPositionMappers.get(path);
3737
if (value) return value;
3838

3939
let mapper: DocumentPositionMapper | undefined;
4040
if (host.getDocumentPositionMapper) {
41-
mapper = host.getDocumentPositionMapper(fileName);
41+
mapper = host.getDocumentPositionMapper(generatedFileName, sourceFileName);
4242
}
4343
else if (host.readFile) {
44-
const file = getSourceFileLike(fileName);
44+
const file = getSourceFileLike(generatedFileName);
4545
mapper = file && ts.getDocumentPositionMapper(
4646
{ getSourceFileLike, getCanonicalFileName, log: s => host.log(s) },
47-
fileName,
47+
generatedFileName,
4848
getLineInfo(file.text, getLineStarts(file)),
4949
f => !host.fileExists || host.fileExists(f) ? host.readFile!(f) : undefined
5050
);
@@ -78,7 +78,7 @@ namespace ts {
7878
getDeclarationEmitOutputFilePathWorker(info.fileName, program.getCompilerOptions(), currentDirectory, program.getCommonSourceDirectory(), getCanonicalFileName);
7979
if (declarationPath === undefined) return undefined;
8080

81-
const newLoc = getDocumentPositionMapper(declarationPath).getGeneratedPosition(info);
81+
const newLoc = getDocumentPositionMapper(declarationPath, info.fileName).getGeneratedPosition(info);
8282
return newLoc === info ? undefined : newLoc;
8383
}
8484

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ namespace ts {
234234
writeFile?(fileName: string, content: string): void;
235235

236236
/* @internal */
237-
getDocumentPositionMapper?(fileName: string): DocumentPositionMapper | undefined;
237+
getDocumentPositionMapper?(generatedFileName: string, sourceFileName?: string): DocumentPositionMapper | undefined;
238238
/* @internal */
239239
getSourceFileLike?(fileName: string): SourceFileLike | undefined;
240240
}

src/testRunner/unittests/tsserverProjectSystem.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10757,7 +10757,6 @@ fn5();`
1075710757
openFilesForSession([dependencyTs, randomFile], session);
1075810758
checkNumberOfProjects(service, { configuredProjects: 2 });
1075910759
checkProjectActualFiles(service.configuredProjects.get(dependencyConfig.path)!, [dependencyTs.path, libFile.path, dependencyConfig.path]);
10760-
debugger;
1076110760
for (let i = 0; i < 5; i++) {
1076210761
const startSpan = { line: i + 1, offset: 17 };
1076310762
const response = session.executeCommandSeq<protocol.RenameRequest>({

0 commit comments

Comments
 (0)