diff --git a/compiler/core/js_of_lam_block.ml b/compiler/core/js_of_lam_block.ml index 850e8ad2f94..bfc943d99f5 100644 --- a/compiler/core/js_of_lam_block.ml +++ b/compiler/core/js_of_lam_block.ml @@ -43,7 +43,7 @@ let field (field_info : Lam_compat.field_dbg_info) e (i : int32) = | Fld_cons -> E.cons_access e i | Fld_record_inline {name} -> E.inline_record_access e name i | Fld_record {name} -> E.record_access e name i - | Fld_module {name} -> E.module_access e name i + | Fld_module {name; jsx_component = _} -> E.module_access e name i let field_by_exp e i = E.array_index e i diff --git a/compiler/core/lam.ml b/compiler/core/lam.ml index 51b8bb3e382..3eb1b227845 100644 --- a/compiler/core/lam.ml +++ b/compiler/core/lam.ml @@ -555,7 +555,8 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t = | ( f :: fields, Lprim { - primitive = Pfield (pos, Fld_module {name = f1}); + primitive = + Pfield (pos, Fld_module {name = f1; jsx_component = false}); args = [(Lglobal_module (v1, _) | Lvar v1)]; } :: args ) -> @@ -566,7 +567,8 @@ let prim ~primitive:(prim : Lam_primitive.t) ~args loc : t = | ( field1 :: rest, Lprim { - primitive = Pfield (pos, Fld_module {name = f1}); + primitive = + Pfield (pos, Fld_module {name = f1; jsx_component = false}); args = [((Lglobal_module (v1, _) | Lvar v1) as lam)]; } :: args1 ) -> diff --git a/compiler/core/lam_analysis.ml b/compiler/core/lam_analysis.ml index a4b78bea0eb..90f4c6c4cf9 100644 --- a/compiler/core/lam_analysis.ml +++ b/compiler/core/lam_analysis.ml @@ -123,7 +123,12 @@ let rec no_side_effects (lam : Lam.t) : bool = (* | Lsend _ -> false *) | Lapply { - ap_func = Lprim {primitive = Pfield (_, Fld_module {name = "from_fun"})}; + ap_func = + Lprim + { + primitive = + Pfield (_, Fld_module {name = "from_fun"; jsx_component = _}); + }; ap_args = [arg]; } -> no_side_effects arg diff --git a/compiler/core/lam_arity_analysis.ml b/compiler/core/lam_arity_analysis.ml index ee3d91f1541..c3608365049 100644 --- a/compiler/core/lam_arity_analysis.ml +++ b/compiler/core/lam_arity_analysis.ml @@ -42,7 +42,7 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t = | Llet (_, _, _, l) -> get_arity meta l | Lprim { - primitive = Pfield (_, Fld_module {name}); + primitive = Pfield (_, Fld_module {name; jsx_component = _}); args = [Lglobal_module (id, dynamic_import)]; _; } -> ( @@ -58,7 +58,7 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t = [ Lprim { - primitive = Pfield (_, Fld_module {name}); + primitive = Pfield (_, Fld_module {name; jsx_component = _}); args = [Lglobal_module (id, dynamic_import)]; }; ]; diff --git a/compiler/core/lam_compat.ml b/compiler/core/lam_compat.ml index a652d74ca43..70c3486c5e9 100644 --- a/compiler/core/lam_compat.ml +++ b/compiler/core/lam_compat.ml @@ -64,7 +64,7 @@ type let_kind = Lambda.let_kind = Strict | Alias | StrictOpt | Variable type field_dbg_info = Lambda.field_dbg_info = | Fld_record of {name: string; mutable_flag: Asttypes.mutable_flag} - | Fld_module of {name: string} + | Fld_module of {name: string; jsx_component: bool} | Fld_record_inline of {name: string} | Fld_record_extension of {name: string} | Fld_tuple diff --git a/compiler/core/lam_compat.mli b/compiler/core/lam_compat.mli index 4d67d95242e..cbea5cd5ea8 100644 --- a/compiler/core/lam_compat.mli +++ b/compiler/core/lam_compat.mli @@ -28,7 +28,7 @@ type let_kind = Lambda.let_kind = Strict | Alias | StrictOpt | Variable type field_dbg_info = Lambda.field_dbg_info = | Fld_record of {name: string; mutable_flag: Asttypes.mutable_flag} - | Fld_module of {name: string} + | Fld_module of {name: string; jsx_component: bool} | Fld_record_inline of {name: string} | Fld_record_extension of {name: string} | Fld_tuple diff --git a/compiler/core/lam_compile.ml b/compiler/core/lam_compile.ml index 42844bc99c0..d72440c85d1 100644 --- a/compiler/core/lam_compile.ml +++ b/compiler/core/lam_compile.ml @@ -232,6 +232,170 @@ type initialization = J.block *) let compile output_prefix = + let root_module_name (id : Ident.t) = + match Ext_namespace.try_split_module_name id.name with + | Some (_namespace, module_name) -> module_name + | None -> ( + match String.index_opt id.name '$' with + | Some index -> String.sub id.name 0 index + | None -> id.name) + in + let rec extract_nested_external_component_segments segments + ((lam : Lam.t), (make_dynamic_import : bool option ref)) : + (Ident.t * bool * string list) option = + match lam with + | Lprim + { + primitive = Pfield (_, Fld_module {name; jsx_component = _}); + args = [arg]; + _; + } -> + extract_nested_external_component_segments (name :: segments) + (arg, make_dynamic_import) + | Lvar id -> + make_dynamic_import := Some false; + Some (id, false, List.rev segments) + | Lglobal_module (id, dynamic_import) -> + make_dynamic_import := Some dynamic_import; + Some (id, dynamic_import, List.rev segments) + | _ -> None + in + let extract_nested_external_component_field (lam : Lam.t) : + (Ident.t * bool * string) option = + match lam with + | Lprim + { + primitive = Pfield (_, Fld_module {name = "make"; jsx_component = _}); + args = [arg]; + _; + } -> ( + let dynamic_import = ref None in + match + extract_nested_external_component_segments [] (arg, dynamic_import) + with + | Some (id, dynamic_import, segments) -> ( + let denamespace_segment segment = + let root_name = root_module_name id in + let namespaced_prefix = root_name ^ "$" in + if Ext_string.starts_with segment namespaced_prefix then + match String.split_on_char '$' segment with + | root :: _namespace :: rest when rest <> [] -> + String.concat "$" (root :: rest) + | _ -> segment + else segment + in + let segments = + match segments with + | head :: rest + when head = id.name + || head = root_module_name id + || Ext_string.starts_with head (root_module_name id ^ "$") -> + rest + | _ -> segments + in + let segments = + match segments with + | head :: rest -> denamespace_segment head :: rest + | [] -> [] + in + match segments with + | [] -> None + | _ -> + Some + ( id, + dynamic_import, + String.concat "$" (root_module_name id :: segments) )) + | None -> None) + | _ -> None + in + let normalize_hidden_component_name (id : Ident.t) (hidden_name : string) = + let root_name = root_module_name id in + let id_parts = String.split_on_char '$' id.name in + let namespace_parts = + match id_parts with + | _root :: rest -> rest + | [] -> [] + in + let hidden_parts = String.split_on_char '$' hidden_name in + let hidden_parts_without_root = + match hidden_parts with + | first :: rest when String.equal first root_name -> rest + | _ -> hidden_parts + in + let rec drop_prefix prefix parts = + match (prefix, parts) with + | [], _ -> parts + | x :: xs, y :: ys when String.equal x y -> drop_prefix xs ys + | _ -> parts + in + let tail = drop_prefix namespace_parts hidden_parts_without_root in + match tail with + | [] -> hidden_name + | _ -> String.concat "$" (root_name :: tail) + in + let hidden_component_name_candidates (id : Ident.t) (hidden_name : string) = + let candidates = ref [] in + let push candidate = + if not (List.mem candidate !candidates) then + candidates := candidate :: !candidates + in + (match String.split_on_char '$' hidden_name with + | root :: _namespace :: rest when rest <> [] -> + push (String.concat "$" (root :: rest)) + | _ -> ()); + push (normalize_hidden_component_name id hidden_name); + push hidden_name; + List.rev !candidates + in + let exported_hidden_component_name ~(id : Ident.t) ~(dynamic_import : bool) + (hidden_name_candidates : string list) = + let rec loop = function + | [] -> None + | candidate :: rest -> ( + match + Lam_compile_env.query_external_id_info ~dynamic_import id + (candidate ^ "$jsx") + with + | _ -> Some candidate + | exception Not_found -> loop rest) + in + loop hidden_name_candidates + in + let rewrite_nested_jsx_component_expr (jsx_tag : Lam.t) + (compiled_expr : J.expression) : J.expression = + let rec extract_root_expr (expr : J.expression) = + match expr.expression_desc with + | Var (Qualified (module_id, Some _)) -> + Some {expr with expression_desc = Var (Qualified (module_id, None))} + | Static_index (inner, _, _) -> extract_root_expr inner + | Var _ -> Some expr + | _ -> None + in + let hidden_component_access (root_expr : J.expression) hidden_name = + match root_expr.expression_desc with + | Var (Qualified (module_id, None)) -> + { + root_expr with + expression_desc = Var (Qualified (module_id, Some hidden_name)); + } + | _ -> E.dot root_expr hidden_name + in + match extract_nested_external_component_field jsx_tag with + | Some (id, dynamic_import, hidden_name) -> ( + let hidden_name_candidates = + hidden_component_name_candidates id hidden_name + in + match extract_root_expr compiled_expr with + | Some root_expr -> ( + match + exported_hidden_component_name ~id ~dynamic_import + hidden_name_candidates + with + | Some hidden_name -> hidden_component_access root_expr hidden_name + | None -> compiled_expr) + | None -> compiled_expr) + | None -> compiled_expr + in let rec compile_external_field (* Like [List.empty]*) ?(dynamic_import = false) (lamba_cxt : Lam_compile_context.t) (id : Ident.t) name : Js_output.t = @@ -300,7 +464,14 @@ let compile output_prefix = (Ext_list.append block args_code, b :: args) | _ -> assert false) in - + let args = + if appinfo.ap_transformed_jsx then + match (appinfo.ap_args, args) with + | jsx_tag :: _, jsx_expr :: rest_args -> + rewrite_nested_jsx_component_expr jsx_tag jsx_expr :: rest_args + | _ -> args + else args + in let fn = E.ml_var_dot ~dynamic_import module_id ident_info.name in let expression = match appinfo.ap_info.ap_status with @@ -1505,7 +1676,7 @@ let compile output_prefix = }; } -> ( match fld_info with - | Fld_module {name} -> + | Fld_module {name; jsx_component = _} -> compile_external_field_apply ~dynamic_import appinfo id name lambda_cxt | _ -> assert false) | _ -> ( @@ -1524,6 +1695,14 @@ let compile output_prefix = (Ext_list.append block args_code, b :: fn_code) | {value = None} -> assert false) in + let args = + if appinfo.ap_transformed_jsx then + match (appinfo.ap_args, args) with + | jsx_tag :: _, jsx_expr :: rest_args -> + rewrite_nested_jsx_component_expr jsx_tag jsx_expr :: rest_args + | _ -> args + else args + in match (ap_func, lambda_cxt.continuation) with | ( Lvar fn_id, ( EffectCall (Maybe_tail_is_return (Tail_with_name {label = Some ret})) @@ -1583,6 +1762,37 @@ let compile output_prefix = and compile_prim (prim_info : Lam.prim_info) (lambda_cxt : Lam_compile_context.t) = match prim_info with + | { + primitive = + Pjs_call + { + prim_name = "jsx" | "jsxs" | "jsxKeyed" | "jsxsKeyed"; + transformed_jsx = true; + _; + }; + args = jsx_tag :: rest_args; + loc; + } -> + let new_cxt = {lambda_cxt with continuation = NeedValue Not_tail} in + let tag_block, tag_expr = + match compile_lambda new_cxt jsx_tag with + | {block; value = Some b} -> + (block, rewrite_nested_jsx_component_expr jsx_tag b) + | {value = None} -> assert false + in + let rest_blocks, rest_exprs = + Ext_list.split_map rest_args (fun x -> + match compile_lambda new_cxt x with + | {block; value = Some b} -> (block, b) + | {value = None} -> assert false) + in + let args_code : J.block = List.concat (tag_block :: rest_blocks) in + let exp = + Lam_compile_primitive.translate output_prefix loc lambda_cxt + prim_info.primitive (tag_expr :: rest_exprs) + in + Js_output.output_of_block_and_expression lambda_cxt.continuation args_code + exp | { primitive = Pfield (_, fld_info); args = [Lglobal_module (id, dynamic_import)]; @@ -1590,7 +1800,7 @@ let compile output_prefix = } -> ( (* should be before Lglobal_global *) match fld_info with - | Fld_module {name = field} -> + | Fld_module {name = field; jsx_component = _} -> compile_external_field ~dynamic_import lambda_cxt id field | _ -> assert false) | {primitive = Praise; args = [e]; _} -> ( diff --git a/compiler/core/lam_pass_remove_alias.ml b/compiler/core/lam_pass_remove_alias.ml index 1dad7d3865c..cd72556a775 100644 --- a/compiler/core/lam_pass_remove_alias.ml +++ b/compiler/core/lam_pass_remove_alias.ml @@ -133,7 +133,8 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t = ap_func = Lprim { - primitive = Pfield (_, Fld_module {name = fld_name}); + primitive = + Pfield (_, Fld_module {name = fld_name; jsx_component = _}); args = [Lglobal_module (ident, dynamic_import)]; _; } as l1; diff --git a/compiler/core/lam_print.ml b/compiler/core/lam_print.ml index 172e219abb7..0607724e232 100644 --- a/compiler/core/lam_print.ml +++ b/compiler/core/lam_print.ml @@ -323,7 +323,7 @@ let lambda ppf v = fprintf ppf ")@ %a)@]" lam body | Lprim { - primitive = Pfield (n, Fld_module {name = s}); + primitive = Pfield (n, Fld_module {name = s; jsx_component = _}); args = [Lglobal_module (id, dynamic_import)]; _; } -> diff --git a/compiler/ml/lambda.ml b/compiler/ml/lambda.ml index db810d4f912..540b9204c0a 100644 --- a/compiler/ml/lambda.ml +++ b/compiler/ml/lambda.ml @@ -113,7 +113,7 @@ let ref_tag_info : tag_info = type field_dbg_info = | Fld_record of {name: string; mutable_flag: Asttypes.mutable_flag} - | Fld_module of {name: string} + | Fld_module of {name: string; jsx_component: bool} | Fld_record_inline of {name: string} | Fld_record_extension of {name: string} | Fld_tuple @@ -134,6 +134,8 @@ let fld_record_extension (lbl : label) = Fld_record_extension {name = Ext_list.find_def lbl.lbl_attributes find_name lbl.lbl_name} +let fld_module ~name ~jsx_component = Fld_module {name; jsx_component} + let ref_field_info : field_dbg_info = Fld_record {name = "contents"; mutable_flag = Mutable} @@ -620,11 +622,28 @@ let rec transl_normal_path = function else Lvar id | Pdot (p, s, pos) -> Lprim - ( Pfield (pos, Fld_module {name = s}), + ( Pfield (pos, Fld_module {name = s; jsx_component = false}), [transl_normal_path p], Location.none ) | Papply _ -> assert false +let transl_jsx_path path = + let rec aux ~is_final = function + | Path.Pident id -> + if Ident.global id then Lprim (Pgetglobal id, [], Location.none) + else Lvar id + | Pdot (p, s, pos) -> + Lprim + ( Pfield + ( pos, + Fld_module + {name = s; jsx_component = is_final && String.equal s "make"} ), + [aux ~is_final:false p], + Location.none ) + | Papply _ -> assert false + in + aux ~is_final:true path + (* Translation of identifiers *) let transl_module_path ?(loc = Location.none) env path = @@ -633,6 +652,9 @@ let transl_module_path ?(loc = Location.none) env path = let transl_value_path ?(loc = Location.none) env path = transl_normal_path (Env.normalize_path_prefix (Some loc) env path) +let transl_jsx_value_path ?(loc = Location.none) env path = + transl_jsx_path (Env.normalize_path_prefix (Some loc) env path) + let transl_extension_path = transl_value_path (* Apply a substitution to a lambda-term. diff --git a/compiler/ml/lambda.mli b/compiler/ml/lambda.mli index d8eaf57be6f..3658437d902 100644 --- a/compiler/ml/lambda.mli +++ b/compiler/ml/lambda.mli @@ -84,7 +84,7 @@ val ref_tag_info : tag_info type field_dbg_info = | Fld_record of {name: string; mutable_flag: Asttypes.mutable_flag} - | Fld_module of {name: string} + | Fld_module of {name: string; jsx_component: bool} | Fld_record_inline of {name: string} | Fld_record_extension of {name: string} | Fld_tuple @@ -100,6 +100,8 @@ val fld_record_inline : Types.label_description -> field_dbg_info val fld_record_extension : Types.label_description -> field_dbg_info +val fld_module : name:string -> jsx_component:bool -> field_dbg_info + val ref_field_info : field_dbg_info type set_field_dbg_info = @@ -393,6 +395,7 @@ val transl_normal_path : Path.t -> lambda (* Path.t is already normal *) val transl_module_path : ?loc:Location.t -> Env.t -> Path.t -> lambda val transl_value_path : ?loc:Location.t -> Env.t -> Path.t -> lambda +val transl_jsx_value_path : ?loc:Location.t -> Env.t -> Path.t -> lambda val transl_extension_path : ?loc:Location.t -> Env.t -> Path.t -> lambda val subst_lambda : lambda Ident.tbl -> lambda -> lambda diff --git a/compiler/ml/printlambda.ml b/compiler/ml/printlambda.ml index 0282f6e113c..ecafa38cd6e 100644 --- a/compiler/ml/printlambda.ml +++ b/compiler/ml/printlambda.ml @@ -72,7 +72,7 @@ let string_of_loc_kind = function let str_of_field_info (fld_info : Lambda.field_dbg_info) = match fld_info with - | Fld_module {name} + | Fld_module {name; jsx_component = _} | Fld_record {name} | Fld_record_inline {name} | Fld_record_extension {name} -> diff --git a/compiler/ml/translcore.ml b/compiler/ml/translcore.ml index 078cbf133a0..670a91a8a82 100644 --- a/compiler/ml/translcore.ml +++ b/compiler/ml/translcore.ml @@ -652,6 +652,11 @@ let extract_directive_for_fn exp = if txt = "directive" then Ast_payload.is_single_string payload else None) +let has_jsx_component_path_attr (exp : Typedtree.expression) = + List.exists + (fun ({txt; _}, _) -> String.equal txt "res.jsxComponentPath") + exp.exp_attributes + let rec transl_exp e = List.iter (Translattribute.check_attribute e) e.exp_attributes; transl_exp0 e @@ -661,7 +666,9 @@ and transl_exp0 (e : Typedtree.expression) : Lambda.lambda = | Texp_ident (_, _, {val_kind = Val_prim p}) -> transl_primitive e.exp_loc p e.exp_env e.exp_type | Texp_ident (path, _, {val_kind = Val_reg}) -> - transl_value_path ~loc:e.exp_loc e.exp_env path + if has_jsx_component_path_attr e then + transl_jsx_value_path ~loc:e.exp_loc e.exp_env path + else transl_value_path ~loc:e.exp_loc e.exp_env path | Texp_constant cst -> Lconst (Const_base cst) | Texp_let (rec_flag, pat_expr_list, body) -> transl_let rec_flag pat_expr_list (transl_exp body) diff --git a/compiler/ml/translmod.ml b/compiler/ml/translmod.ml index 87471ac26bf..caf2b47dde6 100644 --- a/compiler/ml/translmod.ml +++ b/compiler/ml/translmod.ml @@ -64,14 +64,20 @@ let rec apply_coercion loc strict (restr : Typedtree.module_coercion) arg = | Tcoerce_structure (pos_cc_list, id_pos_list, runtime_fields) -> Lambda.name_lambda strict arg (fun id -> let get_field_name name pos = - Lambda.Lprim (Pfield (pos, Fld_module {name}), [Lvar id], loc) + Lambda.Lprim + ( Pfield (pos, Fld_module {name; jsx_component = false}), + [Lvar id], + loc ) in let lam = Lambda.Lprim ( Pmakeblock (Blk_module runtime_fields), Ext_list.map2 pos_cc_list runtime_fields (fun (pos, cc) name -> apply_coercion loc Alias cc - (Lprim (Pfield (pos, Fld_module {name}), [Lvar id], loc))), + (Lprim + ( Pfield (pos, Fld_module {name; jsx_component = false}), + [Lvar id], + loc ))), loc ) in wrap_id_pos_list loc id_pos_list get_field_name lam) @@ -432,7 +438,10 @@ and transl_structure loc fields cc rootpath final_env = function Pgenval, id, Lprim - ( Pfield (pos, Fld_module {name = Ident.name id}), + ( Pfield + ( pos, + Fld_module {name = Ident.name id; jsx_component = false} + ), [Lvar mid], incl.incl_loc ), body ), diff --git a/compiler/syntax/src/jsx_common.ml b/compiler/syntax/src/jsx_common.ml index a48cf11bc97..e7b8698c701 100644 --- a/compiler/syntax/src/jsx_common.ml +++ b/compiler/syntax/src/jsx_common.ml @@ -6,6 +6,7 @@ type jsx_config = { mutable module_: string; mutable nested_modules: string list; mutable has_component: bool; + mutable hoisted_structure_items: structure_item list; } (* Helper method to look up the [@react.component] attribute *) diff --git a/compiler/syntax/src/jsx_ppx.ml b/compiler/syntax/src/jsx_ppx.ml index 4b05e1995d9..078d812eca9 100644 --- a/compiler/syntax/src/jsx_ppx.ml +++ b/compiler/syntax/src/jsx_ppx.ml @@ -117,7 +117,9 @@ let get_mapper ~config = in let structure mapper items = let old_config = save_config () in + let is_top_level = config.nested_modules = [] in config.has_component <- false; + if is_top_level then config.hoisted_structure_items <- []; let result = List.map (fun item -> @@ -129,6 +131,11 @@ let get_mapper ~config = items |> List.flatten in + let result = + if config.version = 4 && is_top_level then + result @ List.rev config.hoisted_structure_items + else result + in restore_config old_config; result in @@ -143,6 +150,7 @@ let rewrite_implementation ~jsx_version ~jsx_module (code : Parsetree.structure) module_ = jsx_module; nested_modules = []; has_component = false; + hoisted_structure_items = []; } in let mapper = get_mapper ~config in @@ -156,6 +164,7 @@ let rewrite_signature ~jsx_version ~jsx_module (code : Parsetree.signature) : module_ = jsx_module; nested_modules = []; has_component = false; + hoisted_structure_items = []; } in let mapper = get_mapper ~config in diff --git a/compiler/syntax/src/jsx_v4.ml b/compiler/syntax/src/jsx_v4.ml index 4cf9c9a0dd1..fb409a2bedb 100644 --- a/compiler/syntax/src/jsx_v4.ml +++ b/compiler/syntax/src/jsx_v4.ml @@ -80,6 +80,34 @@ let make_new_binding binding expression new_name = Jsx_common.raise_error ~loc:pvb_loc "JSX component calls cannot be destructured." +let longident_of_segments = function + | [] -> assert false + | head :: rest -> + List.fold_left (fun acc name -> Ldot (acc, name)) (Lident head) rest + +let make_hoisted_component_binding ~empty_loc ~full_module_name nested_modules = + let path = + nested_modules |> List.rev |> longident_of_segments |> fun txt -> + {loc = empty_loc; txt = Ldot (txt, "make")} + in + let marker_name = full_module_name ^ "$jsx" in + { + pstr_loc = empty_loc; + pstr_desc = + Pstr_value + ( Nonrecursive, + [ + Vb.mk ~loc:empty_loc + (Pat.var ~loc:empty_loc {loc = empty_loc; txt = full_module_name}) + (Exp.ident ~loc:empty_loc path); + Vb.mk ~loc:empty_loc + (Pat.var ~loc:empty_loc {loc = empty_loc; txt = marker_name}) + (Exp.construct ~loc:empty_loc + {loc = empty_loc; txt = Lident "true"} + None); + ] ); + } + (* Lookup the filename from the location information on the AST node and turn it into a valid module identifier *) let filename_from_loc (pstr_loc : Location.t) = let file_name = @@ -94,8 +122,27 @@ let filename_from_loc (pstr_loc : Location.t) = let file_name = String.capitalize_ascii file_name in file_name +let unnamespace_module_name file_name = + match String.index_opt file_name '$' with + | Some index -> String.sub file_name 0 index + | None -> ( + match Ext_namespace.try_split_module_name file_name with + | Some (_namespace, module_name) -> module_name + | None -> file_name) + +let maybe_hoist_nested_make_component ~(config : Jsx_common.jsx_config) + ~empty_loc ~full_module_name fn_name = + match (fn_name, config.nested_modules) with + | "make", _ :: _ -> + config.hoisted_structure_items <- + make_hoisted_component_binding ~empty_loc ~full_module_name + config.nested_modules + :: config.hoisted_structure_items + | _ -> () + (* Build a string representation of a module name with segments separated by $ *) let make_module_name file_name nested_modules fn_name = + let file_name = unnamespace_module_name file_name in let full_module_name = match (file_name, nested_modules, fn_name) with (* TODO: is this even reachable? It seems like the fileName always exists *) @@ -216,6 +263,8 @@ let make_type_decls_with_core_type props_name loc core_type typ_vars = let live_attr = ({txt = "live"; loc = Location.none}, PStr []) let jsx_component_props_attr = ({txt = "res.jsxComponentProps"; loc = Location.none}, PStr []) +let jsx_component_path_attr = + ({txt = "res.jsxComponentPath"; loc = Location.none}, PStr []) (* type props<'x, 'y, ...> = { x: 'x, y?: 'y, ... } *) let make_props_record_type ~core_type_of_attr ~external_ ~typ_vars_of_core_type @@ -805,6 +854,10 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding = }, Some (binding_wrapper full_expression) ) in + let () = + maybe_hoist_nested_make_component ~config ~empty_loc ~full_module_name + fn_name + in (Some props_record_type, binding, new_binding)) else if Jsx_common.has_attr_on_binding Jsx_common.has_attr_with_props binding then @@ -902,6 +955,10 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding = Some (make_new_binding ~loc:empty_loc ~full_module_name modified_binding) in + let () = + maybe_hoist_nested_make_component ~config ~empty_loc ~full_module_name + fn_name + in let binding_expr = { binding.pvb_expr with @@ -935,7 +992,8 @@ let transform_structure_item ~config item = | { pstr_loc; pstr_desc = - Pstr_primitive ({pval_attributes; pval_type} as value_description); + Pstr_primitive + ({pval_attributes; pval_type; pval_name} as value_description); } as pstr -> ( match ( List.filter Jsx_common.has_attr pval_attributes, @@ -996,6 +1054,15 @@ let transform_structure_item ~config item = }; } in + let file_name = filename_from_loc pstr_loc in + let empty_loc = Location.in_file file_name in + let full_module_name = + make_module_name file_name config.nested_modules pval_name.txt + in + let () = + maybe_hoist_nested_make_component ~config ~empty_loc ~full_module_name + pval_name.txt + in [props_record_type; new_structure] | _ -> Jsx_common.raise_error ~loc:pstr_loc @@ -1288,10 +1355,24 @@ let mk_uppercase_tag_name_expr tag_name = | JsxUpperTag path -> Longident.Ldot (path, "make") in let loc = tag_name.loc in - Exp.ident ~loc {txt = tag_identifier; loc} + Exp.ident ~loc ~attrs:[jsx_component_path_attr] {txt = tag_identifier; loc} let expr ~(config : Jsx_common.jsx_config) mapper expression = match expression with + | { + pexp_desc = Pexp_letmodule (name, module_expr, body); + pexp_loc = loc; + pexp_attributes = attrs; + } -> + config.nested_modules <- name.txt :: config.nested_modules; + let mapped_module_expr = default_mapper.module_expr mapper module_expr in + let mapped_body = mapper.expr mapper body in + let () = + match config.nested_modules with + | _ :: rest -> config.nested_modules <- rest + | [] -> () + in + Exp.letmodule ~loc ~attrs name mapped_module_expr mapped_body | { pexp_desc = Pexp_jsx_element jsx_element; pexp_loc = loc; diff --git a/rewatch/tests/watch/06-watch-missing-source-folder.sh b/rewatch/tests/watch/06-watch-missing-source-folder.sh index 08436c4fe19..11d4336c4ca 100755 --- a/rewatch/tests/watch/06-watch-missing-source-folder.sh +++ b/rewatch/tests/watch/06-watch-missing-source-folder.sh @@ -54,14 +54,34 @@ fi # where the config change triggers a full rebuild that runs concurrently # with the subsequent `rewatch build`. exit_watcher -sleep 1 +if ! wait_for_file_gone "lib/rescript.lock" 20; then + error "Watcher did not stop in time" + git checkout "$DEP01_CONFIG" + exit 1 +fi # Restore dep01's rescript.json git checkout "$DEP01_CONFIG" # Rebuild to regenerate any artifacts that were removed by `rewatch clean` # but not rebuilt due to the modified config (e.g. Dep01.mjs). -rewatch build > /dev/null 2>&1 +if ! rewatch build > /dev/null 2>&1; then + error "Rebuild after restoring config failed" + rm -f rewatch.log + exit 1 +fi + +# Slow CI runners can still be catching up on file restoration when the build +# command returns. Wait until git no longer sees tracked deletions before doing +# the final cleanliness check. +timeout=20 +while [ "$timeout" -gt 0 ]; do + if [ -z "$(git diff --name-only --diff-filter=D .)" ]; then + break + fi + sleep 1 + timeout=$((timeout - 1)) +done rm -f rewatch.log if git diff --exit-code . > /dev/null 2>&1 && [ -z "$(git ls-files --others --exclude-standard .)" ]; diff --git a/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt b/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt index 998459a8a1c..e71ea422ce1 100644 --- a/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt +++ b/tests/analysis_tests/tests/src/expected/CreateInterface.res.txt @@ -132,4 +132,8 @@ module OrderedSet: { } let make: (~id: module(Belt.Id.Comparable with type identity = 'a and type t = 'b)) => t<'b, 'a> } +let CreateInterface$ComponentWithPolyProp: ComponentWithPolyProp.props< + [< #large | #small], +> => React.element +let CreateInterface$ComponentWithPolyProp$jsx: bool diff --git a/tests/analysis_tests/tests/src/expected/JsxV4.res.txt b/tests/analysis_tests/tests/src/expected/JsxV4.res.txt index ea1e781cb4e..45682c0a53c 100644 --- a/tests/analysis_tests/tests/src/expected/JsxV4.res.txt +++ b/tests/analysis_tests/tests/src/expected/JsxV4.res.txt @@ -32,4 +32,10 @@ module Other: { @react.component let make: (~name: string) => React.element } +let JsxV4$M4: M4.props => React.element +let JsxV4$M4$jsx: bool +let JsxV4$MM: MM.props => React.element +let JsxV4$MM$jsx: bool +let JsxV4$Other: Other.props => React.element +let JsxV4$Other$jsx: bool diff --git a/tests/build_tests/react_ppx/src/recursive_component_test.res.js b/tests/build_tests/react_ppx/src/recursive_component_test.res.js index 4e3fd9951f4..f3aa4d5fc91 100644 --- a/tests/build_tests/react_ppx/src/recursive_component_test.res.js +++ b/tests/build_tests/react_ppx/src/recursive_component_test.res.js @@ -18,7 +18,13 @@ let Rec = { mm: mm }; +let Recursive_component_test$Rec = make; + +let Recursive_component_test$Rec$jsx = true; + export { Rec, + Recursive_component_test$Rec, + Recursive_component_test$Rec$jsx, } /* No side effect */ diff --git a/tests/build_tests/rsc_component_with_props_nested/input.js b/tests/build_tests/rsc_component_with_props_nested/input.js new file mode 100644 index 00000000000..ac0939c3778 --- /dev/null +++ b/tests/build_tests/rsc_component_with_props_nested/input.js @@ -0,0 +1,33 @@ +// @ts-check + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +await execClean(); +await execBuild(); + +const outputPath = path.join(import.meta.dirname, "src", "MainLayout.res.js"); +const output = await fs.readFile(outputPath, "utf8"); +const sidebarOutputPath = path.join( + import.meta.dirname, + "src", + "Sidebar.res.js", +); +const sidebarOutput = await fs.readFile(sidebarOutputPath, "utf8"); + +assert.match( + output, + /JsxRuntime\.jsx\(Sidebar\$RscComponentWithPropsNested\.Sidebar\$Provider,/, +); +assert.doesNotMatch(output, /\.Provider\.make,/); +assert.match(sidebarOutput, /Sidebar\$Provider\$jsx/); +assert.match( + sidebarOutput, + /export \{[\s\S]*Provider,[\s\S]*Sidebar\$Provider,[\s\S]*Sidebar\$Provider\$jsx[\s\S]*\}/s, +); + +await execClean(); diff --git a/tests/build_tests/rsc_component_with_props_nested/rescript.json b/tests/build_tests/rsc_component_with_props_nested/rescript.json new file mode 100644 index 00000000000..537397ae493 --- /dev/null +++ b/tests/build_tests/rsc_component_with_props_nested/rescript.json @@ -0,0 +1,16 @@ +{ + "name": "rsc-component-with-props-nested", + "jsx": { + "version": 4 + }, + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true, + "suffix": ".res.js" + }, + "namespace": true +} diff --git a/tests/build_tests/rsc_component_with_props_nested/src/MainLayout.res b/tests/build_tests/rsc_component_with_props_nested/src/MainLayout.res new file mode 100644 index 00000000000..e2c753eeb17 --- /dev/null +++ b/tests/build_tests/rsc_component_with_props_nested/src/MainLayout.res @@ -0,0 +1,4 @@ +@react.component +let make = (~children) => { + {children} +} diff --git a/tests/build_tests/rsc_component_with_props_nested/src/React.res b/tests/build_tests/rsc_component_with_props_nested/src/React.res new file mode 100644 index 00000000000..75e3b2c2d78 --- /dev/null +++ b/tests/build_tests/rsc_component_with_props_nested/src/React.res @@ -0,0 +1,12 @@ +type element + +@val external null: element = "null" + +external string: string => element = "%identity" + +type componentLike<'props, 'return> = 'props => 'return + +type component<'props> = componentLike<'props, element> + +@module("react/jsx-runtime") +external jsx: (component<'props>, 'props) => element = "jsx" diff --git a/tests/build_tests/rsc_component_with_props_nested/src/Sidebar.res b/tests/build_tests/rsc_component_with_props_nested/src/Sidebar.res new file mode 100644 index 00000000000..b667cc58fe6 --- /dev/null +++ b/tests/build_tests/rsc_component_with_props_nested/src/Sidebar.res @@ -0,0 +1,6 @@ +module Provider = { + type props = {children: React.element} + + @react.componentWithProps + let make = props => props.children +} diff --git a/tests/build_tests/rsc_dynamic_import_nested_jsx/input.js b/tests/build_tests/rsc_dynamic_import_nested_jsx/input.js new file mode 100644 index 00000000000..13ae1728e8f --- /dev/null +++ b/tests/build_tests/rsc_dynamic_import_nested_jsx/input.js @@ -0,0 +1,30 @@ +// @ts-check + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +await execClean(); +await execBuild(); + +const outputPath = path.join(import.meta.dirname, "src", "MainLayout.res.mjs"); +const output = await fs.readFile(outputPath, "utf8"); + +assert.match( + output, + /let DynamicSidebar = await import\("\.\/Sidebar\.res\.mjs"\);/, +); +assert.match(output, /let dynamicProvider = DynamicSidebar\.Provider\.make;/); +assert.match( + output, + /JsxRuntime\.jsx\(Sidebar\$RscDynamicImportNestedJsx\.Sidebar\$Provider,/, +); +assert.doesNotMatch( + output, + /let dynamicProvider = DynamicSidebar\.Sidebar\$Provider;/, +); + +await execClean(); diff --git a/tests/build_tests/rsc_dynamic_import_nested_jsx/rescript.json b/tests/build_tests/rsc_dynamic_import_nested_jsx/rescript.json new file mode 100644 index 00000000000..f4772ad950b --- /dev/null +++ b/tests/build_tests/rsc_dynamic_import_nested_jsx/rescript.json @@ -0,0 +1,16 @@ +{ + "name": "rsc-dynamic-import-nested-jsx", + "jsx": { + "version": 4 + }, + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true, + "suffix": ".res.mjs" + }, + "namespace": true +} diff --git a/tests/build_tests/rsc_dynamic_import_nested_jsx/src/MainLayout.res b/tests/build_tests/rsc_dynamic_import_nested_jsx/src/MainLayout.res new file mode 100644 index 00000000000..42ebacc8096 --- /dev/null +++ b/tests/build_tests/rsc_dynamic_import_nested_jsx/src/MainLayout.res @@ -0,0 +1,8 @@ +module DynamicSidebar = await Sidebar + +let dynamicProvider = DynamicSidebar.Provider.make + +@react.component +let make = (~children) => { + {Option.getOr(children, React.null)} +} diff --git a/tests/build_tests/rsc_dynamic_import_nested_jsx/src/React.res b/tests/build_tests/rsc_dynamic_import_nested_jsx/src/React.res new file mode 100644 index 00000000000..75e3b2c2d78 --- /dev/null +++ b/tests/build_tests/rsc_dynamic_import_nested_jsx/src/React.res @@ -0,0 +1,12 @@ +type element + +@val external null: element = "null" + +external string: string => element = "%identity" + +type componentLike<'props, 'return> = 'props => 'return + +type component<'props> = componentLike<'props, element> + +@module("react/jsx-runtime") +external jsx: (component<'props>, 'props) => element = "jsx" diff --git a/tests/build_tests/rsc_dynamic_import_nested_jsx/src/Sidebar.res b/tests/build_tests/rsc_dynamic_import_nested_jsx/src/Sidebar.res new file mode 100644 index 00000000000..ad06cf9dc20 --- /dev/null +++ b/tests/build_tests/rsc_dynamic_import_nested_jsx/src/Sidebar.res @@ -0,0 +1,6 @@ +module Provider = { + @react.component + let make = (~children) => { + children + } +} diff --git a/tests/build_tests/rsc_mixed_runtime_import/input.js b/tests/build_tests/rsc_mixed_runtime_import/input.js new file mode 100644 index 00000000000..1e4f73b8bd2 --- /dev/null +++ b/tests/build_tests/rsc_mixed_runtime_import/input.js @@ -0,0 +1,34 @@ +// @ts-check + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +await execClean(); +await execBuild(); + +const outputPath = path.join(import.meta.dirname, "src", "MainLayout.res.mjs"); +const output = await fs.readFile(outputPath, "utf8"); + +assert.match( + output, + /import \* as Sidebar\$RscMixedRuntimeImport from "\.\/Sidebar\.res\.mjs";/, +); +assert.match( + output, + /import \* as Stdlib_OptionJs from "@rescript\/runtime\/lib\/es6\/Stdlib_Option\.js";/, +); +assert.match( + output, + /JsxRuntime\.jsx\(Sidebar\$RscMixedRuntimeImport\.Sidebar\$Provider,/, +); +assert.doesNotMatch(output, /@rescript\/runtime\/lib\/es6\/Stdlib_Option\.mjs/); +assert.equal( + output.match(/@rescript\/runtime\/lib\/es6\/Stdlib_Option\.js/g)?.length ?? 0, + 1, +); + +await execClean(); diff --git a/tests/build_tests/rsc_mixed_runtime_import/rescript.json b/tests/build_tests/rsc_mixed_runtime_import/rescript.json new file mode 100644 index 00000000000..c9a0312aa05 --- /dev/null +++ b/tests/build_tests/rsc_mixed_runtime_import/rescript.json @@ -0,0 +1,16 @@ +{ + "name": "rsc-mixed-runtime-import", + "jsx": { + "version": 4 + }, + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true, + "suffix": ".res.mjs" + }, + "namespace": true +} diff --git a/tests/build_tests/rsc_mixed_runtime_import/src/MainLayout.res b/tests/build_tests/rsc_mixed_runtime_import/src/MainLayout.res new file mode 100644 index 00000000000..4a2dfa741f1 --- /dev/null +++ b/tests/build_tests/rsc_mixed_runtime_import/src/MainLayout.res @@ -0,0 +1,8 @@ +@module("@rescript/runtime/lib/es6/Stdlib_Option.js") +external getOr: (option<'a>, 'a) => 'a = "getOr" + +@react.component +let make = (~children) => { + let child = getOr(children, React.null) + {child} +} diff --git a/tests/build_tests/rsc_mixed_runtime_import/src/React.res b/tests/build_tests/rsc_mixed_runtime_import/src/React.res new file mode 100644 index 00000000000..733b8ee02f0 --- /dev/null +++ b/tests/build_tests/rsc_mixed_runtime_import/src/React.res @@ -0,0 +1,10 @@ +type element + +@val external null: element = "null" + +type componentLike<'props, 'return> = 'props => 'return + +type component<'props> = componentLike<'props, element> + +@module("react/jsx-runtime") +external jsx: (component<'props>, 'props) => element = "jsx" diff --git a/tests/build_tests/rsc_mixed_runtime_import/src/Sidebar.res b/tests/build_tests/rsc_mixed_runtime_import/src/Sidebar.res new file mode 100644 index 00000000000..25e1db24bca --- /dev/null +++ b/tests/build_tests/rsc_mixed_runtime_import/src/Sidebar.res @@ -0,0 +1,4 @@ +module Provider = { + @react.component + let make = (~children) => children +} diff --git a/tests/build_tests/rsc_nested_jsx_deep/input.js b/tests/build_tests/rsc_nested_jsx_deep/input.js new file mode 100644 index 00000000000..e9128fe30ed --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_deep/input.js @@ -0,0 +1,33 @@ +// @ts-check + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +await execClean(); +await execBuild(); + +const outputPath = path.join(import.meta.dirname, "src", "MainLayout.res.js"); +const output = await fs.readFile(outputPath, "utf8"); +const sidebarOutputPath = path.join( + import.meta.dirname, + "src", + "Sidebar.res.js", +); +const sidebarOutput = await fs.readFile(sidebarOutputPath, "utf8"); + +assert.match( + output, + /JsxRuntime\.jsx\(Sidebar\$RscNestedJsxDeep\.Sidebar\$Group,/, +); +assert.doesNotMatch(output, /\.Group\.make,/); +assert.match(sidebarOutput, /Sidebar\$Group\$jsx/); +assert.match( + sidebarOutput, + /export \{[\s\S]*Group,[\s\S]*Sidebar\$Group,[\s\S]*Sidebar\$Group\$jsx[\s\S]*\}/s, +); + +await execClean(); diff --git a/tests/build_tests/rsc_nested_jsx_deep/rescript.json b/tests/build_tests/rsc_nested_jsx_deep/rescript.json new file mode 100644 index 00000000000..7f67937417b --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_deep/rescript.json @@ -0,0 +1,16 @@ +{ + "name": "rsc-nested-jsx-deep", + "jsx": { + "version": 4 + }, + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true, + "suffix": ".res.js" + }, + "namespace": true +} diff --git a/tests/build_tests/rsc_nested_jsx_deep/src/MainLayout.res b/tests/build_tests/rsc_nested_jsx_deep/src/MainLayout.res new file mode 100644 index 00000000000..3e31dfdfcbc --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_deep/src/MainLayout.res @@ -0,0 +1,4 @@ +@react.component +let make = (~children) => { + {children} +} diff --git a/tests/build_tests/rsc_nested_jsx_deep/src/React.res b/tests/build_tests/rsc_nested_jsx_deep/src/React.res new file mode 100644 index 00000000000..733b8ee02f0 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_deep/src/React.res @@ -0,0 +1,10 @@ +type element + +@val external null: element = "null" + +type componentLike<'props, 'return> = 'props => 'return + +type component<'props> = componentLike<'props, element> + +@module("react/jsx-runtime") +external jsx: (component<'props>, 'props) => element = "jsx" diff --git a/tests/build_tests/rsc_nested_jsx_deep/src/Sidebar.res b/tests/build_tests/rsc_nested_jsx_deep/src/Sidebar.res new file mode 100644 index 00000000000..a7feecaa77a --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_deep/src/Sidebar.res @@ -0,0 +1,6 @@ +module Group = { + @react.component + let make = (~children) => { + children + } +} diff --git a/tests/build_tests/rsc_nested_jsx_members/input.js b/tests/build_tests/rsc_nested_jsx_members/input.js new file mode 100644 index 00000000000..2814bdf5a4e --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/input.js @@ -0,0 +1,84 @@ +// @ts-check + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +await execClean(); +await execBuild(); + +const outputPath = path.join(import.meta.dirname, "src", "MainLayout.res.js"); +const output = await fs.readFile(outputPath, "utf8"); +const sidebarOutputPath = path.join( + import.meta.dirname, + "src", + "Sidebar.res.js", +); +const sidebarOutput = await fs.readFile(sidebarOutputPath, "utf8"); +const externalOutputPath = path.join( + import.meta.dirname, + "src", + "MainLayoutExternal.res.js", +); +const externalOutput = await fs.readFile(externalOutputPath, "utf8"); +const externalSidebarOutputPath = path.join( + import.meta.dirname, + "src", + "SidebarExternal.res.js", +); +const externalSidebarOutput = await fs.readFile( + externalSidebarOutputPath, + "utf8", +); +const plainAccessOutputPath = path.join( + import.meta.dirname, + "src", + "PlainAccess.res.js", +); +const plainAccessOutput = await fs.readFile(plainAccessOutputPath, "utf8"); + +assert.match( + output, + /import \* as Sidebar\$RscNestedJsxMembers from "\.\/Sidebar\.res\.js";/, +); +assert.match( + output, + /JsxRuntime\.jsx\(Sidebar\$RscNestedJsxMembers\.Sidebar\$Provider,/, +); +assert.match( + externalOutput, + /JsxRuntime\.jsx\(SidebarExternal\$RscNestedJsxMembers\.SidebarExternal\$Provider,/, +); +assert.doesNotMatch( + output, + /JsxRuntime\.jsx\(Sidebar\$RscNestedJsxMembers\.Sidebar\$RscNestedJsxMembers\$Provider,/, +); +assert.doesNotMatch( + externalOutput, + /JsxRuntime\.jsx\(SidebarExternal\$RscNestedJsxMembers\.Provider\.make,/, +); +assert.doesNotMatch(output, /\.Provider\.make,/); +assert.match( + sidebarOutput, + /export \{[\s\S]*Provider,[\s\S]*Inset,[\s\S]*Sidebar\$Provider,[\s\S]*Sidebar\$Inset,[\s\S]*\}/s, +); +assert.match(sidebarOutput, /Sidebar\$Provider\$jsx/); +assert.match(sidebarOutput, /Sidebar\$Inset\$jsx/); +assert.match( + externalSidebarOutput, + /export \{[\s\S]*Provider,[\s\S]*SidebarExternal\$Provider,[\s\S]*SidebarExternal\$Provider\$jsx[\s\S]*\}/s, +); +assert.match( + plainAccessOutput, + /let provider = Sidebar\$RscNestedJsxMembers\.Provider\.make;/, +); +assert.match( + plainAccessOutput, + /let callProvider = Sidebar\$RscNestedJsxMembers\.Provider\.make\(\{/, +); +assert.doesNotMatch(plainAccessOutput, /Sidebar\$Provider/); + +await execClean(); diff --git a/tests/build_tests/rsc_nested_jsx_members/rescript.json b/tests/build_tests/rsc_nested_jsx_members/rescript.json new file mode 100644 index 00000000000..b83ca40838d --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/rescript.json @@ -0,0 +1,16 @@ +{ + "name": "rsc-nested-jsx-members", + "jsx": { + "version": 4 + }, + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true, + "suffix": ".res.js" + }, + "namespace": true +} diff --git a/tests/build_tests/rsc_nested_jsx_members/src/MainLayout.res b/tests/build_tests/rsc_nested_jsx_members/src/MainLayout.res new file mode 100644 index 00000000000..e2c753eeb17 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/src/MainLayout.res @@ -0,0 +1,4 @@ +@react.component +let make = (~children) => { + {children} +} diff --git a/tests/build_tests/rsc_nested_jsx_members/src/MainLayoutExternal.res b/tests/build_tests/rsc_nested_jsx_members/src/MainLayoutExternal.res new file mode 100644 index 00000000000..1a25cad0b9a --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/src/MainLayoutExternal.res @@ -0,0 +1,4 @@ +@react.component +let make = (~children) => { + {children} +} diff --git a/tests/build_tests/rsc_nested_jsx_members/src/PlainAccess.res b/tests/build_tests/rsc_nested_jsx_members/src/PlainAccess.res new file mode 100644 index 00000000000..50ad67b4bb5 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/src/PlainAccess.res @@ -0,0 +1,3 @@ +let provider = Sidebar.Provider.make + +let callProvider = Sidebar.Provider.make({children: React.null}) diff --git a/tests/build_tests/rsc_nested_jsx_members/src/React.res b/tests/build_tests/rsc_nested_jsx_members/src/React.res new file mode 100644 index 00000000000..0a2d41f0096 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/src/React.res @@ -0,0 +1,30 @@ +type element + +@val external null: element = "null" + +external string: string => element = "%identity" + +external array: array => element = "%identity" + +type componentLike<'props, 'return> = 'props => 'return + +type component<'props> = componentLike<'props, element> + +@module("react") +external createElement: (component<'props>, 'props) => element = "createElement" + +@variadic @module("react") +external createElementVariadic: (component<'props>, 'props, array) => element = + "createElement" + +@module("react/jsx-runtime") +external jsx: (component<'props>, 'props) => element = "jsx" + +@module("react/jsx-runtime") +external jsxKeyed: (component<'props>, 'props, ~key: string=?, @ignore unit) => element = "jsx" + +@module("react/jsx-runtime") +external jsxs: (component<'props>, 'props) => element = "jsxs" + +@module("react/jsx-runtime") +external jsxsKeyed: (component<'props>, 'props, ~key: string=?, @ignore unit) => element = "jsxs" diff --git a/tests/build_tests/rsc_nested_jsx_members/src/Sidebar.res b/tests/build_tests/rsc_nested_jsx_members/src/Sidebar.res new file mode 100644 index 00000000000..a88c10803e2 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/src/Sidebar.res @@ -0,0 +1,13 @@ +module Provider = { + @react.component + let make = (~children) => { + children + } +} + +module Inset = { + @react.component + let make = (~children) => { + children + } +} diff --git a/tests/build_tests/rsc_nested_jsx_members/src/SidebarExternal.res b/tests/build_tests/rsc_nested_jsx_members/src/SidebarExternal.res new file mode 100644 index 00000000000..f0015e98896 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/src/SidebarExternal.res @@ -0,0 +1,4 @@ +module Provider = { + @react.component @module("./SidebarExternalImpl.js") + external make: (~children: React.element=?) => React.element = "default" +} diff --git a/tests/build_tests/rsc_nested_jsx_members/src/SidebarExternalImpl.js b/tests/build_tests/rsc_nested_jsx_members/src/SidebarExternalImpl.js new file mode 100644 index 00000000000..f94e84e1aa9 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members/src/SidebarExternalImpl.js @@ -0,0 +1,3 @@ +export default function SidebarExternalImpl(props) { + return props.children ?? null; +} diff --git a/tests/build_tests/rsc_nested_jsx_members_no_namespace/input.js b/tests/build_tests/rsc_nested_jsx_members_no_namespace/input.js new file mode 100644 index 00000000000..0a672353d49 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members_no_namespace/input.js @@ -0,0 +1,29 @@ +// @ts-check + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +await execClean(); +await execBuild(); + +const outputPath = path.join(import.meta.dirname, "src", "MainLayout.res.js"); +const output = await fs.readFile(outputPath, "utf8"); +const sidebarOutputPath = path.join( + import.meta.dirname, + "src", + "Sidebar.res.js", +); +const sidebarOutput = await fs.readFile(sidebarOutputPath, "utf8"); + +assert.match(output, /JsxRuntime\.jsx\(Sidebar\.Sidebar\$Provider,/); +assert.doesNotMatch(output, /Sidebar\.Provider\.make/); +assert.match( + sidebarOutput, + /export \{[\s\S]*Provider,[\s\S]*Sidebar\$Provider,[\s\S]*Sidebar\$Provider\$jsx[\s\S]*\}/s, +); + +await execClean(); diff --git a/tests/build_tests/rsc_nested_jsx_members_no_namespace/rescript.json b/tests/build_tests/rsc_nested_jsx_members_no_namespace/rescript.json new file mode 100644 index 00000000000..dba3aa44e5a --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members_no_namespace/rescript.json @@ -0,0 +1,16 @@ +{ + "name": "rsc-nested-jsx-members-no-namespace", + "jsx": { + "version": 4 + }, + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true, + "suffix": ".res.js" + }, + "namespace": false +} diff --git a/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/MainLayout.res b/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/MainLayout.res new file mode 100644 index 00000000000..e2c753eeb17 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/MainLayout.res @@ -0,0 +1,4 @@ +@react.component +let make = (~children) => { + {children} +} diff --git a/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/React.res b/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/React.res new file mode 100644 index 00000000000..733b8ee02f0 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/React.res @@ -0,0 +1,10 @@ +type element + +@val external null: element = "null" + +type componentLike<'props, 'return> = 'props => 'return + +type component<'props> = componentLike<'props, element> + +@module("react/jsx-runtime") +external jsx: (component<'props>, 'props) => element = "jsx" diff --git a/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/Sidebar.res b/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/Sidebar.res new file mode 100644 index 00000000000..ad06cf9dc20 --- /dev/null +++ b/tests/build_tests/rsc_nested_jsx_members_no_namespace/src/Sidebar.res @@ -0,0 +1,6 @@ +module Provider = { + @react.component + let make = (~children) => { + children + } +} diff --git a/tests/build_tests/rsc_suffix_runtime_import/input.js b/tests/build_tests/rsc_suffix_runtime_import/input.js new file mode 100644 index 00000000000..ee1dd122963 --- /dev/null +++ b/tests/build_tests/rsc_suffix_runtime_import/input.js @@ -0,0 +1,34 @@ +// @ts-check + +import * as assert from "node:assert"; +import * as fs from "node:fs/promises"; +import * as path from "node:path"; +import { setup } from "#dev/process"; + +const { execBuild, execClean } = setup(import.meta.dirname); + +await execClean(); +await execBuild(); + +const outputPath = path.join(import.meta.dirname, "src", "MainLayout.res.mjs"); +const output = await fs.readFile(outputPath, "utf8"); + +assert.match( + output, + /import \* as Stdlib_Option from "@rescript\/runtime\/lib\/es6\/Stdlib_Option\.mjs";/, +); +assert.match( + output, + /import \* as Sidebar\$RscSuffixRuntimeImport from "\.\/Sidebar\.res\.mjs";/, +); +assert.match( + output, + /JsxRuntime\.jsx\(Sidebar\$RscSuffixRuntimeImport\.Sidebar\$Provider,/, +); +assert.equal(output.match(/Stdlib_Option\.(js|mjs)/g)?.length ?? 0, 1); +assert.doesNotMatch( + output, + /@rescript\/runtime\/lib\/es6\/Stdlib_Option\.(js|mjs)";[\s\S]*@rescript\/runtime\/lib\/es6\/Stdlib_Option\.(js|mjs)";/s, +); + +await execClean(); diff --git a/tests/build_tests/rsc_suffix_runtime_import/rescript.json b/tests/build_tests/rsc_suffix_runtime_import/rescript.json new file mode 100644 index 00000000000..177e276e8f0 --- /dev/null +++ b/tests/build_tests/rsc_suffix_runtime_import/rescript.json @@ -0,0 +1,16 @@ +{ + "name": "rsc-suffix-runtime-import", + "jsx": { + "version": 4 + }, + "sources": { + "dir": "src", + "subdirs": true + }, + "package-specs": { + "module": "esmodule", + "in-source": true, + "suffix": ".res.mjs" + }, + "namespace": true +} diff --git a/tests/build_tests/rsc_suffix_runtime_import/src/MainLayout.res b/tests/build_tests/rsc_suffix_runtime_import/src/MainLayout.res new file mode 100644 index 00000000000..a45377b4f3d --- /dev/null +++ b/tests/build_tests/rsc_suffix_runtime_import/src/MainLayout.res @@ -0,0 +1,5 @@ +@react.component +let make = (~children) => { + let child = Option.getOr(children, React.null) + {child} +} diff --git a/tests/build_tests/rsc_suffix_runtime_import/src/React.res b/tests/build_tests/rsc_suffix_runtime_import/src/React.res new file mode 100644 index 00000000000..75e3b2c2d78 --- /dev/null +++ b/tests/build_tests/rsc_suffix_runtime_import/src/React.res @@ -0,0 +1,12 @@ +type element + +@val external null: element = "null" + +external string: string => element = "%identity" + +type componentLike<'props, 'return> = 'props => 'return + +type component<'props> = componentLike<'props, element> + +@module("react/jsx-runtime") +external jsx: (component<'props>, 'props) => element = "jsx" diff --git a/tests/build_tests/rsc_suffix_runtime_import/src/Sidebar.res b/tests/build_tests/rsc_suffix_runtime_import/src/Sidebar.res new file mode 100644 index 00000000000..25e1db24bca --- /dev/null +++ b/tests/build_tests/rsc_suffix_runtime_import/src/Sidebar.res @@ -0,0 +1,4 @@ +module Provider = { + @react.component + let make = (~children) => children +} diff --git a/tests/gentype_tests/typescript-react-example/src/Hooks.res.js b/tests/gentype_tests/typescript-react-example/src/Hooks.res.js index 8669c8eafed..7eafb7e5f95 100644 --- a/tests/gentype_tests/typescript-react-example/src/Hooks.res.js +++ b/tests/gentype_tests/typescript-react-example/src/Hooks.res.js @@ -204,6 +204,10 @@ let make$1 = Hooks; let $$default = Hooks; +let Hooks$RenderPropRequiresConversion$jsx = true; + +let Hooks$DD$jsx = true; + export { make$1 as make, $$default as default, @@ -219,5 +223,9 @@ export { RenderPropRequiresConversion, WithChildren, DD, + Hooks$RenderPropRequiresConversion, + Hooks$RenderPropRequiresConversion$jsx, + Hooks$DD, + Hooks$DD$jsx, } /* make Not a pure module */ diff --git a/tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt b/tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt index f942516aff7..79c661408f7 100644 --- a/tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt @@ -148,10 +148,17 @@ module C6 = { } let make = ({comp: module(Comp: Comp), x: (a, b), _}: props<_, _>): React.element => - React.jsx(Comp.make, {}) + React.jsx(@res.jsxComponentPath Comp.make, {}) let make = { let \"AliasProps$C6" = (props: props<_>) => make(props) \"AliasProps$C6" } } +let \"AliasProps$C0" = C0.make and \"AliasProps$C0$jsx" = true +let \"AliasProps$C1" = C1.make and \"AliasProps$C1$jsx" = true +let \"AliasProps$C2" = C2.make and \"AliasProps$C2$jsx" = true +let \"AliasProps$C3" = C3.make and \"AliasProps$C3$jsx" = true +let \"AliasProps$C4" = C4.make and \"AliasProps$C4$jsx" = true +let \"AliasProps$C5" = C5.make and \"AliasProps$C5$jsx" = true +let \"AliasProps$C6" = C6.make and \"AliasProps$C6$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt b/tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt index d77b11decd3..dea1b17712c 100644 --- a/tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt @@ -35,3 +35,5 @@ module C1 = { \"AsyncAwait$C1" } } +let \"AsyncAwait$C0" = C0.make and \"AsyncAwait$C0$jsx" = true +let \"AsyncAwait$C1" = C1.make and \"AsyncAwait$C1$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/defaultValueProp.res.txt b/tests/syntax_tests/data/ppx/react/expected/defaultValueProp.res.txt index 22f854f911f..82b591822f6 100644 --- a/tests/syntax_tests/data/ppx/react/expected/defaultValueProp.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/defaultValueProp.res.txt @@ -86,3 +86,7 @@ module C3 = { \"DefaultValueProp$C3" } } +let \"DefaultValueProp$C0" = C0.make and \"DefaultValueProp$C0$jsx" = true +let \"DefaultValueProp$C1" = C1.make and \"DefaultValueProp$C1$jsx" = true +let \"DefaultValueProp$C2" = C2.make and \"DefaultValueProp$C2$jsx" = true +let \"DefaultValueProp$C3" = C3.make and \"DefaultValueProp$C3$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/externalWithCustomName.res.txt b/tests/syntax_tests/data/ppx/react/expected/externalWithCustomName.res.txt index dd98fc72ff3..a7b96145c72 100644 --- a/tests/syntax_tests/data/ppx/react/expected/externalWithCustomName.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/externalWithCustomName.res.txt @@ -11,4 +11,4 @@ module Foo = { external component: React.component> = "component" } -let t = React.jsx(Foo.component, {a: 1, b: {"1"}}) +let t = React.jsx(@res.jsxComponentPath Foo.component, {a: 1, b: {"1"}}) diff --git a/tests/syntax_tests/data/ppx/react/expected/externalWithRef.res.txt b/tests/syntax_tests/data/ppx/react/expected/externalWithRef.res.txt index 44a3420fd87..f981aad592c 100644 --- a/tests/syntax_tests/data/ppx/react/expected/externalWithRef.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/externalWithRef.res.txt @@ -10,3 +10,4 @@ module V4C = { @module("componentForwardRef") external make: React.component> = "component" } +let \"ExternalWithRef$V4C" = V4C.make and \"ExternalWithRef$V4C$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/externalWithTypeVariables.res.txt b/tests/syntax_tests/data/ppx/react/expected/externalWithTypeVariables.res.txt index cbdee832996..e271934ecbb 100644 --- a/tests/syntax_tests/data/ppx/react/expected/externalWithTypeVariables.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/externalWithTypeVariables.res.txt @@ -10,3 +10,4 @@ module V4C = { @module("c") external make: React.component, React.element>> = "component" } +let \"ExternalWithTypeVariables$V4C" = V4C.make and \"ExternalWithTypeVariables$V4C$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/fileLevelConfig.res.txt b/tests/syntax_tests/data/ppx/react/expected/fileLevelConfig.res.txt index 77cde0caea1..2a3dae60b75 100644 --- a/tests/syntax_tests/data/ppx/react/expected/fileLevelConfig.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/fileLevelConfig.res.txt @@ -15,3 +15,4 @@ module V4A = { \"FileLevelConfig$V4A" } } +let \"FileLevelConfig$V4A" = V4A.make and \"FileLevelConfig$V4A$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/forwardRef.res.txt b/tests/syntax_tests/data/ppx/react/expected/forwardRef.res.txt index 8cd3bcff8ce..abe2a0b7e9e 100644 --- a/tests/syntax_tests/data/ppx/react/expected/forwardRef.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/forwardRef.res.txt @@ -45,7 +45,10 @@ module V4A = { "div", { children: ?ReactDOM.someElement( - React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}}), + React.jsx( + @res.jsxComponentPath FancyInput.make, + {ref: input, children: {React.string("Click to focus")}}, + ), ), }, ): React.element @@ -103,7 +106,10 @@ module V4AUncurried = { "div", { children: ?ReactDOM.someElement( - React.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}}), + React.jsx( + @res.jsxComponentPath FancyInput.make, + {ref: input, children: {React.string("Click to focus")}}, + ), ), }, ): React.element @@ -115,3 +121,8 @@ module V4AUncurried = { \"ForwardRef$V4AUncurried" } } +let \"ForwardRef$V4A$FancyInput" = V4A.FancyInput.make and \"ForwardRef$V4A$FancyInput$jsx" = true +let \"ForwardRef$V4A" = V4A.make and \"ForwardRef$V4A$jsx" = true +let \"ForwardRef$V4AUncurried$FancyInput" = V4AUncurried.FancyInput.make +and \"ForwardRef$V4AUncurried$FancyInput$jsx" = true +let \"ForwardRef$V4AUncurried" = V4AUncurried.make and \"ForwardRef$V4AUncurried$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/fragment.res.txt b/tests/syntax_tests/data/ppx/react/expected/fragment.res.txt index 80c3bb1d868..3af2218962c 100644 --- a/tests/syntax_tests/data/ppx/react/expected/fragment.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/fragment.res.txt @@ -7,11 +7,11 @@ let _ = React.jsxs( {children: React.array([ReactDOM.jsx("div", {}), ReactDOM.jsx("div", {})])}, ) let _ = React.jsx(React.jsxFragment, {children: React.jsx(React.jsxFragment, {})}) -let _ = React.jsx(Z.make, {}) -let _ = React.jsx(Z.make, {children: ReactDOM.jsx("div", {})}) -let _ = React.jsx(Z.make, {a: "a", children: ReactDOM.jsx("div", {})}) +let _ = React.jsx(@res.jsxComponentPath Z.make, {}) +let _ = React.jsx(@res.jsxComponentPath Z.make, {children: ReactDOM.jsx("div", {})}) +let _ = React.jsx(@res.jsxComponentPath Z.make, {a: "a", children: ReactDOM.jsx("div", {})}) let _ = React.jsxs( - Z.make, + @res.jsxComponentPath Z.make, {children: React.array([ReactDOM.jsx("div", {}), ReactDOM.jsx("div", {})])}, ) let _ = ReactDOM.jsx("div", {}) diff --git a/tests/syntax_tests/data/ppx/react/expected/interface.res.txt b/tests/syntax_tests/data/ppx/react/expected/interface.res.txt index df0f7137fb5..363f9439a0a 100644 --- a/tests/syntax_tests/data/ppx/react/expected/interface.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/interface.res.txt @@ -21,3 +21,5 @@ module NoProps = { \"Interface$NoProps" } } +let \"Interface$A" = A.make and \"Interface$A$jsx" = true +let \"Interface$NoProps" = NoProps.make and \"Interface$NoProps$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/mangleKeyword.res.txt b/tests/syntax_tests/data/ppx/react/expected/mangleKeyword.res.txt index f1e40360ec5..8b9bd814834 100644 --- a/tests/syntax_tests/data/ppx/react/expected/mangleKeyword.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/mangleKeyword.res.txt @@ -25,5 +25,7 @@ module C4A1 = { external make: React.component> = "default" } -let c4a0 = React.jsx(C4A0.make, {_open: "x", _type: "t"}) -let c4a1 = React.jsx(C4A1.make, {_open: "x", _type: "t"}) +let c4a0 = React.jsx(@res.jsxComponentPath C4A0.make, {_open: "x", _type: "t"}) +let c4a1 = React.jsx(@res.jsxComponentPath C4A1.make, {_open: "x", _type: "t"}) +let \"MangleKeyword$C4A0" = C4A0.make and \"MangleKeyword$C4A0$jsx" = true +let \"MangleKeyword$C4A1" = C4A1.make and \"MangleKeyword$C4A1$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/nested.res.txt b/tests/syntax_tests/data/ppx/react/expected/nested.res.txt index 70f8efde0e3..6cb92900d5e 100644 --- a/tests/syntax_tests/data/ppx/react/expected/nested.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/nested.res.txt @@ -8,16 +8,18 @@ module Outer = { let make = (_: props): React.element => ReactDOM.jsx("div", {}) let make = { - let \"Nested$Outer" = props => make(props) + let \"Nested$Outer$Inner" = props => make(props) - \"Nested$Outer" + \"Nested$Outer$Inner" } } - React.jsx(Inner.make, {}) + React.jsx(@res.jsxComponentPath Inner.make, {}) } let make = { let \"Nested$Outer" = props => make(props) \"Nested$Outer" } } +let \"Nested$Outer$Inner" = Outer.Inner.make and \"Nested$Outer$Inner$jsx" = true +let \"Nested$Outer" = Outer.make and \"Nested$Outer$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/newtype.res.txt b/tests/syntax_tests/data/ppx/react/expected/newtype.res.txt index 2fdecb86740..838fd0daa57 100644 --- a/tests/syntax_tests/data/ppx/react/expected/newtype.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/newtype.res.txt @@ -99,3 +99,4 @@ module Uncurried = { \"Newtype$Uncurried" } } +let \"Newtype$Uncurried" = Uncurried.make and \"Newtype$Uncurried$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/noPropsWithKey.res.txt b/tests/syntax_tests/data/ppx/react/expected/noPropsWithKey.res.txt index aa8dc64f353..bd40f4c83c7 100644 --- a/tests/syntax_tests/data/ppx/react/expected/noPropsWithKey.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/noPropsWithKey.res.txt @@ -29,8 +29,8 @@ module V4C = { React.jsxFragment, { children: React.array([ - React.jsxKeyed(V4CA.make, {}, ~key="k", ()), - React.jsxKeyed(V4CB.make, {}, ~key="k", ()), + React.jsxKeyed(@res.jsxComponentPath V4CA.make, {}, ~key="k", ()), + React.jsxKeyed(@res.jsxComponentPath V4CB.make, {}, ~key="k", ()), ]), }, ) @@ -40,3 +40,6 @@ module V4C = { \"NoPropsWithKey$V4C" } } +let \"NoPropsWithKey$V4CA" = V4CA.make and \"NoPropsWithKey$V4CA$jsx" = true +let \"NoPropsWithKey$V4CB" = V4CB.make and \"NoPropsWithKey$V4CB$jsx" = true +let \"NoPropsWithKey$V4C" = V4C.make and \"NoPropsWithKey$V4C$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/optimizeAutomaticMode.res.txt b/tests/syntax_tests/data/ppx/react/expected/optimizeAutomaticMode.res.txt index dfb3506590e..7797329571a 100644 --- a/tests/syntax_tests/data/ppx/react/expected/optimizeAutomaticMode.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/optimizeAutomaticMode.res.txt @@ -18,3 +18,4 @@ module User = { \"OptimizeAutomaticMode$User" } } +let \"OptimizeAutomaticMode$User" = User.make and \"OptimizeAutomaticMode$User$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/optionalKeyType.res.txt b/tests/syntax_tests/data/ppx/react/expected/optionalKeyType.res.txt index 0420bce343e..24d5b15c217 100644 --- a/tests/syntax_tests/data/ppx/react/expected/optionalKeyType.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/optionalKeyType.res.txt @@ -2,9 +2,9 @@ let key = None @@jsxConfig({version: 4}) -let _ = React.jsxKeyed(C.make, {}, ~key="k", ()) -let _ = React.jsxKeyed(C.make, {}, ~key=?Some("k"), ()) -let _ = React.jsxKeyed(C.make, {}, ~key?, ()) +let _ = React.jsxKeyed(@res.jsxComponentPath C.make, {}, ~key="k", ()) +let _ = React.jsxKeyed(@res.jsxComponentPath C.make, {}, ~key=?Some("k"), ()) +let _ = React.jsxKeyed(@res.jsxComponentPath C.make, {}, ~key?, ()) let _ = ReactDOM.jsxKeyed("div", {}, ~key="k", ()) let _ = ReactDOM.jsxKeyed("div", {}, ~key=?Some("k"), ()) let _ = ReactDOM.jsxKeyed("div", {}, ~key?, ()) diff --git a/tests/syntax_tests/data/ppx/react/expected/returnConstraint.res.txt b/tests/syntax_tests/data/ppx/react/expected/returnConstraint.res.txt index 7d017b6d9b7..84cd1b66fd8 100644 --- a/tests/syntax_tests/data/ppx/react/expected/returnConstraint.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/returnConstraint.res.txt @@ -47,3 +47,6 @@ module Async = { \"ReturnConstraint$Async" } } +let \"ReturnConstraint$Standard" = Standard.make and \"ReturnConstraint$Standard$jsx" = true +let \"ReturnConstraint$ForwardRef" = ForwardRef.make and \"ReturnConstraint$ForwardRef$jsx" = true +let \"ReturnConstraint$Async" = Async.make and \"ReturnConstraint$Async$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/sharedProps.res.txt b/tests/syntax_tests/data/ppx/react/expected/sharedProps.res.txt index 83c7a5fb041..70cb0dee1dc 100644 --- a/tests/syntax_tests/data/ppx/react/expected/sharedProps.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/sharedProps.res.txt @@ -67,3 +67,11 @@ module V4A8 = { external make: React.component = "default" } +let \"SharedProps$V4A1" = V4A1.make and \"SharedProps$V4A1$jsx" = true +let \"SharedProps$V4A2" = V4A2.make and \"SharedProps$V4A2$jsx" = true +let \"SharedProps$V4A3" = V4A3.make and \"SharedProps$V4A3$jsx" = true +let \"SharedProps$V4A4" = V4A4.make and \"SharedProps$V4A4$jsx" = true +let \"SharedProps$V4A5" = V4A5.make and \"SharedProps$V4A5$jsx" = true +let \"SharedProps$V4A6" = V4A6.make and \"SharedProps$V4A6$jsx" = true +let \"SharedProps$V4A7" = V4A7.make and \"SharedProps$V4A7$jsx" = true +let \"SharedProps$V4A8" = V4A8.make and \"SharedProps$V4A8$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/spreadProps.res.txt b/tests/syntax_tests/data/ppx/react/expected/spreadProps.res.txt index db116ee8d81..acc79ec0fee 100644 --- a/tests/syntax_tests/data/ppx/react/expected/spreadProps.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/spreadProps.res.txt @@ -6,10 +6,10 @@ // let c0 = // only spread props -let c1 = React.jsx(A.make, p) +let c1 = React.jsx(@res.jsxComponentPath A.make, p) // reversed order -let c2 = React.jsx(A.make, {...p, x: "x"}) +let c2 = React.jsx(@res.jsxComponentPath A.make, {...p, x: "x"}) let c3 = ReactDOM.jsx("div", p) @@ -23,5 +23,5 @@ let c5 = ReactDOM.jsxsKeyed( ) // both need to be parsed -let c6 = React.jsx(A.make, params->Obj.magic) -let c7 = React.jsx(A.make, params->Obj.magic) +let c6 = React.jsx(@res.jsxComponentPath A.make, params->Obj.magic) +let c7 = React.jsx(@res.jsxComponentPath A.make, params->Obj.magic) diff --git a/tests/syntax_tests/data/ppx/react/expected/topLevel.res.txt b/tests/syntax_tests/data/ppx/react/expected/topLevel.res.txt index 9330346a961..cc329602999 100644 --- a/tests/syntax_tests/data/ppx/react/expected/topLevel.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/topLevel.res.txt @@ -17,3 +17,4 @@ module V4A = { \"TopLevel$V4A" } } +let \"TopLevel$V4A" = V4A.make and \"TopLevel$V4A$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/typeConstraint.res.txt b/tests/syntax_tests/data/ppx/react/expected/typeConstraint.res.txt index 68a3b0278b3..92432c4be8c 100644 --- a/tests/syntax_tests/data/ppx/react/expected/typeConstraint.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/typeConstraint.res.txt @@ -14,3 +14,4 @@ module V4A = { \"TypeConstraint$V4A" } } +let \"TypeConstraint$V4A" = V4A.make and \"TypeConstraint$V4A$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/uncurriedProps.res.txt b/tests/syntax_tests/data/ppx/react/expected/uncurriedProps.res.txt index c029c8c7842..881dbc3b465 100644 --- a/tests/syntax_tests/data/ppx/react/expected/uncurriedProps.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/uncurriedProps.res.txt @@ -55,10 +55,13 @@ module Bar = { @res.jsxComponentProps type props = {} - let make = (_: props): React.element => React.jsx(Foo.make, {callback: {(_, _, _) => ()}}) + let make = (_: props): React.element => + React.jsx(@res.jsxComponentPath Foo.make, {callback: {(_, _, _) => ()}}) let make = { let \"UncurriedProps$Bar" = props => make(props) \"UncurriedProps$Bar" } } +let \"UncurriedProps$Foo" = Foo.make and \"UncurriedProps$Foo$jsx" = true +let \"UncurriedProps$Bar" = Bar.make and \"UncurriedProps$Bar$jsx" = true diff --git a/tests/syntax_tests/data/ppx/react/expected/v4.res.txt b/tests/syntax_tests/data/ppx/react/expected/v4.res.txt index a6f05b0e17f..40159d7edbc 100644 --- a/tests/syntax_tests/data/ppx/react/expected/v4.res.txt +++ b/tests/syntax_tests/data/ppx/react/expected/v4.res.txt @@ -116,3 +116,8 @@ module Rec2 = { } and mm = x => make(x) } +let \"V4$E" = E.make and \"V4$E$jsx" = true +let \"V4$EUncurried" = EUncurried.make and \"V4$EUncurried$jsx" = true +let \"V4$Rec" = Rec.make and \"V4$Rec$jsx" = true +let \"V4$Rec1" = Rec1.make and \"V4$Rec1$jsx" = true +let \"V4$Rec2" = Rec2.make and \"V4$Rec2$jsx" = true diff --git a/tests/tests/src/ExternalArity.mjs b/tests/tests/src/ExternalArity.mjs index 1de3ab38f2d..9f669373f66 100644 --- a/tests/tests/src/ExternalArity.mjs +++ b/tests/tests/src/ExternalArity.mjs @@ -37,10 +37,16 @@ let ReactTest = { FormattedMessage: FormattedMessage }; +let ExternalArity$ReactTest$FormattedMessage = ReactIntl.FormattedMessage; + +let ExternalArity$ReactTest$FormattedMessage$jsx = true; + export { f1, f2, FromTypeConstructor, ReactTest, + ExternalArity$ReactTest$FormattedMessage, + ExternalArity$ReactTest$FormattedMessage$jsx, } /* test1 Not a pure module */ diff --git a/tests/tests/src/alias_default_value_test.mjs b/tests/tests/src/alias_default_value_test.mjs index 2d785cd801a..eaa5124014f 100644 --- a/tests/tests/src/alias_default_value_test.mjs +++ b/tests/tests/src/alias_default_value_test.mjs @@ -91,6 +91,22 @@ let C8 = { make: Alias_default_value_test$C8 }; +let Alias_default_value_test$C0$jsx = true; + +let Alias_default_value_test$C1$jsx = true; + +let Alias_default_value_test$C2$jsx = true; + +let Alias_default_value_test$C3$jsx = true; + +let Alias_default_value_test$C4$jsx = true; + +let Alias_default_value_test$C6$jsx = true; + +let Alias_default_value_test$C7$jsx = true; + +let Alias_default_value_test$C8$jsx = true; + export { C0, C1, @@ -100,5 +116,21 @@ export { C6, C7, C8, + Alias_default_value_test$C0, + Alias_default_value_test$C0$jsx, + Alias_default_value_test$C1, + Alias_default_value_test$C1$jsx, + Alias_default_value_test$C2, + Alias_default_value_test$C2$jsx, + Alias_default_value_test$C3, + Alias_default_value_test$C3$jsx, + Alias_default_value_test$C4, + Alias_default_value_test$C4$jsx, + Alias_default_value_test$C6, + Alias_default_value_test$C6$jsx, + Alias_default_value_test$C7, + Alias_default_value_test$C7$jsx, + Alias_default_value_test$C8, + Alias_default_value_test$C8$jsx, } /* No side effect */ diff --git a/tests/tests/src/async_jsx.mjs b/tests/tests/src/async_jsx.mjs index f02ec3cf205..9feec016fcb 100644 --- a/tests/tests/src/async_jsx.mjs +++ b/tests/tests/src/async_jsx.mjs @@ -33,9 +33,17 @@ let Bar = { make: Async_jsx$Bar }; +let Async_jsx$Foo$jsx = true; + +let Async_jsx$Bar$jsx = true; + export { getNow, Foo, Bar, + Async_jsx$Foo, + Async_jsx$Foo$jsx, + Async_jsx$Bar, + Async_jsx$Bar$jsx, } /* react/jsx-runtime Not a pure module */ diff --git a/tests/tests/src/jsx_optional_props_test.mjs b/tests/tests/src/jsx_optional_props_test.mjs index ef3a0d326f1..b6d2b8bc792 100644 --- a/tests/tests/src/jsx_optional_props_test.mjs +++ b/tests/tests/src/jsx_optional_props_test.mjs @@ -16,8 +16,12 @@ let _element = JsxRuntime.jsx(Jsx_optional_props_test$ComponentWithOptionalProps element: JsxRuntime.jsx("div", {}) }); +let Jsx_optional_props_test$ComponentWithOptionalProps$jsx = true; + export { ComponentWithOptionalProps, _element, + Jsx_optional_props_test$ComponentWithOptionalProps, + Jsx_optional_props_test$ComponentWithOptionalProps$jsx, } /* _element Not a pure module */ diff --git a/tests/tests/src/jsxv4_newtype.mjs b/tests/tests/src/jsxv4_newtype.mjs index a6e2b5a2dc7..138850c6d73 100644 --- a/tests/tests/src/jsxv4_newtype.mjs +++ b/tests/tests/src/jsxv4_newtype.mjs @@ -33,10 +33,26 @@ let V4A3 = { make: Jsxv4_newtype$V4A3 }; +let Jsxv4_newtype$V4A$jsx = true; + +let Jsxv4_newtype$V4A1$jsx = true; + +let Jsxv4_newtype$V4A2$jsx = true; + +let Jsxv4_newtype$V4A3$jsx = true; + export { V4A, V4A1, V4A2, V4A3, + Jsxv4_newtype$V4A, + Jsxv4_newtype$V4A$jsx, + Jsxv4_newtype$V4A1, + Jsxv4_newtype$V4A1$jsx, + Jsxv4_newtype$V4A2, + Jsxv4_newtype$V4A2$jsx, + Jsxv4_newtype$V4A3, + Jsxv4_newtype$V4A3$jsx, } /* No side effect */