Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit d837748

Browse files
committed
add hasReactComponent into jsx config
1 parent f64e5c5 commit d837748

File tree

1 file changed

+62
-56
lines changed

1 file changed

+62
-56
lines changed

cli/reactjs_jsx_ppx.ml

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type jsxConfig = {
99
mutable module_: string;
1010
mutable mode: string;
1111
mutable nestedModules: string list;
12+
mutable hasReactComponent: bool;
1213
}
1314

1415
let getPayloadFields payload =
@@ -2004,7 +2005,7 @@ module V4 = struct
20042005
| name when isOptional name -> (true, getLabel name, [], type_) :: types
20052006
| _ -> types
20062007

2007-
let transformStructureItem ~hasReactComponent ~config mapper item =
2008+
let transformStructureItem ~config mapper item =
20082009
match item with
20092010
(* external *)
20102011
| {
@@ -2016,52 +2017,53 @@ module V4 = struct
20162017
| [] -> [item]
20172018
| [_] ->
20182019
(* If there is another @react.component, throw error *)
2019-
if !hasReactComponent then
2020+
if config.hasReactComponent then
20202021
Location.raise_errorf ~loc:pstr_loc
20212022
"Each module should have one react component at most"
2022-
else hasReactComponent := true;
2023-
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType) =
2024-
match ptyp_desc with
2025-
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
2026-
when isLabelled name || isOptional name ->
2027-
getPropTypes ((name, ptyp_loc, type_) :: types) rest
2028-
| Ptyp_arrow (Nolabel, _type, rest) -> getPropTypes types rest
2029-
| Ptyp_arrow (name, type_, returnValue)
2030-
when isLabelled name || isOptional name ->
2031-
(returnValue, (name, returnValue.ptyp_loc, type_) :: types)
2032-
| _ -> (fullType, types)
2033-
in
2034-
let innerType, propTypes = getPropTypes [] pval_type in
2035-
let namedTypeList = List.fold_left argToConcreteType [] propTypes in
2036-
let retPropsType =
2037-
Typ.constr ~loc:pstr_loc
2038-
(Location.mkloc (Lident "props") pstr_loc)
2039-
(makePropsTypeParams namedTypeList)
2040-
in
2041-
(* type props<'id, 'name> = { @optional key: string, @optional id: 'id, ... } *)
2042-
let propsRecordType =
2043-
makePropsRecordType "props" Location.none
2044-
((true, "key", [], keyType pstr_loc) :: namedTypeList)
2045-
in
2046-
(* can't be an arrow because it will defensively uncurry *)
2047-
let newExternalType =
2048-
Ptyp_constr
2049-
( {loc = pstr_loc; txt = Ldot (Lident "React", "componentLike")},
2050-
[retPropsType; innerType] )
2051-
in
2052-
let newStructure =
2053-
{
2054-
pstr with
2055-
pstr_desc =
2056-
Pstr_primitive
2057-
{
2058-
value_description with
2059-
pval_type = {pval_type with ptyp_desc = newExternalType};
2060-
pval_attributes = List.filter otherAttrsPure pval_attributes;
2061-
};
2062-
}
2063-
in
2064-
[propsRecordType; newStructure]
2023+
else (
2024+
config.hasReactComponent <- true;
2025+
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType) =
2026+
match ptyp_desc with
2027+
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
2028+
when isLabelled name || isOptional name ->
2029+
getPropTypes ((name, ptyp_loc, type_) :: types) rest
2030+
| Ptyp_arrow (Nolabel, _type, rest) -> getPropTypes types rest
2031+
| Ptyp_arrow (name, type_, returnValue)
2032+
when isLabelled name || isOptional name ->
2033+
(returnValue, (name, returnValue.ptyp_loc, type_) :: types)
2034+
| _ -> (fullType, types)
2035+
in
2036+
let innerType, propTypes = getPropTypes [] pval_type in
2037+
let namedTypeList = List.fold_left argToConcreteType [] propTypes in
2038+
let retPropsType =
2039+
Typ.constr ~loc:pstr_loc
2040+
(Location.mkloc (Lident "props") pstr_loc)
2041+
(makePropsTypeParams namedTypeList)
2042+
in
2043+
(* type props<'id, 'name> = { @optional key: string, @optional id: 'id, ... } *)
2044+
let propsRecordType =
2045+
makePropsRecordType "props" Location.none
2046+
((true, "key", [], keyType pstr_loc) :: namedTypeList)
2047+
in
2048+
(* can't be an arrow because it will defensively uncurry *)
2049+
let newExternalType =
2050+
Ptyp_constr
2051+
( {loc = pstr_loc; txt = Ldot (Lident "React", "componentLike")},
2052+
[retPropsType; innerType] )
2053+
in
2054+
let newStructure =
2055+
{
2056+
pstr with
2057+
pstr_desc =
2058+
Pstr_primitive
2059+
{
2060+
value_description with
2061+
pval_type = {pval_type with ptyp_desc = newExternalType};
2062+
pval_attributes = List.filter otherAttrsPure pval_attributes;
2063+
};
2064+
}
2065+
in
2066+
[propsRecordType; newStructure])
20652067
| _ ->
20662068
raise
20672069
(Invalid_argument
@@ -2073,11 +2075,11 @@ module V4 = struct
20732075
let emptyLoc = Location.in_file fileName in
20742076
let mapBinding binding =
20752077
if hasAttrOnBinding binding then
2076-
if !hasReactComponent then
2078+
if config.hasReactComponent then
20772079
Location.raise_errorf ~loc:pstr_loc
20782080
"Each module should have one react component at most"
20792081
else (
2080-
hasReactComponent := true;
2082+
config.hasReactComponent <- true;
20812083
let bindingLoc = binding.pvb_loc in
20822084
let bindingPatLoc = binding.pvb_pat.ppat_loc in
20832085
let binding =
@@ -2453,7 +2455,7 @@ module V4 = struct
24532455
| _ -> [item]
24542456
[@@raises Invalid_argument]
24552457

2456-
let transformSignatureItem ~hasReactComponent _mapper item =
2458+
let transformSignatureItem ~config _mapper item =
24572459
match item with
24582460
| {
24592461
psig_loc;
@@ -2463,10 +2465,10 @@ module V4 = struct
24632465
| [] -> [item]
24642466
| [_] ->
24652467
(* If there is another @react.component, throw error *)
2466-
if !hasReactComponent then
2468+
if config.hasReactComponent then
24672469
Location.raise_errorf ~loc:psig_loc
24682470
"Each module should have one react component at most"
2469-
else hasReactComponent := true;
2471+
else config.hasReactComponent <- true;
24702472
let hasForwardRef = ref false in
24712473
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType) =
24722474
match ptyp_desc with
@@ -2649,9 +2651,14 @@ module V4 = struct
26492651
[@@raises Invalid_argument]
26502652

26512653
let module_binding ~config mapper module_binding =
2654+
let hadReactComponent = config.hasReactComponent in
2655+
(* set default false in each module *)
2656+
config.hasReactComponent <- false;
26522657
config.nestedModules <- module_binding.pmb_name.txt :: config.nestedModules;
26532658
let mapped = default_mapper.module_binding mapper module_binding in
26542659
config.nestedModules <- List.tl config.nestedModules;
2660+
(* restore it *)
2661+
config.hasReactComponent <- hadReactComponent;
26552662
mapped
26562663
[@@raises Failure]
26572664

@@ -2660,6 +2667,7 @@ module V4 = struct
26602667
let expr = expr ~config in
26612668
let module_binding = module_binding ~config in
26622669
let transformStructureItem = transformStructureItem ~config in
2670+
let transformSignatureItem = transformSignatureItem ~config in
26632671
(expr, module_binding, transformSignatureItem, transformStructureItem)
26642672
[@@raises Invalid_argument, Failure]
26652673
end
@@ -2699,7 +2707,6 @@ let getMapper ~config =
26992707
in
27002708
let signature mapper items =
27012709
let oldConfig = saveConfig () in
2702-
let hasReactComponent = ref false in
27032710
let result =
27042711
List.map
27052712
(fun item ->
@@ -2708,8 +2715,7 @@ let getMapper ~config =
27082715
| _ -> ());
27092716
let item = default_mapper.signature_item mapper item in
27102717
if config.version = 3 then transformSignatureItem3 mapper item
2711-
else if config.version = 4 then
2712-
transformSignatureItem4 ~hasReactComponent mapper item
2718+
else if config.version = 4 then transformSignatureItem4 mapper item
27132719
else [item])
27142720
items
27152721
|> List.flatten
@@ -2720,7 +2726,6 @@ let getMapper ~config =
27202726
in
27212727
let structure mapper items =
27222728
let oldConfig = saveConfig () in
2723-
let hasReactComponent = ref false in
27242729
let result =
27252730
List.map
27262731
(fun item ->
@@ -2729,8 +2734,7 @@ let getMapper ~config =
27292734
| _ -> ());
27302735
let item = default_mapper.structure_item mapper item in
27312736
if config.version = 3 then transformStructureItem3 mapper item
2732-
else if config.version = 4 then
2733-
transformStructureItem4 ~hasReactComponent mapper item
2737+
else if config.version = 4 then transformStructureItem4 mapper item
27342738
else [item])
27352739
items
27362740
|> List.flatten
@@ -2750,6 +2754,7 @@ let rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
27502754
module_ = jsxModule;
27512755
mode = jsxMode;
27522756
nestedModules = [];
2757+
hasReactComponent = false;
27532758
}
27542759
in
27552760
let mapper = getMapper ~config in
@@ -2764,6 +2769,7 @@ let rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
27642769
module_ = jsxModule;
27652770
mode = jsxMode;
27662771
nestedModules = [];
2772+
hasReactComponent = false;
27672773
}
27682774
in
27692775
let mapper = getMapper ~config in

0 commit comments

Comments
 (0)