Skip to content

Commit c563e83

Browse files
authored
Merge pull request #13092 from SaschaNaz/jsxdangling
Format JSX expression and closing token
2 parents e8b3ff0 + 784f29b commit c563e83

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

src/services/formatting/formatting.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,14 +494,26 @@ namespace ts.formatting {
494494
case SyntaxKind.WhileKeyword:
495495
case SyntaxKind.AtToken:
496496
return indentation;
497+
case SyntaxKind.SlashToken:
498+
case SyntaxKind.GreaterThanToken: {
499+
if (container.kind === SyntaxKind.JsxOpeningElement ||
500+
container.kind === SyntaxKind.JsxClosingElement ||
501+
container.kind === SyntaxKind.JsxSelfClosingElement
502+
) {
503+
return indentation;
504+
}
505+
break;
506+
}
497507
case SyntaxKind.OpenBracketToken:
498-
case SyntaxKind.CloseBracketToken:
499-
return (container.kind === SyntaxKind.MappedType) ?
500-
indentation + getEffectiveDelta(delta, container) : indentation;
501-
default:
502-
// if token line equals to the line of containing node (this is a first token in the node) - use node indentation
503-
return nodeStartLine !== line ? indentation + getEffectiveDelta(delta, container) : indentation;
508+
case SyntaxKind.CloseBracketToken: {
509+
if (container.kind !== SyntaxKind.MappedType) {
510+
return indentation;
511+
}
512+
break;
513+
}
504514
}
515+
// if token line equals to the line of containing node (this is a first token in the node) - use node indentation
516+
return nodeStartLine !== line ? indentation + getEffectiveDelta(delta, container) : indentation;
505517
},
506518
getIndentation: () => indentation,
507519
getDelta: child => getEffectiveDelta(delta, child),

src/services/formatting/rules.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,11 +710,18 @@ namespace ts.formatting {
710710
case SyntaxKind.ClassDeclaration:
711711
case SyntaxKind.ModuleDeclaration:
712712
case SyntaxKind.EnumDeclaration:
713-
case SyntaxKind.Block:
714713
case SyntaxKind.CatchClause:
715714
case SyntaxKind.ModuleBlock:
716715
case SyntaxKind.SwitchStatement:
717716
return true;
717+
case SyntaxKind.Block: {
718+
const blockParent = context.currentTokenParent.parent;
719+
if (blockParent.kind !== SyntaxKind.ArrowFunction &&
720+
blockParent.kind !== SyntaxKind.FunctionExpression
721+
) {
722+
return true;
723+
}
724+
}
718725
}
719726
return false;
720727
}

tests/cases/fourslash/formatVariableDeclarationList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ verify.currentLineContentIs(" x = 'Foo';");
3737
goTo.marker("11");
3838
verify.currentLineContentIs(" return fun;");
3939
goTo.marker("12");
40-
verify.currentLineContentIs(" } (fun1));");
40+
verify.currentLineContentIs(" }(fun1));");

tests/cases/fourslash/formattingJsxElements.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
////<span>) </span>;/*closingParenInJsxElement2*/
7373
////<Router routes = { 3 } / >;/*jsxExpressionSpaces*/
7474
////<Router routes={ (3) } />;/*jsxExpressionSpaces2*/
75+
////<Router routes={() => {}}/*jsxExpressionSpaces3*/
76+
/////>;/*jsxDanglingSelfClosingToken*/
7577

7678
format.document();
7779
goTo.marker("autoformat");
@@ -120,8 +122,7 @@ goTo.marker("expressionIndent");
120122
verify.indentationIs(12);
121123

122124
goTo.marker("danglingBracketAutoformat")
123-
// TODO: verify.currentLineContentIs(" >");
124-
verify.currentLineContentIs(" >");
125+
verify.currentLineContentIs(" >");
125126
goTo.marker("closingTagAutoformat");
126127
verify.currentLineContentIs(" </div>");
127128

@@ -145,4 +146,8 @@ verify.currentLineContentIs("<span>) </span>;");
145146
goTo.marker("jsxExpressionSpaces");
146147
verify.currentLineContentIs("<Router routes={3} />;");
147148
goTo.marker("jsxExpressionSpaces2");
148-
verify.currentLineContentIs("<Router routes={(3)} />;");
149+
verify.currentLineContentIs("<Router routes={(3)} />;");
150+
goTo.marker("jsxExpressionSpaces3");
151+
verify.currentLineContentIs("<Router routes={() => { }}");
152+
goTo.marker("jsxDanglingSelfClosingToken");
153+
verify.currentLineContentIs("/>;");

0 commit comments

Comments
 (0)