Skip to content

Commit 99b771c

Browse files
Merge pull request #4398 from SaschaNaz/formatTsxAttr
Fixing JSX/TSX closing tag/attribute/expression formatting
2 parents 72bb1d3 + 9e3ee5d commit 99b771c

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

src/services/formatting/formatting.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ namespace ts.formatting {
360360
range: TextRange,
361361
inheritedIndentation: number): number {
362362

363-
if (rangeOverlapsWithStartEnd(range, startPos, endPos)) {
363+
if (rangeOverlapsWithStartEnd(range, startPos, endPos) ||
364+
rangeContainsStartEnd(range, startPos, endPos) /* Not to miss zero-range nodes e.g. JsxText */) {
365+
364366
if (inheritedIndentation !== Constants.Unknown) {
365367
return inheritedIndentation;
366368
}

src/services/formatting/smartIndenter.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,9 @@ namespace ts.formatting {
450450
case SyntaxKind.ConditionalExpression:
451451
case SyntaxKind.ArrayBindingPattern:
452452
case SyntaxKind.ObjectBindingPattern:
453-
case SyntaxKind.JsxElement:
453+
case SyntaxKind.JsxOpeningElement:
454454
case SyntaxKind.JsxSelfClosingElement:
455+
case SyntaxKind.JsxExpression:
455456
case SyntaxKind.MethodSignature:
456457
case SyntaxKind.CallSignature:
457458
case SyntaxKind.ConstructSignature:
@@ -467,6 +468,7 @@ namespace ts.formatting {
467468
return false;
468469
}
469470

471+
/* @internal */
470472
export function nodeWillIndentChild(parent: TextRangeWithKind, child: TextRangeWithKind, indentByDefault: boolean) {
471473
let childKind = child ? child.kind : SyntaxKind.Unknown;
472474
switch (parent.kind) {
@@ -484,6 +486,8 @@ namespace ts.formatting {
484486
case SyntaxKind.GetAccessor:
485487
case SyntaxKind.SetAccessor:
486488
return childKind !== SyntaxKind.Block;
489+
case SyntaxKind.JsxElement:
490+
return childKind !== SyntaxKind.JsxClosingElement;
487491
}
488492
// No explicit rule for given nodes so the result will follow the default value argument
489493
return indentByDefault;

tests/cases/fourslash/formattingJsxElements.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//// </div>
1010
//// )
1111
////}
12-
////
12+
////
1313
////function foo1() {
1414
//// return (
1515
//// <div className="commentBox" data-id="test">
@@ -45,8 +45,26 @@
4545
//// class3= {/*5*/
4646
//// }/>/*6*/
4747
//// )
48+
////}
49+
////
50+
////(function () {
51+
//// return <div
52+
////className=""/*attrAutoformat*/
53+
/////*attrIndent*/
54+
////id={
55+
////"abc" + "cde"/*expressionAutoformat*/
56+
/////*expressionIndent*/
4857
////}
49-
58+
//// >/*danglingBracketAutoformat*/
59+
//// </div>/*closingTagAutoformat*/
60+
////})
61+
////
62+
////let h5 = <h5>
63+
////<span>/*childJsxElementAutoformat*/
64+
/////*childJsxElementIndent*/
65+
////<span></span>/*grandchildJsxElementAutoformat*/
66+
////</span>/*containedClosingTagAutoformat*/
67+
////</h5>
5068

5169
format.document();
5270
goTo.marker("autoformat");
@@ -83,3 +101,28 @@ goTo.marker("5");
83101
verify.currentLineContentIs(' class3= {');
84102
goTo.marker("6");
85103
verify.currentLineContentIs(' }/>');
104+
105+
106+
goTo.marker("attrAutoformat");
107+
verify.currentLineContentIs(' className=""');
108+
goTo.marker("attrIndent");
109+
verify.indentationIs(8);
110+
goTo.marker("expressionAutoformat");
111+
verify.currentLineContentIs(' "abc" + "cde"');
112+
goTo.marker("expressionIndent");
113+
verify.indentationIs(12);
114+
115+
goTo.marker("danglingBracketAutoformat")
116+
// TODO: verify.currentLineContentIs(" >");
117+
verify.currentLineContentIs(" >");
118+
goTo.marker("closingTagAutoformat");
119+
verify.currentLineContentIs(" </div>");
120+
121+
goTo.marker("childJsxElementAutoformat");
122+
verify.currentLineContentIs(" <span>");
123+
goTo.marker("childJsxElementIndent");
124+
verify.indentationIs(8);
125+
goTo.marker("grandchildJsxElementAutoformat");
126+
verify.currentLineContentIs(" <span></span>");
127+
goTo.marker("containedClosingTagAutoformat");
128+
verify.currentLineContentIs(" </span>");

0 commit comments

Comments
 (0)