Skip to content

Commit 6ff9c2b

Browse files
committed
Do a bit less work when preserveNewlines is off
1 parent 0ea8d79 commit 6ff9c2b

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

Diff for: src/compiler/emitter.ts

+22-18
Original file line numberDiff line numberDiff line change
@@ -4278,11 +4278,13 @@ namespace ts {
42784278
return rangeIsOnSingleLine(parentNode, currentSourceFile!) ? 0 : 1;
42794279
}
42804280
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(firstChild) && firstChild.parent === parentNode) {
4281-
let lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ true);
4282-
if (lines === 0) {
4283-
lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ false);
4281+
if (preserveNewlines) {
4282+
const lines = getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ true);
4283+
return lines === 0
4284+
? getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(firstChild.pos, currentSourceFile!, /*includeComments*/ false)
4285+
: lines;
42844286
}
4285-
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
4287+
return rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile!) ? 0 : 1;
42864288
}
42874289
else if (synthesizedNodeStartsOnNewLine(firstChild, format)) {
42884290
return 1;
@@ -4297,8 +4299,8 @@ namespace ts {
42974299
return 0;
42984300
}
42994301
else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode) && previousNode.parent === nextNode.parent) {
4300-
const lines = getEffectiveLinesBetweenRanges(previousNode, nextNode, getLinesBetweenRangeEndAndRangeStart);
4301-
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
4302+
const lines = getEffectiveLinesBetweenRanges(previousNode, nextNode);
4303+
return preserveNewlines ? lines : Math.min(lines, 1);
43024304
}
43034305
else if (synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format)) {
43044306
return 1;
@@ -4322,7 +4324,7 @@ namespace ts {
43224324
}
43234325
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(lastChild) && lastChild.parent === parentNode) {
43244326
const lines = getLinesBetweenRangeEndPositions(lastChild, parentNode, currentSourceFile!);
4325-
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
4327+
return preserveNewlines ? lines : Math.min(lines, 1);
43264328
}
43274329
else if (synthesizedNodeStartsOnNewLine(lastChild, format)) {
43284330
return 1;
@@ -4334,26 +4336,28 @@ namespace ts {
43344336
return 0;
43354337
}
43364338

4337-
function getEffectiveLinesBetweenRanges(
4338-
node1: TextRange,
4339-
node2: TextRange,
4340-
getLinesBetweenPositions: (range1: TextRange, range2: TextRange, sourceFile: SourceFile, includeComments: boolean) => number
4341-
) {
4342-
// We start by measuring the line difference from parentNode's start to node2's comments start,
4343-
// so that this is counted as a one line difference, not two:
4339+
function getEffectiveLinesBetweenRanges(node1: TextRange, node2: TextRange) {
4340+
// If 'preserveNewlines' is disabled, skip the more accurate check that might require
4341+
// querying for source position twice.
4342+
if (!preserveNewlines) {
4343+
return getLinesBetweenRangeEndAndRangeStart(node1, node2, currentSourceFile!, /*includeComments*/ false);
4344+
}
4345+
// We start by measuring the line difference from node1's end to node2's comments start,
4346+
// so that this is counted as a one-line difference, not two:
43444347
//
43454348
// node1;
43464349
// // NODE2 COMMENT
43474350
// node2;
4348-
const lines = getLinesBetweenPositions(node1, node2, currentSourceFile!, /*includeComments*/ true);
4351+
const lines = getLinesBetweenRangeEndAndRangeStart(node1, node2, currentSourceFile!, /*includeComments*/ true);
43494352
if (lines === 0) {
43504353
// However, if the line difference considering node2's comments was 0, we might have this:
43514354
//
43524355
// node1; // NODE2 COMMENT
43534356
// node2;
43544357
//
4355-
// in which case we should be ignoring node2's comment.
4356-
return getLinesBetweenPositions(node1, node2, currentSourceFile!, /*includeComments*/ false);
4358+
// in which case we should be ignoring node2's comment, so this too is counted as
4359+
// a one-line difference, not zero.
4360+
return getLinesBetweenRangeEndAndRangeStart(node1, node2, currentSourceFile!, /*includeComments*/ false);
43574361
}
43584362
return lines;
43594363
}
@@ -4386,7 +4390,7 @@ namespace ts {
43864390
}
43874391

43884392
if (!nodeIsSynthesized(parent) && !nodeIsSynthesized(node1) && !nodeIsSynthesized(node2)) {
4389-
const lines = getEffectiveLinesBetweenRanges(node1, node2, getLinesBetweenRangeEndAndRangeStart);
4393+
const lines = getEffectiveLinesBetweenRanges(node1, node2);
43904394
return preserveNewlines ? lines : Math.min(lines, 1);
43914395
}
43924396

Diff for: src/compiler/utilities.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4703,7 +4703,7 @@ namespace ts {
47034703

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

0 commit comments

Comments
 (0)