Skip to content

Commit eb1973a

Browse files
committed
Fixup sourcemap positions and text writer calculations
1 parent 8adbf85 commit eb1973a

5 files changed

Lines changed: 40 additions & 29 deletions

File tree

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ namespace ts {
10261026

10271027
// SyntaxKind.UnparsedSource
10281028
function emitUnparsedSource(unparsed: UnparsedSource) {
1029-
write(unparsed.text);
1029+
writer.rawWrite(unparsed.text);
10301030
}
10311031

10321032
//

src/compiler/sourcemap.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ namespace ts {
151151

152152
// Initialize source map data
153153
completedSections = [];
154-
sectionStartLine = 0;
155-
sectionStartColumn = 0;
154+
sectionStartLine = 1;
155+
sectionStartColumn = 1;
156156
sourceMapData = {
157157
sourceMapFilePath,
158158
jsSourceMappingURL: !compilerOptions.inlineSourceMap ? getBaseFileName(normalizeSlashes(sourceMapFilePath)) : undefined!, // TODO: GH#18217
@@ -270,14 +270,11 @@ namespace ts {
270270

271271
function generateMap(): SourceMap {
272272
if (completedSections.length) {
273-
const last = {
274-
offset: { line: sectionStartLine, column: sectionStartColumn },
275-
map: captureSection()
276-
};
273+
captureSectionalSpanIfNeeded(/*reset*/ false);
277274
return {
278275
version: 3,
279-
file: last.map.file,
280-
sections: [...completedSections, last]
276+
file: sourceMapData.sourceMapFile,
277+
sections: completedSections
281278
};
282279
}
283280
else {
@@ -353,8 +350,8 @@ namespace ts {
353350
sourceLinePos.line++;
354351
sourceLinePos.character++;
355352

356-
const emittedLine = writer.getLine() - sectionStartLine;
357-
const emittedColumn = emittedLine === 0 ? writer.getColumn() - sectionStartColumn : writer.getColumn();
353+
const emittedLine = writer.getLine() - sectionStartLine + 1;
354+
const emittedColumn = emittedLine === 0 ? (writer.getColumn() - sectionStartColumn + 1) : writer.getColumn();
358355

359356
// If this location wasn't recorded or the location in source is going backwards, record the span
360357
if (!lastRecordedSourceMapSpan ||
@@ -389,6 +386,15 @@ namespace ts {
389386
}
390387
}
391388

389+
function captureSectionalSpanIfNeeded(reset: boolean) {
390+
if (lastRecordedSourceMapSpan && lastRecordedSourceMapSpan === lastEncodedSourceMapSpan) { // If we've recorded some spans, save them
391+
completedSections.push({ offset: { line: sectionStartLine - 1, column: sectionStartColumn - 1 }, map: captureSection() });
392+
if (reset) {
393+
resetSectionalData();
394+
}
395+
}
396+
}
397+
392398
/**
393399
* Emits a node with possible leading and trailing source maps.
394400
*
@@ -403,10 +409,7 @@ namespace ts {
403409

404410
if (node) {
405411
if (isUnparsedSource(node) && node.sourceMapText !== undefined) {
406-
if (lastRecordedSourceMapSpan && lastRecordedSourceMapSpan === lastEncodedSourceMapSpan) { // If we've recorded some spans, save them
407-
completedSections.push({ offset: { line: sectionStartLine, column: sectionStartColumn }, map: captureSection() });
408-
resetSectionalData();
409-
}
412+
captureSectionalSpanIfNeeded(/*reset*/ true);
410413
const text = node.sourceMapText;
411414
let parsed: {} | undefined;
412415
try {
@@ -415,7 +418,7 @@ namespace ts {
415418
catch {
416419
// empty
417420
}
418-
const offset = { line: writer.getLine(), column: writer.getColumn() };
421+
const offset = { line: writer.getLine() - 1, column: writer.getColumn() - 1 };
419422
completedSections.push(parsed
420423
? {
421424
offset,
@@ -431,7 +434,7 @@ namespace ts {
431434
sectionStartLine = writer.getLine();
432435
sectionStartColumn = writer.getColumn();
433436
lastRecordedSourceMapSpan = undefined!;
434-
lastEncodedSourceMapSpan = undefined!;
437+
lastEncodedSourceMapSpan = defaultLastEncodedSourceMapSpan;
435438
return emitResult;
436439
}
437440
const emitNode = node.emitNode;

src/compiler/utilities.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,13 +2825,26 @@ namespace ts {
28252825
let lineCount: number;
28262826
let linePos: number;
28272827

2828+
function updateLineCountAndPosFor(s: string) {
2829+
const lineStartsOfS = computeLineStarts(s);
2830+
if (lineStartsOfS.length > 1) {
2831+
lineCount = lineCount + lineStartsOfS.length - 1;
2832+
linePos = output.length - s.length + last(lineStartsOfS);
2833+
lineStart = (linePos - output.length) === 0;
2834+
}
2835+
else {
2836+
lineStart = false;
2837+
}
2838+
}
2839+
28282840
function write(s: string) {
28292841
if (s && s.length) {
28302842
if (lineStart) {
2831-
output += getIndentString(indent);
2843+
s = getIndentString(indent) + s;
28322844
lineStart = false;
28332845
}
28342846
output += s;
2847+
updateLineCountAndPosFor(s);
28352848
}
28362849
}
28372850

@@ -2845,21 +2858,14 @@ namespace ts {
28452858

28462859
function rawWrite(s: string) {
28472860
if (s !== undefined) {
2848-
if (lineStart) {
2849-
lineStart = false;
2850-
}
28512861
output += s;
2862+
updateLineCountAndPosFor(s);
28522863
}
28532864
}
28542865

28552866
function writeLiteral(s: string) {
28562867
if (s && s.length) {
28572868
write(s);
2858-
const lineStartsOfS = computeLineStarts(s);
2859-
if (lineStartsOfS.length > 1) {
2860-
lineCount = lineCount + lineStartsOfS.length - 1;
2861-
linePos = output.length - s.length + last(lineStartsOfS);
2862-
}
28632869
}
28642870
}
28652871

@@ -2873,7 +2879,9 @@ namespace ts {
28732879
}
28742880

28752881
function writeTextOfNode(text: string, node: Node) {
2876-
write(getTextOfNodeFromSourceText(text, node));
2882+
const s = getTextOfNodeFromSourceText(text, node);
2883+
write(s);
2884+
updateLineCountAndPosFor(s);
28772885
}
28782886

28792887
reset();

tests/baselines/reference/tsxErrorRecovery1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ function foo() {
1414
}
1515
// Shouldn't see any errors down here
1616
var y = {a} 1 };
17-
</>;
17+
</>;
1818
}

tests/baselines/reference/tsxStatelessFunctionComponents3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ define(["require", "exports", "react"], function (require, exports, React) {
2727
// Should be OK
2828
var MainMenu = function (props) { return (<div>
2929
<h3>Main Menu</h3>
30-
</div>); };
30+
</div>); };
3131
var App = function (_a) {
3232
var children = _a.children;
3333
return (<div>

0 commit comments

Comments
 (0)