From df2db9737f46fbf568798ae27c4af5130b02ad78 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Wed, 8 Apr 2020 16:14:43 -0700 Subject: [PATCH] More sophisticated check for source position comparability --- src/compiler/emitter.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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) {