Skip to content
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

correctly print exotic JSX names #6451

Merged
merged 2 commits into from
Oct 26, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- Fix using dynamic import of module in block instead of async function https://github.com/rescript-lang/rescript-compiler/pull/6434
- Fix issue with using dynamic import of module in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/6434
- Fix build error where JSX v4 transformation of the discouraged forwardRef in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/6447
- Fix printing of exotic JSX names https://github.com/rescript-lang/rescript-compiler/pull/6451

#### :nail_care: Polish

Expand Down
12 changes: 6 additions & 6 deletions jscomp/syntax/src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4354,19 +4354,19 @@ and printJsxProp ~state arg cmtTbl =
* Navabar.createElement -> Navbar
* Staff.Users.createElement -> Staff.Users *)
and printJsxName {txt = lident} =
let printIdent = printIdentLike ~allowUident:true in
let rec flatten acc lident =
match lident with
| Longident.Lident txt -> txt :: acc
| Ldot (lident, txt) ->
let acc = if txt = "createElement" then acc else txt :: acc in
flatten acc lident
| Longident.Lident txt -> printIdent txt :: acc
| Ldot (lident, "createElement") -> flatten acc lident
| Ldot (lident, txt) -> flatten (printIdent txt :: acc) lident
| _ -> acc
in
match lident with
| Longident.Lident txt -> Doc.text txt
| Longident.Lident txt -> printIdent txt
| _ as lident ->
let segments = flatten [] lident in
Doc.join ~sep:Doc.dot (List.map Doc.text segments)
Doc.join ~sep:Doc.dot segments

and printArgumentsWithCallbackInFirstPosition ~dotted ~state args cmtTbl =
(* Because the same subtree gets printed twice, we need to copy the cmtTbl.
Expand Down
12 changes: 12 additions & 0 deletions jscomp/syntax/tests/printer/expr/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ let x = <Foo.Bar className="container" />
let x = <Foo.Bar.Baz className="container" />
let x = <Foo.bar className="container" />
let x = <Foo.baz className="multiline" />
let x = <\"custom-tag" className="container" />
let x = <Foo.\"custom-tag" className="container" />

// https://github.com/rescript-lang/syntax/issues/570
let x =
Expand Down Expand Up @@ -35,6 +37,16 @@ let x =
{a}
{b}
</A>
let x =
<\"custom-tag" className="container">
{a}
<B />
</\"custom-tag">
let x =
<Foo.\"custom-tag" className="container">
{a}
<B />
</Foo.\"custom-tag">

let x = <div className="container" className2="container2" className3="container3" onClick />

Expand Down
4 changes: 4 additions & 0 deletions jscomp/syntax/tests/printer/expr/jsx.res
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ let x = <Foo.bar className="container" />
let x = <Foo.baz
className="multiline"
/>
let x = <\"custom-tag" className="container" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note, but given that this looks like a close tag (but is not), are there other issues this could cause? How can we test those?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes readability suffers a bit with this, maybe we could change the parser to make it accept exotic names inside double quotes without escaping them with a \, like polymorphic variants, but this would be more work for something that is definitely a corner case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably will be easier to make <my-custom-tag className="hello" /> compile in the case

let x = <Foo.\"custom-tag" className="container" />

// https://github.com/rescript-lang/syntax/issues/570
let x = <A> <B> <C> <D /> <E /> </C> <F> <G /> <H /> </F> </B> </A>
let x = <A> {children} <B/> </A>
let x = <A> <B/> {children} </A>
let x = <A> {a} </A>
let x = <A> {a} {b} </A>
let x = <\"custom-tag" className="container" > {a} <B/> </\"custom-tag">
let x = <Foo.\"custom-tag" className="container" > {a} <B/> </Foo.\"custom-tag">

let x =
<div
Expand Down