Skip to content

Commit 7585051

Browse files
Copilot comment fixes
1 parent 19bf531 commit 7585051

File tree

7 files changed

+98
-22
lines changed

7 files changed

+98
-22
lines changed

compiler/core/lam_compile.ml

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,23 @@ let compile output_prefix =
347347
push hidden_name;
348348
List.rev !candidates
349349
in
350+
let exported_hidden_component_name (module_id : J.module_id)
351+
(hidden_name_candidates : string list) =
352+
let rec loop = function
353+
| [] -> None
354+
| candidate :: rest -> (
355+
match
356+
Lam_compile_env.query_external_id_info
357+
~dynamic_import:module_id.dynamic_import module_id.id
358+
(candidate ^ "$jsx")
359+
with
360+
| _ -> Some candidate
361+
| exception Not_found -> loop rest)
362+
in
363+
match module_id.kind with
364+
| Ml -> loop hidden_name_candidates
365+
| _ -> None
366+
in
350367
let rewrite_nested_jsx_component_expr (jsx_tag : Lam.t)
351368
(compiled_expr : J.expression) : J.expression =
352369
let rec extract_module_id (expr : J.expression) =
@@ -356,22 +373,22 @@ let compile output_prefix =
356373
| _ -> None
357374
in
358375
match extract_nested_external_component_field jsx_tag with
359-
| Some (id, dynamic_import, hidden_name) -> (
376+
| Some (id, _dynamic_import, hidden_name) -> (
360377
let hidden_name_candidates =
361378
hidden_component_name_candidates id hidden_name
362379
in
363-
let resolved_hidden_name =
364-
match hidden_name_candidates with
365-
| candidate :: _ -> Some candidate
366-
| [] -> None
367-
in
368-
match (resolved_hidden_name, extract_module_id compiled_expr) with
369-
| Some hidden_name, Some module_id ->
370-
{
371-
compiled_expr with
372-
expression_desc = Var (Qualified (module_id, Some hidden_name));
373-
}
374-
| _ -> compiled_expr)
380+
match extract_module_id compiled_expr with
381+
| Some module_id -> (
382+
match
383+
exported_hidden_component_name module_id hidden_name_candidates
384+
with
385+
| Some hidden_name ->
386+
{
387+
compiled_expr with
388+
expression_desc = Var (Qualified (module_id, Some hidden_name));
389+
}
390+
| None -> compiled_expr)
391+
| None -> compiled_expr)
375392
| None -> compiled_expr
376393
in
377394
let rec compile_external_field (* Like [List.empty]*)

compiler/syntax/src/jsx_v4.ml

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,19 @@ let unnamespace_module_name file_name =
127127
| Some index -> String.sub file_name 0 index
128128
| None -> (
129129
match Ext_namespace.try_split_module_name file_name with
130-
| Some (module_name, _namespace) -> module_name
130+
| Some (_namespace, module_name) -> module_name
131131
| None -> file_name)
132132

133+
let maybe_hoist_nested_make_component ~(config : Jsx_common.jsx_config)
134+
~empty_loc ~full_module_name fn_name =
135+
match (fn_name, config.nested_modules) with
136+
| "make", _ :: _ ->
137+
config.hoisted_structure_items <-
138+
make_hoisted_component_binding ~empty_loc ~full_module_name
139+
config.nested_modules
140+
:: config.hoisted_structure_items
141+
| _ -> ()
142+
133143
(* Build a string representation of a module name with segments separated by $ *)
134144
let make_module_name file_name nested_modules fn_name =
135145
let file_name = unnamespace_module_name file_name in
@@ -845,13 +855,8 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
845855
Some (binding_wrapper full_expression) )
846856
in
847857
let () =
848-
match (fn_name, config.nested_modules) with
849-
| "make", _ :: _ ->
850-
config.hoisted_structure_items <-
851-
make_hoisted_component_binding ~empty_loc ~full_module_name
852-
config.nested_modules
853-
:: config.hoisted_structure_items
854-
| _ -> ()
858+
maybe_hoist_nested_make_component ~config ~empty_loc ~full_module_name
859+
fn_name
855860
in
856861
(Some props_record_type, binding, new_binding))
857862
else if Jsx_common.has_attr_on_binding Jsx_common.has_attr_with_props binding
@@ -983,7 +988,8 @@ let transform_structure_item ~config item =
983988
| {
984989
pstr_loc;
985990
pstr_desc =
986-
Pstr_primitive ({pval_attributes; pval_type} as value_description);
991+
Pstr_primitive
992+
({pval_attributes; pval_type; pval_name} as value_description);
987993
} as pstr -> (
988994
match
989995
( List.filter Jsx_common.has_attr pval_attributes,
@@ -1044,6 +1050,15 @@ let transform_structure_item ~config item =
10441050
};
10451051
}
10461052
in
1053+
let file_name = filename_from_loc pstr_loc in
1054+
let empty_loc = Location.in_file file_name in
1055+
let full_module_name =
1056+
make_module_name file_name config.nested_modules pval_name.txt
1057+
in
1058+
let () =
1059+
maybe_hoist_nested_make_component ~config ~empty_loc ~full_module_name
1060+
pval_name.txt
1061+
in
10471062
[props_record_type; new_structure]
10481063
| _ ->
10491064
Jsx_common.raise_error ~loc:pstr_loc

