Skip to content

Fix formatting for JSX fragment tags #20912

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
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
10 changes: 5 additions & 5 deletions src/services/formatting/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ namespace ts.formatting {
rule("SpaceAfterConstructor", SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken, [isOptionEnabled("insertSpaceAfterConstructor"), isNonJsxSameLineTokenContext], RuleAction.Space),
rule("NoSpaceAfterConstructor", SyntaxKind.ConstructorKeyword, SyntaxKind.OpenParenToken, [isOptionDisabledOrUndefined("insertSpaceAfterConstructor"), isNonJsxSameLineTokenContext], RuleAction.Delete),

rule("SpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionEnabled("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementContext, isNextTokenNotCloseBracket], RuleAction.Space),
rule("NoSpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementContext], RuleAction.Delete),
rule("SpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionEnabled("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNextTokenNotCloseBracket], RuleAction.Space),
rule("NoSpaceAfterComma", SyntaxKind.CommaToken, anyToken, [isOptionDisabledOrUndefined("insertSpaceAfterCommaDelimiter"), isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext], RuleAction.Delete),

// Insert space after function keyword for anonymous functions
rule("SpaceAfterAnonymousFunctionKeyword", SyntaxKind.FunctionKeyword, SyntaxKind.OpenParenToken, [isOptionEnabled("insertSpaceAfterFunctionKeywordForAnonymousFunctions"), isFunctionDeclContext], RuleAction.Space),
Expand Down Expand Up @@ -319,7 +319,7 @@ namespace ts.formatting {
"SpaceBetweenStatements",
[SyntaxKind.CloseParenToken, SyntaxKind.DoKeyword, SyntaxKind.ElseKeyword, SyntaxKind.CaseKeyword],
anyToken,
[isNonJsxSameLineTokenContext, isNonJsxElementContext, isNotForContext],
[isNonJsxSameLineTokenContext, isNonJsxElementOrFragmentContext, isNotForContext],
RuleAction.Space),
// This low-pri rule takes care of "try {" and "finally {" in case the rule SpaceBeforeOpenBraceInControl didn't execute on FormatOnEnter.
rule("SpaceAfterTryFinally", [SyntaxKind.TryKeyword, SyntaxKind.FinallyKeyword], SyntaxKind.OpenBraceToken, [isNonJsxSameLineTokenContext], RuleAction.Space),
Expand Down Expand Up @@ -614,8 +614,8 @@ namespace ts.formatting {
return context.TokensAreOnSameLine() && context.contextNode.kind !== SyntaxKind.JsxText;
}

function isNonJsxElementContext(context: FormattingContext): boolean {
return context.contextNode.kind !== SyntaxKind.JsxElement;
function isNonJsxElementOrFragmentContext(context: FormattingContext): boolean {
return context.contextNode.kind !== SyntaxKind.JsxElement && context.contextNode.kind !== SyntaxKind.JsxFragment;
}

function isJsxExpressionContext(context: FormattingContext): boolean {
Expand Down
3 changes: 3 additions & 0 deletions src/services/formatting/smartIndenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ namespace ts.formatting {
case SyntaxKind.ArrayBindingPattern:
case SyntaxKind.ObjectBindingPattern:
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.JsxOpeningFragment:
case SyntaxKind.JsxSelfClosingElement:
case SyntaxKind.JsxExpression:
case SyntaxKind.MethodSignature:
Expand Down Expand Up @@ -554,6 +555,8 @@ namespace ts.formatting {
(!!(<ImportClause>child).namedBindings && (<ImportClause>child).namedBindings.kind !== SyntaxKind.NamedImports);
case SyntaxKind.JsxElement:
return childKind !== SyntaxKind.JsxClosingElement;
case SyntaxKind.JsxFragment:
return childKind !== SyntaxKind.JsxClosingFragment;
}
// No explicit rule for given nodes so the result will follow the default value argument
return indentByDefault;
Expand Down
26 changes: 26 additions & 0 deletions tests/cases/fourslash/formattingJsxElements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
//// )
////}
////
////const bar = (
//// <>
//// /*fragmentChildIndent*/<p>text</p>
//// </>
////);
////
////const bar2 = <>
//// <p>text</p>
//// /*fragmentClosingTagIndent*/</>;
////
////(function () {
//// return <div
////className=""/*attrAutoformat*/
Expand All @@ -68,8 +78,12 @@
////
////<div>,{integer}</div>;/*commaInJsxElement*/
////<div>, {integer}</div>;/*commaInJsxElement2*/
////<>,{integer}</>;/*commaInJsxFragment*/
////<>, {integer}</>;/*commaInJsxFragment2*/
////<span>)</span>;/*closingParenInJsxElement*/
////<span>) </span>;/*closingParenInJsxElement2*/
////<>)</>;/*closingParenInJsxFragment*/
////<>) </>;/*closingParenInJsxFragment2*/
////<Router routes = { 3 } / >;/*jsxExpressionSpaces*/
////<Router routes={ (3) } />;/*jsxExpressionSpaces2*/
////<Router routes={() => {}}/*jsxExpressionSpaces3*/
Expand Down Expand Up @@ -111,6 +125,10 @@ verify.currentLineContentIs(' class3={');
goTo.marker("6");
verify.currentLineContentIs(' } />');

goTo.marker("fragmentChildIndent");
verify.currentLineContentIs(" <p>text</p>");
goTo.marker("fragmentClosingTagIndent");
verify.currentLineContentIs("</>;");

goTo.marker("attrAutoformat");
verify.currentLineContentIs(' className=""');
Expand Down Expand Up @@ -139,10 +157,18 @@ goTo.marker("commaInJsxElement");
verify.currentLineContentIs("<div>,{integer}</div>;");
goTo.marker("commaInJsxElement2");
verify.currentLineContentIs("<div>, {integer}</div>;");
goTo.marker("commaInJsxFragment");
verify.currentLineContentIs("<>,{integer}</>;");
goTo.marker("commaInJsxFragment2");
verify.currentLineContentIs("<>, {integer}</>;");
goTo.marker("closingParenInJsxElement");
verify.currentLineContentIs("<span>)</span>;");
goTo.marker("closingParenInJsxElement2");
verify.currentLineContentIs("<span>) </span>;");
goTo.marker("closingParenInJsxFragment");
verify.currentLineContentIs("<>)</>;");
goTo.marker("closingParenInJsxFragment2");
verify.currentLineContentIs("<>) </>;");
goTo.marker("jsxExpressionSpaces");
verify.currentLineContentIs("<Router routes={3} />;");
goTo.marker("jsxExpressionSpaces2");
Expand Down