Skip to content

Commit faf128d

Browse files
Orta Theroxandrewbranch
andauthored
Fix formatting scanner on JSX text that looks like trivia (microsoft#39718)
* Fix formatting scanner on JSX text that looks like trivia * Combine if statements Co-authored-by: Andrew Branch <[email protected]>
1 parent bffe354 commit faf128d

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

src/compiler/utilities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ namespace ts {
483483
return node.pos;
484484
}
485485

486-
if (isJSDocNode(node)) {
486+
if (isJSDocNode(node) || node.kind === SyntaxKind.JsxText) {
487+
// JsxText cannot actually contain comments, even though the scanner will think it sees comments
487488
return skipTrivia((sourceFile || getSourceFileOfNode(node)).text, node.pos, /*stopAfterLineBreak*/ false, /*stopAtComments*/ true);
488489
}
489490

src/services/formatting/formatting.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,9 @@ namespace ts.formatting {
720720
// proceed any parent tokens that are located prior to child.getStart()
721721
const tokenInfo = formattingScanner.readTokenInfo(node);
722722
if (tokenInfo.token.end > childStartPos) {
723+
if (tokenInfo.token.pos > childStartPos) {
724+
formattingScanner.skipToStartOf(child);
725+
}
723726
// stop when formatting scanner advances past the beginning of the child
724727
break;
725728
}
@@ -731,13 +734,15 @@ namespace ts.formatting {
731734
return inheritedIndentation;
732735
}
733736

734-
// JSX text shouldn't affect indenting
735-
if (isToken(child) && child.kind !== SyntaxKind.JsxText) {
737+
if (isToken(child)) {
736738
// if child node is a token, it does not impact indentation, proceed it using parent indentation scope rules
737739
const tokenInfo = formattingScanner.readTokenInfo(child);
738-
Debug.assert(tokenInfo.token.end === child.end, "Token end is child end");
739-
consumeTokenAndAdvanceScanner(tokenInfo, node, parentDynamicIndentation, child);
740-
return inheritedIndentation;
740+
// JSX text shouldn't affect indenting
741+
if (child.kind !== SyntaxKind.JsxText) {
742+
Debug.assert(tokenInfo.token.end === child.end, "Token end is child end");
743+
consumeTokenAndAdvanceScanner(tokenInfo, node, parentDynamicIndentation, child);
744+
return inheritedIndentation;
745+
}
741746
}
742747

743748
const effectiveParentStartLine = child.kind === SyntaxKind.Decorator ? childStartLine : undecoratedParentStartLine;

src/services/formatting/formattingScanner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace ts.formatting {
1212
getCurrentLeadingTrivia(): TextRangeWithKind[] | undefined;
1313
lastTrailingTriviaWasNewLine(): boolean;
1414
skipToEndOf(node: Node): void;
15+
skipToStartOf(node: Node): void;
1516
}
1617

1718
const enum ScanAction {
@@ -47,6 +48,7 @@ namespace ts.formatting {
4748
getCurrentLeadingTrivia: () => leadingTrivia,
4849
lastTrailingTriviaWasNewLine: () => wasNewLine,
4950
skipToEndOf,
51+
skipToStartOf,
5052
});
5153

5254
lastTokenInfo = undefined;
@@ -298,5 +300,15 @@ namespace ts.formatting {
298300
leadingTrivia = undefined;
299301
trailingTrivia = undefined;
300302
}
303+
304+
function skipToStartOf(node: Node): void {
305+
scanner.setTextPos(node.pos);
306+
savedPos = scanner.getStartPos();
307+
lastScanAction = undefined;
308+
lastTokenInfo = undefined;
309+
wasNewLine = false;
310+
leadingTrivia = undefined;
311+
trailingTrivia = undefined;
312+
}
301313
}
302314
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// <reference path="fourslash.ts"/>
2+
// @Filename: foo.tsx
3+
////const a = <div>
4+
//// // <a />
5+
////</div>
6+
7+
format.document();
8+
verify.currentFileContentIs(`const a = <div>
9+
// <a />
10+
</div>`);

0 commit comments

Comments
 (0)