Skip to content

Commit

Permalink
correctly print exotic JSX names
Browse files Browse the repository at this point in the history
  • Loading branch information
tsnobip committed Oct 25, 2023
1 parent c8cfd63 commit a4aaa55
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
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" />
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

0 comments on commit a4aaa55

Please sign in to comment.