Skip to content

Commit

Permalink
Fix formatter's processChildNodes (#48921)
Browse files Browse the repository at this point in the history
processChildNodes needs to skip processing when the node array is
outside the target range, just like processChildNode already does for a
single node.

Fixes #48006
  • Loading branch information
sandersn authored May 2, 2022
1 parent 63a941d commit e73d755
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,13 @@ namespace ts.formatting {

let listDynamicIndentation = parentDynamicIndentation;
let startLine = parentStartLine;
// node range is outside the target range - do not dive inside
if (!rangeOverlapsWithStartEnd(originalRange, nodes.pos, nodes.end)) {
if (nodes.end < originalRange.pos) {
formattingScanner.skipToEndOf(nodes);
}
return;
}

if (listStartToken !== SyntaxKind.Unknown) {
// introduce a new indentation scope for lists (including list start and end tokens)
Expand Down
4 changes: 2 additions & 2 deletions src/services/formatting/formattingScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace ts.formatting {
readEOFTokenRange(): TextRangeWithKind;
getCurrentLeadingTrivia(): TextRangeWithKind[] | undefined;
lastTrailingTriviaWasNewLine(): boolean;
skipToEndOf(node: Node): void;
skipToEndOf(node: Node | NodeArray<Node>): void;
skipToStartOf(node: Node): void;
}

Expand Down Expand Up @@ -286,7 +286,7 @@ namespace ts.formatting {
return tokenInfo;
}

function skipToEndOf(node: Node): void {
function skipToEndOf(node: Node | NodeArray<Node>): void {
scanner.setTextPos(node.end);
savedPos = scanner.getStartPos();
lastScanAction = undefined;
Expand Down
9 changes: 9 additions & 0 deletions tests/cases/fourslash/formatAfterPasteInString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference path='fourslash.ts' />

//// /*2*/const x = f('aa/*1*/a').x()

goTo.marker('1');
edit.paste("bb");
format.document();
goTo.marker('2');
verify.currentLineContentIs("const x = f('aabba').x()");

0 comments on commit e73d755

Please sign in to comment.