-
Notifications
You must be signed in to change notification settings - Fork 470
Preserve JSX #7387
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
Preserve JSX #7387
Changes from 15 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
ffc7666
Add additional node to Pexp_apply
nojaf 92e38cb
Try and pass jsx_element from typed_tree to js_call
nojaf dd56e61
Follow Lprim
nojaf a0c170f
Transform initial simple element
nojaf a98e3b8
Initial fragment support
nojaf 1801da1
WIP extract, good stuff
nojaf ab32462
Print props, catch with key functions
nojaf 6b075c0
Don't pass untyped ast, simple flag is sufficient.
nojaf d0bfedc
Support fragments
nojaf 3d2930e
Unwrap children
nojaf 547dfca
Poor man feature flag
nojaf 09950f1
Remove duplicated in
nojaf 0ab6987
Older camls
nojaf 8e4811b
Revert "Poor man feature flag"
nojaf aa94f6f
Add new -bs-jsx-preserve flag
nojaf e30cab3
WIP, deal with prop spreading
nojaf 773124f
Deal with prop spreading
nojaf 46d3d50
Clean up Lprim and move the information to the call primitive.
cristianoc 1a20f38
Add test for spreading children
nojaf 8929f37
Refactor duplicate code
nojaf cecf67d
Support keyed and prop spreading
nojaf 3fcf1df
Rename test file
nojaf 8b7eb45
Keep key prop before spreading props. Also ensure test can run.
nojaf 90b28f0
Add only spread props case
nojaf faa5618
Give record a name
nojaf 400b550
Use config flag in Js_dump instead
nojaf 3e1867e
Remove helper code
nojaf 32bffd8
Extra call info
nojaf f104529
Detect direct Array as well
nojaf 848b3ab
Don't run with mocha
nojaf aceb0e2
Feedback code review
nojaf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -524,6 +524,48 @@ and expression_desc cxt ~(level : int) f x : cxt = | |
when Ext_list.length_equal el i | ||
]} | ||
*) | ||
| Call | ||
nojaf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
( ({expression_desc = J.Var (J.Qualified (_, Some fnName))} as e), | ||
el, | ||
{call_transformed_jsx = true} ) -> ( | ||
match el with | ||
| [ | ||
tag; | ||
{ | ||
expression_desc = | ||
Caml_block (el, _mutable_flag, _, Lambda.Blk_record {fields}); | ||
}; | ||
] -> | ||
let fields = | ||
Ext_list.array_list_filter_map fields el (fun (f, opt) x -> | ||
match x.expression_desc with | ||
| Undefined _ when opt -> None | ||
| _ -> Some (f, x)) | ||
in | ||
print_jsx cxt ~level f fnName tag fields | ||
| [ | ||
tag; | ||
{ | ||
expression_desc = | ||
Caml_block (el, _mutable_flag, _, Lambda.Blk_record {fields}); | ||
}; | ||
key; | ||
] -> | ||
let fields = | ||
Ext_list.array_list_filter_map fields el (fun (f, opt) x -> | ||
match x.expression_desc with | ||
| Undefined _ when opt -> None | ||
| _ -> Some (f, x)) | ||
in | ||
let fields = ("key", key) :: fields in | ||
print_jsx cxt ~level f fnName tag fields | ||
| _ -> | ||
expression_desc cxt ~level f | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this code branch ever executed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it is, it means we missed something. Could we add some sort of assert in this case? |
||
(Call | ||
( e, | ||
el, | ||
{call_transformed_jsx = false; arity = Full; call_info = Call_ml} | ||
))) | ||
| Call (e, el, info) -> | ||
P.cond_paren_group f (level > 15) (fun _ -> | ||
P.group f 0 (fun _ -> | ||
|
@@ -956,6 +998,77 @@ and expression_desc cxt ~(level : int) f x : cxt = | |
P.string f "..."; | ||
expression ~level:13 cxt f e) | ||
|
||
and print_jsx cxt ~(level : int) f (fnName : string) (tag : J.expression) | ||
(fields : (string * J.expression) list) : cxt = | ||
let print_tag () = | ||
match tag.expression_desc with | ||
| J.Str {txt} -> P.string f txt | ||
(* fragment *) | ||
| J.Var (J.Qualified ({id = {name = "JsxRuntime"}}, Some "Fragment")) -> () | ||
| _ -> | ||
let _ = expression ~level cxt f tag in | ||
() | ||
in | ||
let children_opt = | ||
List.find_map | ||
(fun (n, e) -> | ||
if n = "children" then | ||
if fnName = "jsxs" then | ||
match e.J.expression_desc with | ||
| J.Optional_block ({expression_desc = J.Array (xs, _)}, _) -> | ||
Some xs | ||
| _ -> Some [e] | ||
else Some [e] | ||
else None) | ||
fields | ||
in | ||
let print_props () = | ||
let props = List.filter (fun (n, _) -> n <> "children") fields in | ||
if List.length props > 0 then | ||
(List.iter (fun (n, x) -> | ||
P.space f; | ||
P.string f n; | ||
P.string f "="; | ||
P.string f "{"; | ||
let _ = expression ~level:0 cxt f x in | ||
P.string f "}")) | ||
props | ||
in | ||
(match children_opt with | ||
| None -> | ||
P.string f "<"; | ||
print_tag (); | ||
print_props (); | ||
P.string f "/>" | ||
| Some children -> | ||
let child_is_jsx child = | ||
match child.J.expression_desc with | ||
| J.Call (_, _, {call_transformed_jsx = is_jsx}) -> is_jsx | ||
| _ -> false | ||
in | ||
|
||
P.string f "<"; | ||
print_tag (); | ||
print_props (); | ||
P.string f ">"; | ||
|
||
let _ = | ||
children | ||
|> List.fold_left | ||
(fun acc e -> | ||
if not (child_is_jsx e) then P.string f "{"; | ||
let next = expression ~level acc f e in | ||
if not (child_is_jsx e) then P.string f "}"; | ||
next) | ||
cxt | ||
in | ||
|
||
P.string f "</"; | ||
print_tag (); | ||
P.string f ">"); | ||
|
||
cxt | ||
|
||
and property_name_and_value_list cxt f (l : J.property_map) = | ||
iter_lst cxt f l | ||
(fun cxt f (pn, e) -> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
let lambda_tag_info_to_string (e : Lambda.tag_info) = | ||
match e with | ||
| Lambda.Blk_constructor _ -> "Blk_constructor" | ||
| Lambda.Blk_record_inlined _ -> "Blk_record_inlined" | ||
| Lambda.Blk_tuple -> "Blk_tuple" | ||
| Lambda.Blk_poly_var _ -> "Blk_poly_var" | ||
| Lambda.Blk_record _ -> "Blk_record" | ||
| Lambda.Blk_module _ -> "Blk_module" | ||
| Lambda.Blk_module_export _ -> "Blk_module_export" | ||
| Lambda.Blk_extension -> "Blk_extension" | ||
| Lambda.Blk_some -> "Blk_some" | ||
| Lambda.Blk_some_not_nested -> "Blk_some_not_nested" | ||
| Lambda.Blk_record_ext _ -> "Blk_record_ext" | ||
| Lambda.Blk_lazy_general -> "Blk_lazy_general" | ||
|
||
let j_exp_to_string (e : J.expression) = | ||
match e.J.expression_desc with | ||
| J.Object _ -> "Object" | ||
| J.Str _ -> "String" | ||
| J.Var (J.Qualified (_, Some o)) -> "Var_" ^ o | ||
| J.Var _ -> "Var" | ||
| J.Call _ -> "Call" | ||
| J.Fun _ -> "Fun" | ||
| J.Array _ -> "Array" | ||
| J.Bin _ -> "Bin" | ||
| J.Cond _ -> "Cond" | ||
| J.New _ -> "New" | ||
| J.Seq _ -> "Seq" | ||
| J.Number _ -> "Number" | ||
| J.Bool _ -> "Bool" | ||
| J.Null -> "Null" | ||
| J.Undefined _ -> "Undefined" | ||
| J.Is_null_or_undefined _ -> "Is_null_or_undefined" | ||
| J.Js_not _ -> "Js_not" | ||
| J.Typeof _ -> "Typeof" | ||
| J.String_index _ -> "String_index" | ||
| J.Array_index _ -> "Array_index" | ||
| J.Static_index _ -> "Static_index" | ||
| J.Length _ -> "Length" | ||
| J.Caml_block (_, _, _, tag) -> | ||
Format.sprintf "Caml_block (%s)" (lambda_tag_info_to_string tag) | ||
| J.Caml_block_tag _ -> "Caml_block_tag" | ||
| J.Tagged_template _ -> "Tagged_template" | ||
| J.Optional_block _ -> "Optional_block" | ||
| J.Spread _ -> "Spread" | ||
| J.Await _ -> "Await" | ||
| J.Raw_js_code _ -> "Raw_js_code" | ||
| _ -> "Other" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.