Skip to content

Commit b69e65f

Browse files
committed
Fix source maps for arrow functions, comments in sourcemap writer.
1 parent d594865 commit b69e65f

6 files changed

Lines changed: 474 additions & 72 deletions

File tree

src/compiler/printer.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ const _super = (function (geti, seti) {
155155
const {
156156
emitStart,
157157
emitEnd,
158-
emitPos
158+
emitTokenStart,
159+
emitTokenEnd
159160
} = sourceMap;
160161

161162
const comments = createCommentWriter(host, writer, sourceMap);
@@ -172,8 +173,9 @@ const _super = (function (geti, seti) {
172173
let context: TransformationContext;
173174
let getNodeEmitFlags: (node: Node) => NodeEmitFlags;
174175
let setNodeEmitFlags: (node: Node, flags: NodeEmitFlags) => void;
175-
let getCommentRange: (node: Node) => TextRange;
176176
let getSourceMapRange: (node: Node) => TextRange;
177+
let getTokenSourceMapRange: (node: Node, token: SyntaxKind) => TextRange;
178+
let getCommentRange: (node: Node) => TextRange;
177179
let isSubstitutionEnabled: (node: Node) => boolean;
178180
let isEmitNotificationEnabled: (node: Node) => boolean;
179181
let onSubstituteNode: (node: Node, isExpression: boolean) => Node;
@@ -234,8 +236,9 @@ const _super = (function (geti, seti) {
234236

235237
getNodeEmitFlags = undefined;
236238
setNodeEmitFlags = undefined;
237-
getCommentRange = undefined;
238239
getSourceMapRange = undefined;
240+
getTokenSourceMapRange = undefined;
241+
getCommentRange = undefined;
239242
isSubstitutionEnabled = undefined;
240243
isEmitNotificationEnabled = undefined;
241244
onSubstituteNode = undefined;
@@ -255,8 +258,9 @@ const _super = (function (geti, seti) {
255258
context = _context;
256259
getNodeEmitFlags = context.getNodeEmitFlags;
257260
setNodeEmitFlags = context.setNodeEmitFlags;
258-
getCommentRange = context.getCommentRange;
259261
getSourceMapRange = context.getSourceMapRange;
262+
getTokenSourceMapRange = context.getTokenSourceMapRange;
263+
getCommentRange = context.getCommentRange;
260264
isSubstitutionEnabled = context.isSubstitutionEnabled;
261265
isEmitNotificationEnabled = context.isEmitNotificationEnabled;
262266
onSubstituteNode = context.onSubstituteNode;
@@ -343,9 +347,9 @@ const _super = (function (geti, seti) {
343347
const leadingComments = getLeadingComments(node, shouldSkipLeadingCommentsForNode, getCommentRange);
344348
const trailingComments = getTrailingComments(node, shouldSkipTrailingCommentsForNode, getCommentRange);
345349
emitLeadingComments(node, leadingComments, getCommentRange);
346-
emitStart(node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
350+
emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
347351
emitWorker(node);
348-
emitEnd(node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
352+
emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
349353
emitTrailingComments(node, trailingComments);
350354
}
351355
}
@@ -1646,7 +1650,7 @@ const _super = (function (geti, seti) {
16461650

16471651
emitTrailingDetachedComments(body.statements, body, shouldSkipTrailingCommentsForNode);
16481652
decreaseIndent();
1649-
writeToken(SyntaxKind.CloseBraceToken, body.statements.end);
1653+
writeToken(SyntaxKind.CloseBraceToken, body.statements.end, body);
16501654
}
16511655

16521656
function emitClassDeclaration(node: ClassDeclaration) {
@@ -2410,12 +2414,10 @@ const _super = (function (geti, seti) {
24102414
}
24112415
}
24122416

2413-
function writeToken(token: SyntaxKind, tokenStartPos: number, contextNode?: Node) {
2414-
tokenStartPos = skipTrivia(currentText, tokenStartPos);
2415-
emitPos(tokenStartPos, contextNode, shouldSkipLeadingSourceMapForToken);
2417+
function writeToken(token: SyntaxKind, pos: number, contextNode?: Node) {
2418+
const tokenStartPos = emitTokenStart(token, pos, contextNode, shouldSkipLeadingSourceMapForToken, getTokenSourceMapRange);
24162419
const tokenEndPos = writeTokenText(token, tokenStartPos);
2417-
emitPos(tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken);
2418-
return tokenEndPos;
2420+
return emitTokenEnd(token, tokenEndPos, contextNode, shouldSkipTrailingSourceMapForToken, getTokenSourceMapRange);
24192421
}
24202422

24212423
function shouldSkipLeadingSourceMapForToken(contextNode: Node) {
@@ -2434,9 +2436,9 @@ const _super = (function (geti, seti) {
24342436

24352437
function writeTokenNode(node: Node) {
24362438
if (node) {
2437-
emitStart(node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
2439+
emitStart(/*range*/ node, /*contextNode*/ node, shouldSkipLeadingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
24382440
writeTokenText(node.kind);
2439-
emitEnd(node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
2441+
emitEnd(/*range*/ node, /*contextNode*/ node, shouldSkipTrailingSourceMapForNode, shouldSkipSourceMapForChildren, getSourceMapRange);
24402442
}
24412443
}
24422444

0 commit comments

Comments
 (0)