tests/build_tests/rsc_nested_jsx_members/input.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,21 @@ const sidebarOutputPath = path.join(
1818
"Sidebar.res.js",
1919
);
2020
const sidebarOutput = await fs.readFile(sidebarOutputPath, "utf8");
21+
const externalOutputPath = path.join(
22+
import.meta.dirname,
23+
"src",
24+
"MainLayoutExternal.res.js",
25+
);
26+
const externalOutput = await fs.readFile(externalOutputPath, "utf8");
27+
const externalSidebarOutputPath = path.join(
28+
import.meta.dirname,
29+
"src",
30+
"SidebarExternal.res.js",
31+
);
32+
const externalSidebarOutput = await fs.readFile(
33+
externalSidebarOutputPath,
34+
"utf8",
35+
);
2136

2237
assert.match(
2338
output,
@@ -27,16 +42,28 @@ assert.match(
2742
output,
2843
/JsxRuntime\.jsx\(Sidebar\$RscNestedJsxMembers\.Sidebar\$Provider,/,
2944
);
45+
assert.match(
46+
externalOutput,
47+
/JsxRuntime\.jsx\(SidebarExternal\$RscNestedJsxMembers\.SidebarExternal\$Provider,/,
48+
);
3049
assert.doesNotMatch(
3150
output,
3251
/JsxRuntime\.jsx\(Sidebar\$RscNestedJsxMembers\.Sidebar\$RscNestedJsxMembers\$Provider,/,
3352
);
53+
assert.doesNotMatch(
54+
externalOutput,
55+
/JsxRuntime\.jsx\(SidebarExternal\$RscNestedJsxMembers\.Provider\.make,/,
56+
);
3457
assert.doesNotMatch(output, /\.Provider\.make,/);
3558
assert.match(
3659
sidebarOutput,
3760
/export \{[\s\S]*Provider,[\s\S]*Inset,[\s\S]*Sidebar\$Provider,[\s\S]*Sidebar\$Inset,[\s\S]*\}/s,
3861
);
3962
assert.match(sidebarOutput, /Sidebar\$Provider\$jsx/);
4063
assert.match(sidebarOutput, /Sidebar\$Inset\$jsx/);
64+
assert.match(
65+
externalSidebarOutput,
66+
/export \{[\s\S]*Provider,[\s\S]*SidebarExternal\$Provider,[\s\S]*SidebarExternal\$Provider\$jsx[\s\S]*\}/s,
67+
);
4168

4269
await execClean();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@react.component
2+
let make = (~children) => {
3+
<SidebarExternal.Provider> {children} </SidebarExternal.Provider>
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Provider = {
2+
@react.component @module("./SidebarExternalImpl.js")
3+
external make: (~children: React.element=?) => React.element = "default"
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function SidebarExternalImpl(props) {
2+
return props.children ?? null;
3+
}

tests/tests/src/ExternalArity.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ let ReactTest = {
3737
FormattedMessage: FormattedMessage
3838
};
3939

40+
let ExternalArity$ReactTest$FormattedMessage = ReactIntl.FormattedMessage;
41+
42+
let ExternalArity$ReactTest$FormattedMessage$jsx = true;
43+
4044
export {
4145
f1,
4246
f2,
4347
FromTypeConstructor,
4448
ReactTest,
49+
ExternalArity$ReactTest$FormattedMessage,
50+
ExternalArity$ReactTest$FormattedMessage$jsx,
4551
}
4652
/* test1 Not a pure module */

0 commit comments

Comments
 (0)