Skip to content

feat: add jsxExpressionContainer.preferSingleLine to keep the braces on their own lines when written that way - #892

Merged
dsherret merged 1 commit into
mainfrom
feat-jsx-expr-container-maintain-newlines
Nov 4, 2026
Merged

dsherret merged 1 commit into
mainfrom
feat-jsx-expr-container-maintain-newlines

Conversation

@dsherret

@dsherret dsherret commented Nov 4, 2026

Copy link
Copy Markdown
Member

Closes #577

Adds jsxExpressionContainer.preferSingleLine. When it's false, a JSX expression container (in children or an attribute value) that has a newline after its { keeps its braces on their own lines, with the expression indented between them:

<div
  render={
    work(
      nice,
      to,
      have,
    )
  }
>
  {
    isThisConditionOk
      ? (
        <p>Yes</p>
      )
      : (
        <p>No</p>
      )
  }
</div>

Without a newline after {, the braces hug the expression as before. This is how objects and the other preferSingleLine nodes keep line breaks. Only the newline after { decides, so {\n cond ? a : b} also puts the } on its own line.

Like importDeclaration.preferSingleLine and exportDeclaration.preferSingleLine, it falls back to the global preferSingleLine when that's set and is otherwise true, so default output doesn't change. With true, the braces hug even when the content spans multiple lines, as before.

With jsx.multiLineParens: always, a newline after { is ignored when the expression is JSX, since it's already wrapped in {( ... )}.

This also fixes multi-line JSX right after a comment in a container ({\n// comment\n<span>...</span>\n}), which got blank lines around it and changed on a second format.

Schema

The per-node preferSingleLine properties shared a definition that says the default is false, which was wrong for importDeclaration.preferSingleLine and exportDeclaration.preferSingleLine. They and the new option now use a preferSingleLineDefaultTrue definition.

Tests

  • issue0577.txt has the issue's examples.
  • JsxExpressionContainer_PreferSingleLine_False.txt covers children, attributes, conditionals, &&, arrow functions, object literals, text siblings, nested containers, comments, and empty and spread containers.
  • _Default, _True and _Global_False show the default is unchanged, the per-node value wins, and the fallback to the global preferSingleLine.
  • Combinations with spaceSurroundingExpression, jsx.multiLineParens always and never, and useTabs.
  • A unit test in resolve_config.rs covers how the option resolves.

In the regression corpus I use, nothing changed.

…n written that way

Adds `jsxExpressionContainer.preferSingleLine`. When it's `false`, a JSX
expression container (children and attribute values) with a newline between
its open brace and expression gets its braces on their own lines with the
expression indented between them. Only the newline after the open brace is
checked. The option falls back to the global `preferSingleLine` when that's
set and is otherwise `true`, which keeps the braces beside the expression
like before, so the default output doesn't change.

Multi-line JSX directly in a container no longer adds its own newlines when
the container already puts its braces on their own lines, which also fixes
blank lines and unstable output for multi-line JSX after a comment in a
container (ex. `{\n// comment\n<div>...</div>\n}`).

The schema's `importDeclaration.preferSingleLine` and
`exportDeclaration.preferSingleLine` now point at a new
`preferSingleLineDefaultTrue` definition because they also default to `true`
when the global `preferSingleLine` isn't set, rather than the shared
definition's `false`.

Closes #577
@dsherret
dsherret merged commit 2e8e164 into main Nov 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep bracket of jsx expression in a single new line

1 participant