Skip to content

Commit a2e9e69

Browse files
committed
Fix preserveNewlines printer option when a list child has the same start or end as its parent
1 parent 5a79169 commit a2e9e69

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

src/compiler/emitter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4301,6 +4301,7 @@ namespace ts {
43014301
return getEffectiveLines(
43024302
includeComments => getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(
43034303
firstChild.pos,
4304+
parentNode.pos,
43044305
currentSourceFile!,
43054306
includeComments));
43064307
}
@@ -4358,6 +4359,7 @@ namespace ts {
43584359
return getEffectiveLines(
43594360
includeComments => getLinesBetweenPositionAndNextNonWhitespaceCharacter(
43604361
lastChild.end,
4362+
parentNode.end,
43614363
currentSourceFile!,
43624364
includeComments));
43634365
}

src/compiler/utilities.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4776,19 +4776,19 @@ namespace ts {
47764776
return positionIsSynthesized(range.pos) ? -1 : skipTrivia(sourceFile.text, range.pos, /*stopAfterLineBreak*/ false, includeComments);
47774777
}
47784778

4779-
export function getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(pos: number, sourceFile: SourceFile, includeComments?: boolean) {
4779+
export function getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(pos: number, stopPos: number, sourceFile: SourceFile, includeComments?: boolean) {
47804780
const startPos = skipTrivia(sourceFile.text, pos, /*stopAfterLineBreak*/ false, includeComments);
4781-
const prevPos = getPreviousNonWhitespacePosition(startPos, sourceFile);
4781+
const prevPos = getPreviousNonWhitespacePosition(startPos, stopPos, sourceFile);
47824782
return getLinesBetweenPositions(sourceFile, prevPos || 0, startPos);
47834783
}
47844784

4785-
export function getLinesBetweenPositionAndNextNonWhitespaceCharacter(pos: number, sourceFile: SourceFile, includeComments?: boolean) {
4785+
export function getLinesBetweenPositionAndNextNonWhitespaceCharacter(pos: number, stopPos: number, sourceFile: SourceFile, includeComments?: boolean) {
47864786
const nextPos = skipTrivia(sourceFile.text, pos, /*stopAfterLineBreak*/ false, includeComments);
4787-
return getLinesBetweenPositions(sourceFile, pos, nextPos);
4787+
return getLinesBetweenPositions(sourceFile, pos, Math.min(stopPos, nextPos));
47884788
}
47894789

4790-
function getPreviousNonWhitespacePosition(pos: number, sourceFile: SourceFile) {
4791-
while (pos-- > 0) {
4790+
function getPreviousNonWhitespacePosition(pos: number, stopPos = 0, sourceFile: SourceFile) {
4791+
while (pos-- > stopPos) {
47924792
if (!isWhiteSpaceLike(sourceFile.text.charCodeAt(pos))) {
47934793
return pos;
47944794
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// #37813
2+
3+
/// <reference path="fourslash.ts" />
4+
5+
////function foo() {
6+
//// /*1*/var x: number
7+
////
8+
//// x = 10;
9+
//// return x;/*2*/
10+
////}
11+
12+
goTo.select("1", "2");
13+
edit.applyRefactor({
14+
refactorName: "Extract Symbol",
15+
actionName: "function_scope_1",
16+
actionDescription: "Extract to function in global scope",
17+
newContent:
18+
`function foo() {
19+
return /*RENAME*/newFunction();
20+
}
21+
22+
function newFunction() {
23+
var x: number;
24+
25+
x = 10;
26+
return x;
27+
}
28+
`
29+
});

0 commit comments

Comments
 (0)