Skip to content

Commit 1aaa686

Browse files
committed
Initial custom component unary tag
1 parent 9251609 commit 1aaa686

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

compiler/syntax/src/jsx_v4.ml

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ let starts_with_lowercase s =
15361536
let c = s.[0] in
15371537
Char.lowercase_ascii c = c
15381538

1539-
let _starts_with_uppercase s =
1539+
let starts_with_uppercase s =
15401540
if String.length s = 0 then false
15411541
else
15421542
let c = s.[0] in
@@ -1666,14 +1666,18 @@ module AutomaticExpr = struct
16661666
| {
16671667
pexp_desc =
16681668
Pexp_jsx_unary_element
1669-
{jsx_unary_element_tag_name = name; jsx_unary_element_props = props};
1669+
{
1670+
jsx_unary_element_tag_name = tag_name;
1671+
jsx_unary_element_props = props;
1672+
};
16701673
pexp_loc = loc;
16711674
pexp_attributes = attrs;
1672-
} -> (
1673-
match name.txt with
1674-
| Longident.Lident elementName when starts_with_lowercase elementName ->
1675+
} ->
1676+
let loc = {loc with loc_ghost = true} in
1677+
let name = Longident.flatten tag_name.txt |> String.concat "." in
1678+
if starts_with_lowercase name then
16751679
(* For example 'input' *)
1676-
let component_name_expr = constant_string ~loc:name.loc elementName in
1680+
let component_name_expr = constant_string ~loc:tag_name.loc name in
16771681
let element_binding =
16781682
match config.module_ |> String.lowercase_ascii with
16791683
| "react" -> Lident "ReactDOM"
@@ -1694,10 +1698,33 @@ module AutomaticExpr = struct
16941698

16951699
Exp.apply ~loc ~attrs jsx_expr
16961700
([(nolabel, component_name_expr); (nolabel, props)] @ key_and_unit)
1697-
| _ ->
1701+
else if starts_with_uppercase name then
1702+
(* MyModule.make *)
1703+
let make_id =
1704+
Exp.ident ~loc:tag_name.loc
1705+
{txt = Ldot (tag_name.txt, "make"); loc = tag_name.loc}
1706+
in
1707+
let jsx_expr, key_and_unit =
1708+
match try_find_key_prop props with
1709+
| None ->
1710+
( Exp.ident
1711+
{loc = Location.none; txt = module_access_name config "jsx"},
1712+
[] )
1713+
| Some key_prop ->
1714+
( Exp.ident
1715+
{
1716+
loc = Location.none;
1717+
txt = module_access_name config "jsxKeyed";
1718+
},
1719+
[key_prop; (nolabel, unit_expr ~loc:Location.none)] )
1720+
in
1721+
let props = mk_record_from_props loc props in
1722+
Exp.apply ~loc ~attrs jsx_expr
1723+
([(nolabel, make_id); (nolabel, props)] @ key_and_unit)
1724+
else
16981725
Jsx_common.raise_error ~loc
16991726
"JSX: element name is neither upper- or lowercase, got \"%s\""
1700-
(Longident.flatten name.txt |> String.concat "."))
1727+
(Longident.flatten tag_name.txt |> String.concat ".")
17011728
| e -> default_mapper.expr mapper e
17021729
end
17031730

0 commit comments

Comments
 (0)