Skip to content

Commit c689617

Browse files
committed
Progress
1 parent 2b9e4aa commit c689617

File tree

3 files changed

+52
-15
lines changed

3 files changed

+52
-15
lines changed

src/compiler/emitter.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4271,8 +4271,9 @@ namespace ts {
42714271
if (firstChild === undefined) {
42724272
return rangeIsOnSingleLine(parentNode, currentSourceFile!) ? 0 : 1;
42734273
}
4274-
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(firstChild)) {
4275-
return getLinesBetweenRangeEndAndRangeStart(parentNode, firstChild, currentSourceFile!);
4274+
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(firstChild) && firstChild.parent === parentNode) {
4275+
const lines = getEffectiveLinesBetweenRanges(parentNode, firstChild, getLinesBetweenRangeStartPositions);
4276+
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
42764277
}
42774278
else if (synthesizedNodeStartsOnNewLine(firstChild, format)) {
42784279
return 1;
@@ -4286,8 +4287,9 @@ namespace ts {
42864287
if (previousNode === undefined || nextNode === undefined) {
42874288
return 0;
42884289
}
4289-
else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode)) {
4290-
return getLinesBetweenRangeEndAndRangeStart(previousNode, nextNode, currentSourceFile!);
4290+
else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode) && previousNode.parent === nextNode.parent) {
4291+
const lines = getEffectiveLinesBetweenRanges(previousNode, nextNode, getLinesBetweenRangeEndAndRangeStart);
4292+
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
42914293
}
42924294
else if (synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format)) {
42934295
return 1;
@@ -4309,19 +4311,44 @@ namespace ts {
43094311
if (lastChild === undefined) {
43104312
return rangeIsOnSingleLine(parentNode, currentSourceFile!) ? 0 : 1;
43114313
}
4312-
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(lastChild)) {
4313-
return getLinesBetweenRangeEndAndRangeStart(parentNode, lastChild, currentSourceFile!);
4314+
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(lastChild) && lastChild.parent === parentNode) {
4315+
const lines = getLinesBetweenRangeEndPositions(lastChild, parentNode, currentSourceFile!);
4316+
return printerOptions.preserveNewlines ? lines : Math.min(lines, 1);
43144317
}
4315-
else {
4316-
return Number(synthesizedNodeStartsOnNewLine(lastChild, format));
4318+
else if (synthesizedNodeStartsOnNewLine(lastChild, format)) {
4319+
return 1;
43174320
}
43184321
}
4319-
if (format & ListFormat.MultiLine) {
4320-
return (format & ListFormat.NoTrailingNewLine) === 0 ? 1 : 0;
4322+
if (format & ListFormat.MultiLine && !(format & ListFormat.NoTrailingNewLine)) {
4323+
return 1;
43214324
}
43224325
return 0;
43234326
}
43244327

4328+
function getEffectiveLinesBetweenRanges(
4329+
node1: TextRange,
4330+
node2: TextRange,
4331+
getLinesBetweenPositions: (range1: TextRange, range2: TextRange, sourceFile: SourceFile, includeComments: boolean) => number
4332+
) {
4333+
// We start by measuring the line difference from parentNode's start to node2's comments start,
4334+
// so that this is counted as a one line difference, not two:
4335+
//
4336+
// function node1() {
4337+
// // NODE2 COMMENT
4338+
// node2;
4339+
const lines = getLinesBetweenPositions(node1, node2, currentSourceFile!, /*includeComments*/ true);
4340+
if (lines === 0) {
4341+
// However, if the line difference considering node2's comments was 0, we might have this:
4342+
//
4343+
// function node1() { // NODE2 COMMENT
4344+
// node2;
4345+
//
4346+
// in which case we should be ignoring node2's comment.
4347+
return getLinesBetweenPositions(node1, node2, currentSourceFile!, /*includeComments*/ false);
4348+
}
4349+
return lines;
4350+
}
4351+
43254352
function synthesizedNodeStartsOnNewLine(node: Node, format: ListFormat) {
43264353
if (nodeIsSynthesized(node)) {
43274354
const startsOnNewLine = getStartsOnNewLine(node);

src/compiler/utilities.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4676,8 +4676,16 @@ namespace ts {
46764676
return positionsAreOnSameLine(range1.end, getStartPositionOfRange(range2, sourceFile), sourceFile);
46774677
}
46784678

4679-
export function getLinesBetweenRangeEndAndRangeStart(range1: TextRange, range2: TextRange, sourceFile: SourceFile) {
4680-
return getLineOfLocalPosition(sourceFile, getStartPositionOfRange(range2, sourceFile)) - getLineOfLocalPosition(sourceFile, range1.end);
4679+
export function getLinesBetweenRangeEndAndRangeStart(range1: TextRange, range2: TextRange, sourceFile: SourceFile, includeSecondRangeComments: boolean) {
4680+
return getLineOfLocalPosition(sourceFile, getStartPositionOfRange(range2, sourceFile, includeSecondRangeComments)) - getLineOfLocalPosition(sourceFile, range1.end);
4681+
}
4682+
4683+
export function getLinesBetweenRangeStartPositions(range1: TextRange, range2: TextRange, sourceFile: SourceFile, includeSecondRangeComments: boolean) {
4684+
return getLineOfLocalPosition(sourceFile, getStartPositionOfRange(range2, sourceFile, includeSecondRangeComments)) - getLineOfLocalPosition(sourceFile, getStartPositionOfRange(range1, sourceFile));
4685+
}
4686+
4687+
export function getLinesBetweenRangeEndPositions(range1: TextRange, range2: TextRange, sourceFile: SourceFile) {
4688+
return getLineOfLocalPosition(sourceFile, range2.end) - getLineOfLocalPosition(sourceFile, range1.end);
46814689
}
46824690

46834691
export function isNodeArrayMultiLine(list: NodeArray<Node>, sourceFile: SourceFile): boolean {
@@ -4689,8 +4697,8 @@ namespace ts {
46894697
getLineOfLocalPosition(sourceFile, pos1) === getLineOfLocalPosition(sourceFile, pos2);
46904698
}
46914699

4692-
export function getStartPositionOfRange(range: TextRange, sourceFile: SourceFile) {
4693-
return positionIsSynthesized(range.pos) ? -1 : skipTrivia(sourceFile.text, range.pos);
4700+
export function getStartPositionOfRange(range: TextRange, sourceFile: SourceFile, includeComments?: boolean) {
4701+
return positionIsSynthesized(range.pos) ? -1 : skipTrivia(sourceFile.text, range.pos, /*stopAfterLineBreak*/ false, includeComments);
46944702
}
46954703

46964704
/**

src/harness/fourslashImpl.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2669,7 +2669,9 @@ namespace FourSlash {
26692669
const oldText = this.tryGetFileContent(change.fileName);
26702670
ts.Debug.assert(!!change.isNewFile === (oldText === undefined));
26712671
const newContent = change.isNewFile ? ts.first(change.textChanges).newText : ts.textChanges.applyChanges(oldText!, change.textChanges);
2672-
assert.equal(newContent, expectedNewContent, `String mis-matched in file ${change.fileName}`);
2672+
if (newContent !== expectedNewContent) {
2673+
assert.fail(undefined, undefined, `String mis-matched in file ${change.fileName}: ${showTextDiff(expectedNewContent, newContent)}`);
2674+
}
26732675
}
26742676
for (const newFileName in newFileContent) {
26752677
ts.Debug.assert(changes.some(c => c.fileName === newFileName), "No change in file", () => newFileName);

0 commit comments

Comments
 (0)