Skip to content

Commit d097650

Browse files
committed
Add tests and fix DocumentPositionMapper creation on updates to d.ts, source file, map file etc
1 parent 0113f43 commit d097650

File tree

4 files changed

+194
-64
lines changed

4 files changed

+194
-64
lines changed

src/server/editorServices.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,8 +2241,8 @@ namespace ts.server {
22412241
const mapFileInfo = this.getScriptInfoForPath(declarationInfo.sourceMapFilePath);
22422242
if (mapFileInfo) {
22432243
mapFileInfo.getSnapshot();
2244-
if (mapFileInfo.mapper !== undefined) {
2245-
return mapFileInfo.mapper ? mapFileInfo.mapper : undefined;
2244+
if (mapFileInfo.documentPositionMapper !== undefined) {
2245+
return mapFileInfo.documentPositionMapper ? mapFileInfo.documentPositionMapper : undefined;
22462246
}
22472247
}
22482248
}
@@ -2251,15 +2251,16 @@ namespace ts.server {
22512251
declarationInfo.sourceMapFilePath = undefined;
22522252
let sourceMapFileInfo: ScriptInfo | undefined;
22532253

2254-
let readMapFile: ((fileName: string) => string | undefined) | undefined = fileName => {
2255-
const mapInfo = this.getOrCreateScriptInfoNotOpenedByClient(fileName, project.currentDirectory, project.directoryStructureHost);
2254+
let readMapFile: ReadMapFile | undefined = mapFileName => {
2255+
const mapInfo = this.getOrCreateScriptInfoNotOpenedByClient(mapFileName, project.currentDirectory, project.directoryStructureHost);
22562256
if (!mapInfo) return undefined;
22572257
sourceMapFileInfo = mapInfo;
22582258
const snap = mapInfo.getSnapshot();
2259+
if (mapInfo.documentPositionMapper !== undefined) return mapInfo.documentPositionMapper;
22592260
return snap.getText(0, snap.getLength());
22602261
};
22612262
const projectName = project.projectName;
2262-
const mapper = getDocumentPositionMapper(
2263+
const documentPositionMapper = getDocumentPositionMapper(
22632264
{ getCanonicalFileName: this.toCanonicalFileName, log: s => this.logger.info(s), getSourceFileLike: f => this.getSourceFileLike(f, projectName, declarationInfo) },
22642265
declarationInfo.fileName,
22652266
declarationInfo.getLineInfo(),
@@ -2268,8 +2269,9 @@ namespace ts.server {
22682269
readMapFile = undefined; // Remove ref to project
22692270
if (sourceMapFileInfo) {
22702271
declarationInfo.sourceMapFilePath = sourceMapFileInfo.path;
2271-
sourceMapFileInfo.mapper = mapper || false;
2272-
if (sourceFileName && mapper) {
2272+
sourceMapFileInfo.declarationInfoPath = declarationInfo.path;
2273+
sourceMapFileInfo.documentPositionMapper = documentPositionMapper || false;
2274+
if (sourceFileName && documentPositionMapper) {
22732275
// Attach as source
22742276
const sourceInfo = this.getOrCreateScriptInfoNotOpenedByClient(sourceFileName, project.currentDirectory, project.directoryStructureHost)!;
22752277
(sourceMapFileInfo.sourceInfos || (sourceMapFileInfo.sourceInfos = createMap())).set(sourceInfo.path, true);
@@ -2278,7 +2280,7 @@ namespace ts.server {
22782280
else {
22792281
declarationInfo.sourceMapFilePath = false;
22802282
}
2281-
return mapper;
2283+
return documentPositionMapper;
22822284
}
22832285

22842286
/*@internal*/

src/server/scriptInfo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace ts.server {
6969
this.info.sourceMapFilePath = undefined;
7070
this.info.declarationInfoPath = undefined;
7171
this.info.sourceInfos = undefined;
72-
this.info.mapper = undefined;
72+
this.info.documentPositionMapper = undefined;
7373
}
7474

7575
/** Public for testing */
@@ -317,7 +317,7 @@ namespace ts.server {
317317
/*@internal*/
318318
sourceInfos?: Map<true>;
319319
/*@internal*/
320-
mapper?: DocumentPositionMapper | false;
320+
documentPositionMapper?: DocumentPositionMapper | false;
321321

322322
constructor(
323323
private readonly host: ServerHost,

src/services/sourcemaps.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,17 @@ namespace ts {
127127
}
128128
}
129129

130+
/**
131+
* string | undefined to contents of map file to create DocumentPositionMapper from it
132+
* DocumentPositionMapper | false to give back cached DocumentPositionMapper
133+
*/
134+
export type ReadMapFile = (mapFileName: string) => string | undefined | DocumentPositionMapper | false;
135+
130136
export function getDocumentPositionMapper(
131137
host: DocumentPositionMapperHost,
132138
generatedFileName: string,
133139
generatedFileLineInfo: LineInfo,
134-
readMapFile: (fileName: string) => string | undefined) {
140+
readMapFile: ReadMapFile) {
135141
let mapFileName = tryGetSourceMappingURL(generatedFileLineInfo);
136142
if (mapFileName) {
137143
const match = base64UrlRegExp.exec(mapFileName);
@@ -152,9 +158,12 @@ namespace ts {
152158
for (const location of possibleMapLocations) {
153159
const mapFileName = getNormalizedAbsolutePath(location, getDirectoryPath(generatedFileName));
154160
const mapFileContents = readMapFile(mapFileName);
155-
if (mapFileContents) {
161+
if (isString(mapFileContents)) {
156162
return convertDocumentToSourceMapper(host, mapFileContents, mapFileName);
157163
}
164+
if (mapFileContents !== undefined) {
165+
return mapFileContents || undefined;
166+
}
158167
}
159168
return undefined;
160169
}

0 commit comments

Comments
 (0)