-
Notifications
You must be signed in to change notification settings - Fork 471
Wrap non-components JSX children in curly braces #7863
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
base: master
Are you sure you want to change the base?
Conversation
res
<span> hello </span>
should be printed as
res
<span>{hello}</span>
and
res
<span> hello <span/></span>
should be printed as
```res
<span>{hello}<s...
... Fixed spread variables in JSX children to be wrapped in curly braces. The change ensures |
312375d
to
e6ebbb9
Compare
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
e6ebbb9
to
d5cc8ea
Compare
According to the PR description, let x = <span> hello </span> should be reformatted to let x = <span>{hello}</span> but instead it does keep the spaces: let x = <span> {hello} </span> This is not ideal, because in JavaScript JSX those spaces would actually go into the output: |
Yes whitespace is a big issue if we start having string templates. |
Maybe wrap stuff in "" if there are spaces between words, but trim the outer spaces away. Could be similar to how polymorphic variants do it.
|
This PR modifies the ReScript printer to wrap variables (identifiers) in curly braces when they appear as JSX children, making the output consistent with standard JSX conventions.
Problem
Previously, variables inside JSX children were printed without braces:
This is inconsistent with JSX standards where variables and spread expressions should be wrapped in braces to distinguish them from text content.
Solution
With this change, variables and constants are now wrapped in curly braces:
Implementation
The fix involves surgical changes to the
jsx_child_expr
function incompiler/syntax/src/res_parens.ml
:Pexp_ident _
(variables/identifiers) from the pattern that returnedNothing
, causing them to fall through to the default case that returnsParenthesized
, which wraps them in braces.What Changes
Now wrapped in braces:
hello
→{hello}
someVar
→{someVar}
"hello"
->{"hello"}
obj.field
->{obj.field}
Some(x)
->{Some(x)}
%raw("eval()")
->{%raw("eval()")}
{"x": 1, "y": 2}
->{{"x": 1, "y": 2}}
Already had braces (no change):
{func()}
stays{func()}
{a + b}
stays{a + b}
** Remaining to support **
Testing
All existing syntax tests have been updated to reflect the new behavior. The changes are minimal and targeted, affecting only variables in JSX children while preserving existing behavior for all other expression types.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.