Skip to content

handle jsx identifiers correctly, indent content of JsxSelfClosingEle… #4646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/services/formatting/formattingScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace ts.formatting {
Scan,
RescanGreaterThanToken,
RescanSlashToken,
RescanTemplateToken
RescanTemplateToken,
RescanJsxIdentifier
}

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

return false;
}

function shouldRescanJsxIdentifier(node: Node): boolean {
if (node.parent) {
switch(node.parent.kind) {
case SyntaxKind.JsxAttribute:
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.JsxClosingElement:
case SyntaxKind.JsxSelfClosingElement:
return node.kind === SyntaxKind.Identifier;
}
}

return false;
}

function shouldRescanSlashToken(container: Node): boolean {
return container.kind === SyntaxKind.RegularExpressionLiteral;
Expand Down Expand Up @@ -141,7 +156,9 @@ namespace ts.formatting {
? ScanAction.RescanSlashToken
: shouldRescanTemplateToken(n)
? ScanAction.RescanTemplateToken
: ScanAction.Scan
: shouldRescanJsxIdentifier(n)
? ScanAction.RescanJsxIdentifier
: ScanAction.Scan

if (lastTokenInfo && expectedScanAction === lastScanAction) {
// readTokenInfo was called before with the same expected scan action.
Expand Down Expand Up @@ -176,6 +193,10 @@ namespace ts.formatting {
currentToken = scanner.reScanTemplateToken();
lastScanAction = ScanAction.RescanTemplateToken;
}
else if (expectedScanAction === ScanAction.RescanJsxIdentifier && currentToken === SyntaxKind.Identifier) {
currentToken = scanner.scanJsxIdentifier();
lastScanAction = ScanAction.RescanJsxIdentifier;
}
else {
lastScanAction = ScanAction.Scan;
}
Expand Down
1 change: 1 addition & 0 deletions src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ namespace ts.formatting {
case SyntaxKind.ArrayBindingPattern:
case SyntaxKind.ObjectBindingPattern:
case SyntaxKind.JsxElement:
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.MethodSignature:
case SyntaxKind.CallSignature:
case SyntaxKind.ConstructSignature:
Expand Down
70 changes: 68 additions & 2 deletions tests/cases/fourslash/formattingJsxElements.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path='fourslash.ts' />

//@Filename: file.tsx
////function () {
////function foo0() {
//// return (
//// <div className="commentBox" >
////Hello, World!/*autoformat*/
Expand All @@ -10,10 +10,76 @@
//// )
////}
////
////function foo1() {
//// return (
//// <div className="commentBox" data-id="test">
////Hello, World!/*autoformat1*/
/////*indent1*/
//// </div>
//// )
////}
////
////function foo2() {
//// return (
//// <div data-name="commentBox"
////class1= {/*1*/
////}>/*2*/
////Hello, World!/*autoformat2*/
/////*indent2*/
//// </div>
//// )
////}
////function foo3() {
//// return (
//// <jsx-element className="commentBox"
//// class2= {/*3*/
//// }>/*4*/
//// Hello, World!/*autoformat3*/
//// /*indent3*/
//// </jsx-element>
//// )
////}
////function foo4() {
//// return (
//// <jsx-element className="commentBox"
//// class3= {/*5*/
//// }/>/*6*/
//// )
////}


format.document();
goTo.marker("autoformat");
verify.currentLineContentIs(' Hello, World!');
goTo.marker("indent");
verify.indentationIs(12);
verify.indentationIs(12);

goTo.marker("autoformat1");
verify.currentLineContentIs(' Hello, World!');
goTo.marker("indent1");
verify.indentationIs(12);

goTo.marker("1");
verify.currentLineContentIs(' class1= {');
goTo.marker("2");
verify.currentLineContentIs(' }>');

goTo.marker("autoformat2");
verify.currentLineContentIs(' Hello, World!');
goTo.marker("indent2");
verify.indentationIs(12);

goTo.marker("3");
verify.currentLineContentIs(' class2= {');
goTo.marker("4");
verify.currentLineContentIs(' }>');

goTo.marker("autoformat3");
verify.currentLineContentIs(' Hello, World!');
goTo.marker("indent3");
verify.indentationIs(12);

goTo.marker("5");
verify.currentLineContentIs(' class3= {');
goTo.marker("6");
verify.currentLineContentIs(' }/>');