Skip to content
Draft
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
21 changes: 19 additions & 2 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,21 @@ and parse_jsx_prop p : Parsetree.jsx_prop option =
and parse_jsx_props p : Parsetree.jsx_prop list =
parse_region ~grammar:Grammar.JsxAttribute ~f:parse_jsx_prop p

and wrap_jsx_string_literal expr =
(* Check if the expression is a string literal and wrap it with Jsx.string() *)
match expr.Parsetree.pexp_desc with
| Pexp_constant (Pconst_string (s, delimiter)) ->
let jsx_string_ident =
Ast_helper.Exp.ident
~loc:expr.pexp_loc
(Location.mkloc (Longident.Ldot (Longident.Lident "Jsx", "string")) expr.pexp_loc)
in
Ast_helper.Exp.apply
~loc:expr.pexp_loc
jsx_string_ident
[(Nolabel, expr)]
| _ -> expr

and parse_jsx_children p : Parsetree.jsx_children =
let rec loop p children =
match p.Parser.token with
Expand All @@ -2991,12 +3006,14 @@ and parse_jsx_children p : Parsetree.jsx_children =
let child =
parse_primary_expr ~operand:(parse_atomic_expr p) ~no_call:true p
in
loop p (child :: children)
let wrapped_child = wrap_jsx_string_literal child in
loop p (wrapped_child :: children)
| token when Grammar.is_jsx_child_start token ->
let child =
parse_primary_expr ~operand:(parse_atomic_expr p) ~no_call:true p
in
loop p (child :: children)
let wrapped_child = wrap_jsx_string_literal child in
loop p (wrapped_child :: children)
| _ -> children
in
let children =
Expand Down
6 changes: 6 additions & 0 deletions tests/syntax_tests/data/parsing/grammar/expressions/jsx.res
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ let _ = <Outer inner=<Inner /> />

let _ = <div onClick=onClickHandler> <> "foobar" </> </div>

// Test automatic string wrapping in JSX children
let _ = <span>hello world</span>
let _ = <div>simple string</div>
let _ = <> fragment string </>
let _ = <div>first string<span>nested</span>last string</div>


let _ =
<Window
Expand Down
Loading