diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 492e680860b..adaa11ea86f 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4321,12 +4321,12 @@ namespace ts { // JsxText will be written with its leading whitespace, so don't add more manually. return 0; } - else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode) && previousNode.parent === nextNode.parent) { + else if (siblingNodePositionsAreComparable(previousNode, nextNode)) { if (preserveSourceNewlines) { return getEffectiveLines( includeComments => getLinesBetweenRangeEndAndRangeStart( - previousNode, - nextNode, + getOriginalNode(previousNode), + getOriginalNode(nextNode), currentSourceFile!, includeComments)); } @@ -4342,6 +4342,22 @@ namespace ts { return format & ListFormat.MultiLine ? 1 : 0; } + function siblingNodePositionsAreComparable(previousNode: Node, nextNode: Node) { + if (previousNode.kind === SyntaxKind.NotEmittedStatement && nextNode.kind === SyntaxKind.NotEmittedStatement) { + return false; + } + if (nodeIsSynthesized(previousNode) || nodeIsSynthesized(nextNode)) { + return false; + } + + if (!previousNode.parent || !nextNode.parent) { + const previousParent = getOriginalNode(previousNode).parent; + return previousParent && previousParent === getOriginalNode(nextNode).parent; + } + + return nextNode.pos >= previousNode.end; + } + function getClosingLineTerminatorCount(parentNode: TextRange, children: readonly Node[], format: ListFormat): number { if (format & ListFormat.PreserveLines || preserveSourceNewlines) { if (format & ListFormat.PreferNewLine) {