Skip to content

Commit 2d37a44

Browse files
committed
Merge pull request #4646 from Microsoft/formattingJsxAttributes
handle jsx identifiers correctly, indent content of JsxSelfClosingEle…
2 parents 6c23a6b + 35d6086 commit 2d37a44

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

src/services/formatting/formattingScanner.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace ts.formatting {
1717
Scan,
1818
RescanGreaterThanToken,
1919
RescanSlashToken,
20-
RescanTemplateToken
20+
RescanTemplateToken,
21+
RescanJsxIdentifier
2122
}
2223

2324
export function getFormattingScanner(sourceFile: SourceFile, startPos: number, endPos: number): FormattingScanner {
@@ -108,6 +109,20 @@ namespace ts.formatting {
108109

109110
return false;
110111
}
112+
113+
function shouldRescanJsxIdentifier(node: Node): boolean {
114+
if (node.parent) {
115+
switch(node.parent.kind) {
116+
case SyntaxKind.JsxAttribute:
117+
case SyntaxKind.JsxOpeningElement:
118+
case SyntaxKind.JsxClosingElement:
119+
case SyntaxKind.JsxSelfClosingElement:
120+
return node.kind === SyntaxKind.Identifier;
121+
}
122+
}
123+
124+
return false;
125+
}
111126

112127
function shouldRescanSlashToken(container: Node): boolean {
113128
return container.kind === SyntaxKind.RegularExpressionLiteral;
@@ -141,7 +156,9 @@ namespace ts.formatting {
141156
? ScanAction.RescanSlashToken
142157
: shouldRescanTemplateToken(n)
143158
? ScanAction.RescanTemplateToken
144-
: ScanAction.Scan
159+
: shouldRescanJsxIdentifier(n)
160+
? ScanAction.RescanJsxIdentifier
161+
: ScanAction.Scan
145162

146163
if (lastTokenInfo && expectedScanAction === lastScanAction) {
147164
// readTokenInfo was called before with the same expected scan action.
@@ -176,6 +193,10 @@ namespace ts.formatting {
176193
currentToken = scanner.reScanTemplateToken();
177194
lastScanAction = ScanAction.RescanTemplateToken;
178195
}
196+
else if (expectedScanAction === ScanAction.RescanJsxIdentifier && currentToken === SyntaxKind.Identifier) {
197+
currentToken = scanner.scanJsxIdentifier();
198+
lastScanAction = ScanAction.RescanJsxIdentifier;
199+
}
179200
else {
180201
lastScanAction = ScanAction.Scan;
181202
}

src/services/formatting/smartIndenter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ namespace ts.formatting {
431431
case SyntaxKind.ArrayBindingPattern:
432432
case SyntaxKind.ObjectBindingPattern:
433433
case SyntaxKind.JsxElement:
434+
case SyntaxKind.JsxSelfClosingElement:
434435
case SyntaxKind.MethodSignature:
435436
case SyntaxKind.CallSignature:
436437
case SyntaxKind.ConstructSignature:
Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
//@Filename: file.tsx
4-
////function () {
4+
////function foo0() {
55
//// return (
66
//// <div className="commentBox" >
77
////Hello, World!/*autoformat*/
@@ -10,10 +10,76 @@
1010
//// )
1111
////}
1212
////
13+
////function foo1() {
14+
//// return (
15+
//// <div className="commentBox" data-id="test">
16+
////Hello, World!/*autoformat1*/
17+
/////*indent1*/
18+
//// </div>
19+
//// )
20+
////}
21+
////
22+
////function foo2() {
23+
//// return (
24+
//// <div data-name="commentBox"
25+
////class1= {/*1*/
26+
////}>/*2*/
27+
////Hello, World!/*autoformat2*/
28+
/////*indent2*/
29+
//// </div>
30+
//// )
31+
////}
32+
////function foo3() {
33+
//// return (
34+
//// <jsx-element className="commentBox"
35+
//// class2= {/*3*/
36+
//// }>/*4*/
37+
//// Hello, World!/*autoformat3*/
38+
//// /*indent3*/
39+
//// </jsx-element>
40+
//// )
41+
////}
42+
////function foo4() {
43+
//// return (
44+
//// <jsx-element className="commentBox"
45+
//// class3= {/*5*/
46+
//// }/>/*6*/
47+
//// )
48+
////}
1349

1450

1551
format.document();
1652
goTo.marker("autoformat");
1753
verify.currentLineContentIs(' Hello, World!');
1854
goTo.marker("indent");
19-
verify.indentationIs(12);
55+
verify.indentationIs(12);
56+
57+
goTo.marker("autoformat1");
58+
verify.currentLineContentIs(' Hello, World!');
59+
goTo.marker("indent1");
60+
verify.indentationIs(12);
61+
62+
goTo.marker("1");
63+
verify.currentLineContentIs(' class1= {');
64+
goTo.marker("2");
65+
verify.currentLineContentIs(' }>');
66+
67+
goTo.marker("autoformat2");
68+
verify.currentLineContentIs(' Hello, World!');
69+
goTo.marker("indent2");
70+
verify.indentationIs(12);
71+
72+
goTo.marker("3");
73+
verify.currentLineContentIs(' class2= {');
74+
goTo.marker("4");
75+
verify.currentLineContentIs(' }>');
76+
77+
goTo.marker("autoformat3");
78+
verify.currentLineContentIs(' Hello, World!');
79+
goTo.marker("indent3");
80+
verify.indentationIs(12);
81+
82+
goTo.marker("5");
83+
verify.currentLineContentIs(' class3= {');
84+
goTo.marker("6");
85+
verify.currentLineContentIs(' }/>');

0 commit comments

Comments
 (0)