Skip to content

Commit

Permalink
Do a bit less work when preserveNewlines is off
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Feb 10, 2020
1 parent 0ea8d79 commit 6ff9c2b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
40 changes: 22 additions & 18 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4278,11 +4278,13 @@ namespace ts {
return rangeIsOnSingleLine(parentNode, currentSourceFile!) ? 0 : 1;
}
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(firstChild) && firstChild.parent === parentNode) {
let lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ true);
if (lines === 0) {
lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ false);
if (preserveNewlines) {
const lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ true);
return lines === 0
? getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ false)
: lines;
}
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
return rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile!) ? 0 : 1;
}
else if (synthesizedNodeStartsOnNewLine(firstChild, format)) {
return 1;
Expand All @@ -4297,8 +4299,8 @@ namespace ts {
return 0;
}
else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode) && previousNode.parent === nextNode.parent) {
const lines = getEffectiveLinesBetweenRanges(previousNode, nextNode, getLinesBetweenRangeEndAndRangeStart);
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
const lines = getEffectiveLinesBetweenRanges(previousNode, nextNode);
return preserveNewlines ? lines : Math.min(lines, 1);
}
else if (synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format)) {
return 1;
Expand All @@ -4322,7 +4324,7 @@ namespace ts {
}
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(lastChild) && lastChild.parent === parentNode) {
const lines = getLinesBetweenRangeEndPositions(lastChild, parentNode, currentSourceFile!);
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
return preserveNewlines ? lines : Math.min(lines, 1);
}
else if (synthesizedNodeStartsOnNewLine(lastChild, format)) {
return 1;
Expand All @@ -4334,26 +4336,28 @@ namespace ts {
return 0;
}

function getEffectiveLinesBetweenRanges(
node1: TextRange,
node2: TextRange,
getLinesBetweenPositions: (range1: TextRange, range2: TextRange, sourceFile: SourceFile, includeComments: boolean) => number
) {
// We start by measuring the line difference from parentNode's start to node2's comments start,
// so that this is counted as a one line difference, not two:
function getEffectiveLinesBetweenRanges(node1: TextRange, node2: TextRange) {
// If 'preserveNewlines' is disabled, skip the more accurate check that might require
// querying for source position twice.
if (!preserveNewlines) {
return getLinesBetweenRangeEndAndRangeStart(node1, node2, currentSourceFile!, /*includeComments*/ false);
}
// We start by measuring the line difference from node1's end to node2's comments start,
// so that this is counted as a one-line difference, not two:
//
// node1;
// // NODE2 COMMENT
// node2;
const lines = getLinesBetweenPositions(node1, node2, currentSourceFile!, /*includeComments*/ true);
const lines = getLinesBetweenRangeEndAndRangeStart(node1, node2, currentSourceFile!, /*includeComments*/ true);
if (lines === 0) {
// However, if the line difference considering node2's comments was 0, we might have this:
//
// node1; // NODE2 COMMENT
// node2;
//
// in which case we should be ignoring node2's comment.
return getLinesBetweenPositions(node1, node2, currentSourceFile!, /*includeComments*/ false);
// in which case we should be ignoring node2's comment, so this too is counted as
// a one-line difference, not zero.
return getLinesBetweenRangeEndAndRangeStart(node1, node2, currentSourceFile!, /*includeComments*/ false);
}
return lines;
}
Expand Down Expand Up @@ -4386,7 +4390,7 @@ namespace ts {
}

if (!nodeIsSynthesized(parent) && !nodeIsSynthesized(node1) && !nodeIsSynthesized(node2)) {
const lines = getEffectiveLinesBetweenRanges(node1, node2, getLinesBetweenRangeEndAndRangeStart);
const lines = getEffectiveLinesBetweenRanges(node1, node2);
return preserveNewlines ? lines : Math.min(lines, 1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4703,7 +4703,7 @@ namespace ts {

export function getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(pos: number, sourceFile: SourceFile, includeComments?: boolean) {
const startPos = skipTrivia(sourceFile.text, pos, /*stopAfterLineBreak*/ false, includeComments);
const prevPos = getPreviousNonWhitespacePosition(pos, sourceFile);
const prevPos = getPreviousNonWhitespacePosition(startPos, sourceFile);
return getLineOfLocalPosition(sourceFile, startPos) - getLineOfLocalPosition(sourceFile, prevPos || 0);
}

Expand Down

0 comments on commit 6ff9c2b

Please sign in to comment.