Skip to content

Test ast conversion #7318

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

Merged
merged 3 commits into from
Mar 3, 2025
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
35 changes: 30 additions & 5 deletions compiler/syntax/cli/res_cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ module ResClflags : sig
val jsx_module : string ref
val jsx_mode : string ref
val typechecker : bool ref
val test_ast_conversion : bool ref

val parse : unit -> unit
end = struct
Expand All @@ -178,6 +179,7 @@ end = struct
let jsx_mode = ref "automatic"
let file = ref ""
let typechecker = ref false
let test_ast_conversion = ref false

let usage =
"\n\
Expand Down Expand Up @@ -215,6 +217,9 @@ end = struct
Arg.Unit (fun () -> typechecker := true),
"Parses the ast as it would be passed to the typechecker and not the \
printer" );
( "-test-ast-conversion",
Arg.Unit (fun () -> test_ast_conversion := true),
"Test the ast conversion" );
]

let parse () = Arg.parse spec (fun f -> file := f) usage
Expand All @@ -225,7 +230,7 @@ module CliArgProcessor = struct
[@@unboxed]

let process_file ~is_interface ~width ~recover ~target ~jsx_version
~jsx_module ~jsx_mode ~typechecker filename =
~jsx_module ~jsx_mode ~typechecker ~test_ast_conversion filename =
let len = String.length filename in
let process_interface =
is_interface
Expand Down Expand Up @@ -267,8 +272,17 @@ module CliArgProcessor = struct
else exit 1)
else
let parsetree =
Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode
parse_result.parsetree
if not test_ast_conversion then parse_result.parsetree
else
let tree0 =
Ast_mapper_to0.default_mapper.signature
Ast_mapper_to0.default_mapper parse_result.parsetree
in
Ast_mapper_from0.default_mapper.signature
Ast_mapper_from0.default_mapper tree0
in
let parsetree =
Jsx_ppx.rewrite_signature ~jsx_version ~jsx_module ~jsx_mode parsetree
in
print_engine.print_interface ~width ~filename
~comments:parse_result.comments parsetree
Expand All @@ -282,9 +296,19 @@ module CliArgProcessor = struct
~comments:parse_result.comments parse_result.parsetree
else exit 1)
else
let parsetree =
if not test_ast_conversion then parse_result.parsetree
else
let tree0 =
Ast_mapper_to0.default_mapper.structure
Ast_mapper_to0.default_mapper parse_result.parsetree
in
Ast_mapper_from0.default_mapper.structure
Ast_mapper_from0.default_mapper tree0
in
let parsetree =
Jsx_ppx.rewrite_implementation ~jsx_version ~jsx_module ~jsx_mode
parse_result.parsetree
parsetree
in
print_engine.print_implementation ~width ~filename
~comments:parse_result.comments parsetree
Expand All @@ -298,5 +322,6 @@ let () =
~width:!ResClflags.width ~recover:!ResClflags.recover
~target:!ResClflags.print ~jsx_version:!ResClflags.jsx_version
~jsx_module:!ResClflags.jsx_module ~jsx_mode:!ResClflags.jsx_mode
~typechecker:!ResClflags.typechecker !ResClflags.file)
~typechecker:!ResClflags.typechecker !ResClflags.file
~test_ast_conversion:!ResClflags.test_ast_conversion)
[@@raises exit]
6 changes: 6 additions & 0 deletions scripts/test_syntax.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ while read file; do
$DUNE_BIN_DIR/res_parser $file &> $(exp $file) & maybeWait
done <temp/files.txt

# printing with ast conversion
find syntax_tests/data/ast-mapping -name "*.res" -o -name "*.resi" -o -name "*.ml" -o -name "*.mli" >temp/files.txt
while read file; do
$DUNE_BIN_DIR/res_parser -test-ast-conversion -jsx-version 4 -jsx-mode "automatic" $file &> $(exp $file) & maybeWait
done <temp/files.txt

# printing with ppx
find syntax_tests/data/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
while read file; do
Expand Down
25 changes: 25 additions & 0 deletions tests/syntax_tests/data/ast-mapping/JSXElements.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let emptyUnary = <input />

let emptyNonunary = <div></div>

let emptyUnaryWithAttributes = <input type_="text" />

let emptyNonunaryWithAttributes = <div className="container"></div>

let elementWithChildren = <div>
<h1>{React.string("Hi")}</h1>
<p>{React.string("Hello")}</p>
</div>

let elementWithChildrenAndAttributes = <div className="container">
<h1>{React.string("Hi")}</h1>
<p>{React.string("Hello")}</p>
</div>

let elementWithConditionalChildren = <div>
{if true {
<h1>{React.string("Hi")}</h1>
} else {
React.null
}}
</div>
14 changes: 14 additions & 0 deletions tests/syntax_tests/data/ast-mapping/JSXFragments.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let empty = <></>

let fragmentWithBracedExpresssion = <>{React.int(1 + 2)}</>

let fragmentWithJSXElements = <>
<h1>{React.string("Hi")}</h1>
<p>{React.string("Hello")}</p>
</>

let nestedFragments = <>
<h1>{React.string("Hi")}</h1>
<p>{React.string("Hello")}</p>
<>{React.string("Bye")}</>
</>
41 changes: 41 additions & 0 deletions tests/syntax_tests/data/ast-mapping/expected/JSXElements.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
let emptyUnary = ReactDOM.jsx("input", {})

let emptyNonunary = ReactDOM.jsx("div", {})

let emptyUnaryWithAttributes = ReactDOM.jsx("input", {type_: "text"})

let emptyNonunaryWithAttributes = ReactDOM.jsx("div", {className: "container"})

let elementWithChildren = ReactDOM.jsxs(
"div",
{
children: React.array([
ReactDOM.jsx("h1", {children: ?ReactDOM.someElement({React.string("Hi")})}),
ReactDOM.jsx("p", {children: ?ReactDOM.someElement({React.string("Hello")})}),
]),
},
)

let elementWithChildrenAndAttributes = ReactDOM.jsxs(
"div",
{
className: "container",
children: React.array([
ReactDOM.jsx("h1", {children: ?ReactDOM.someElement({React.string("Hi")})}),
ReactDOM.jsx("p", {children: ?ReactDOM.someElement({React.string("Hello")})}),
]),
},
)

let elementWithConditionalChildren = ReactDOM.jsx(
"div",
{
children: ?ReactDOM.someElement({
if true {
ReactDOM.jsx("h1", {children: ?ReactDOM.someElement({React.string("Hi")})})
} else {
React.null
}
}),
},
)
24 changes: 24 additions & 0 deletions tests/syntax_tests/data/ast-mapping/expected/JSXFragments.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let empty = React.jsx(React.jsxFragment, {})

let fragmentWithBracedExpresssion = React.jsx(React.jsxFragment, {children: {React.int(1 + 2)}})

let fragmentWithJSXElements = React.jsxs(
React.jsxFragment,
{
children: React.array([
ReactDOM.jsx("h1", {children: ?ReactDOM.someElement({React.string("Hi")})}),
ReactDOM.jsx("p", {children: ?ReactDOM.someElement({React.string("Hello")})}),
]),
},
)

let nestedFragments = React.jsxs(
React.jsxFragment,
{
children: React.array([
ReactDOM.jsx("h1", {children: ?ReactDOM.someElement({React.string("Hi")})}),
ReactDOM.jsx("p", {children: ?ReactDOM.someElement({React.string("Hello")})}),
React.jsx(React.jsxFragment, {children: {React.string("Bye")}}),
]),
},
)