@@ -9,6 +9,7 @@ type jsxConfig = {
9
9
mutable module_ : string ;
10
10
mutable mode : string ;
11
11
mutable nestedModules : string list ;
12
+ mutable hasReactComponent : bool ;
12
13
}
13
14
14
15
let getPayloadFields payload =
@@ -2004,7 +2005,7 @@ module V4 = struct
2004
2005
| name when isOptional name -> (true , getLabel name, [] , type_) :: types
2005
2006
| _ -> types
2006
2007
2007
- let transformStructureItem ~hasReactComponent ~ config mapper item =
2008
+ let transformStructureItem ~config mapper item =
2008
2009
match item with
2009
2010
(* external *)
2010
2011
| {
@@ -2016,52 +2017,53 @@ module V4 = struct
2016
2017
| [] -> [item]
2017
2018
| [_] ->
2018
2019
(* If there is another @react.component, throw error *)
2019
- if ! hasReactComponent then
2020
+ if config. hasReactComponent then
2020
2021
Location. raise_errorf ~loc: pstr_loc
2021
2022
" 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])
2065
2067
| _ ->
2066
2068
raise
2067
2069
(Invalid_argument
@@ -2073,11 +2075,11 @@ module V4 = struct
2073
2075
let emptyLoc = Location. in_file fileName in
2074
2076
let mapBinding binding =
2075
2077
if hasAttrOnBinding binding then
2076
- if ! hasReactComponent then
2078
+ if config. hasReactComponent then
2077
2079
Location. raise_errorf ~loc: pstr_loc
2078
2080
" Each module should have one react component at most"
2079
2081
else (
2080
- hasReactComponent := true ;
2082
+ config. hasReactComponent < - true ;
2081
2083
let bindingLoc = binding.pvb_loc in
2082
2084
let bindingPatLoc = binding.pvb_pat.ppat_loc in
2083
2085
let binding =
@@ -2453,7 +2455,7 @@ module V4 = struct
2453
2455
| _ -> [item]
2454
2456
[@@ raises Invalid_argument ]
2455
2457
2456
- let transformSignatureItem ~hasReactComponent _mapper item =
2458
+ let transformSignatureItem ~config _mapper item =
2457
2459
match item with
2458
2460
| {
2459
2461
psig_loc;
@@ -2463,10 +2465,10 @@ module V4 = struct
2463
2465
| [] -> [item]
2464
2466
| [_] ->
2465
2467
(* If there is another @react.component, throw error *)
2466
- if ! hasReactComponent then
2468
+ if config. hasReactComponent then
2467
2469
Location. raise_errorf ~loc: psig_loc
2468
2470
" Each module should have one react component at most"
2469
- else hasReactComponent := true ;
2471
+ else config. hasReactComponent < - true ;
2470
2472
let hasForwardRef = ref false in
2471
2473
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType ) =
2472
2474
match ptyp_desc with
@@ -2649,9 +2651,14 @@ module V4 = struct
2649
2651
[@@ raises Invalid_argument ]
2650
2652
2651
2653
let module_binding ~config mapper module_binding =
2654
+ let hadReactComponent = config.hasReactComponent in
2655
+ (* set default false in each module *)
2656
+ config.hasReactComponent < - false ;
2652
2657
config.nestedModules < - module_binding.pmb_name.txt :: config.nestedModules;
2653
2658
let mapped = default_mapper.module_binding mapper module_binding in
2654
2659
config.nestedModules < - List. tl config.nestedModules;
2660
+ (* restore it *)
2661
+ config.hasReactComponent < - hadReactComponent;
2655
2662
mapped
2656
2663
[@@ raises Failure ]
2657
2664
@@ -2660,6 +2667,7 @@ module V4 = struct
2660
2667
let expr = expr ~config in
2661
2668
let module_binding = module_binding ~config in
2662
2669
let transformStructureItem = transformStructureItem ~config in
2670
+ let transformSignatureItem = transformSignatureItem ~config in
2663
2671
(expr, module_binding, transformSignatureItem, transformStructureItem)
2664
2672
[@@ raises Invalid_argument , Failure ]
2665
2673
end
@@ -2699,7 +2707,6 @@ let getMapper ~config =
2699
2707
in
2700
2708
let signature mapper items =
2701
2709
let oldConfig = saveConfig () in
2702
- let hasReactComponent = ref false in
2703
2710
let result =
2704
2711
List. map
2705
2712
(fun item ->
@@ -2708,8 +2715,7 @@ let getMapper ~config =
2708
2715
| _ -> () );
2709
2716
let item = default_mapper.signature_item mapper item in
2710
2717
if config.version = 3 then transformSignatureItem3 mapper item
2711
- else if config.version = 4 then
2712
- transformSignatureItem4 ~has ReactComponent mapper item
2718
+ else if config.version = 4 then transformSignatureItem4 mapper item
2713
2719
else [item])
2714
2720
items
2715
2721
|> List. flatten
@@ -2720,7 +2726,6 @@ let getMapper ~config =
2720
2726
in
2721
2727
let structure mapper items =
2722
2728
let oldConfig = saveConfig () in
2723
- let hasReactComponent = ref false in
2724
2729
let result =
2725
2730
List. map
2726
2731
(fun item ->
@@ -2729,8 +2734,7 @@ let getMapper ~config =
2729
2734
| _ -> () );
2730
2735
let item = default_mapper.structure_item mapper item in
2731
2736
if config.version = 3 then transformStructureItem3 mapper item
2732
- else if config.version = 4 then
2733
- transformStructureItem4 ~has ReactComponent mapper item
2737
+ else if config.version = 4 then transformStructureItem4 mapper item
2734
2738
else [item])
2735
2739
items
2736
2740
|> List. flatten
@@ -2750,6 +2754,7 @@ let rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
2750
2754
module_ = jsxModule;
2751
2755
mode = jsxMode;
2752
2756
nestedModules = [] ;
2757
+ hasReactComponent = false ;
2753
2758
}
2754
2759
in
2755
2760
let mapper = getMapper ~config in
@@ -2764,6 +2769,7 @@ let rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
2764
2769
module_ = jsxModule;
2765
2770
mode = jsxMode;
2766
2771
nestedModules = [] ;
2772
+ hasReactComponent = false ;
2767
2773
}
2768
2774
in
2769
2775
let mapper = getMapper ~config in
0 commit comments