Skip to content

Commit e73d755

Browse files
authored
Fix formatter's processChildNodes (microsoft#48921)
processChildNodes needs to skip processing when the node array is outside the target range, just like processChildNode already does for a single node. Fixes microsoft#48006
1 parent 63a941d commit e73d755

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/services/formatting/formatting.ts

+7
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,13 @@ namespace ts.formatting {
778778

779779
let listDynamicIndentation = parentDynamicIndentation;
780780
let startLine = parentStartLine;
781+
// node range is outside the target range - do not dive inside
782+
if (!rangeOverlapsWithStartEnd(originalRange, nodes.pos, nodes.end)) {
783+
if (nodes.end < originalRange.pos) {
784+
formattingScanner.skipToEndOf(nodes);
785+
}
786+
return;
787+
}
781788

782789
if (listStartToken !== SyntaxKind.Unknown) {
783790
// introduce a new indentation scope for lists (including list start and end tokens)

src/services/formatting/formattingScanner.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts.formatting {
1212
readEOFTokenRange(): TextRangeWithKind;
1313
getCurrentLeadingTrivia(): TextRangeWithKind[] | undefined;
1414
lastTrailingTriviaWasNewLine(): boolean;
15-
skipToEndOf(node: Node): void;
15+
skipToEndOf(node: Node | NodeArray<Node>): void;
1616
skipToStartOf(node: Node): void;
1717
}
1818

@@ -286,7 +286,7 @@ namespace ts.formatting {
286286
return tokenInfo;
287287
}
288288

289-
function skipToEndOf(node: Node): void {
289+
function skipToEndOf(node: Node | NodeArray<Node>): void {
290290
scanner.setTextPos(node.end);
291291
savedPos = scanner.getStartPos();
292292
lastScanAction = undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// /*2*/const x = f('aa/*1*/a').x()
4+
5+
goTo.marker('1');
6+
edit.paste("bb");
7+
format.document();
8+
goTo.marker('2');
9+
verify.currentLineContentIs("const x = f('aabba').x()");

0 commit comments

Comments
 (0)