Skip to content

Commit 084f176

Browse files
committed
move to support react.component and generic jsx.component only
1 parent 7dcd211 commit 084f176

File tree

3 files changed

+31
-36
lines changed

3 files changed

+31
-36
lines changed

jscomp/syntax/src/jsx_common.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ type jsxConfig = {
1111

1212
let mkModuleAccessName config = String.capitalize_ascii config.module_
1313

14-
let mkJsxComponentName config =
15-
String.lowercase_ascii config.module_ ^ ".component"
16-
1714
(* Helper method to look up the [@react.component] attribute *)
18-
let hasAttr ~config (loc, _) = loc.txt = mkJsxComponentName config
15+
let hasAttr (loc, _) =
16+
match loc.txt with
17+
| "react.component" | "jsx.component" -> true
18+
| _ -> false
1919

2020
(* Iterate over the attributes and try to find the [@react.component] attribute *)
21-
let hasAttrOnBinding ~config {pvb_attributes} =
22-
List.find_opt (hasAttr ~config) pvb_attributes <> None
21+
let hasAttrOnBinding {pvb_attributes} =
22+
List.find_opt hasAttr pvb_attributes <> None
2323

24-
let coreTypeOfAttrs ~config attributes =
24+
let coreTypeOfAttrs attributes =
2525
List.find_map
2626
(fun ({txt}, payload) ->
2727
match (txt, payload) with
28-
| txt, PTyp coreType when txt = mkJsxComponentName config -> Some coreType
28+
| ("react.component" | "jsx.component"), PTyp coreType -> Some coreType
2929
| _ -> None)
3030
attributes
3131

jscomp/syntax/src/jsx_v4.ml

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ open Parsetree
55
open Longident
66

77
let moduleAccessName = Jsx_common.mkModuleAccessName
8-
let jsxComponentName = Jsx_common.mkJsxComponentName
98

109
let nolabel = Nolabel
1110

@@ -119,7 +118,10 @@ let extractChildren ?(removeLastPositionUnit = false) ~loc propsAndChildren =
119118
let merlinFocus = ({loc = Location.none; txt = "merlin.focus"}, PStr [])
120119

121120
(* Helper method to filter out any attribute that isn't [@react.component] *)
122-
let otherAttrsPure ~config (loc, _) = loc.txt <> jsxComponentName config
121+
let otherAttrsPure (loc, _) =
122+
match loc.txt with
123+
| "react.component" | "jsx.component" -> false
124+
| _ -> true
123125

124126
(* Finds the name of the variable the binding is assigned to, otherwise raises Invalid_argument *)
125127
let rec getFnName binding =
@@ -904,12 +906,10 @@ let vbMatchExpr namedArgList expr =
904906
aux (List.rev namedArgList)
905907

906908
let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
907-
if Jsx_common.hasAttrOnBinding ~config binding then (
909+
if Jsx_common.hasAttrOnBinding binding then (
908910
checkMultipleComponents ~config ~loc:pstr_loc;
909911
let binding = Jsx_common.removeArity binding in
910-
let coreTypeOfAttr =
911-
Jsx_common.coreTypeOfAttrs ~config binding.pvb_attributes
912-
in
912+
let coreTypeOfAttr = Jsx_common.coreTypeOfAttrs binding.pvb_attributes in
913913
let typVarsOfCoreType =
914914
coreTypeOfAttr
915915
|> Option.map Jsx_common.typVarsOfCoreType
@@ -922,8 +922,7 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
922922
binding with
923923
pvb_pat = {binding.pvb_pat with ppat_loc = emptyLoc};
924924
pvb_loc = emptyLoc;
925-
pvb_attributes =
926-
binding.pvb_attributes |> List.filter (otherAttrsPure ~config);
925+
pvb_attributes = binding.pvb_attributes |> List.filter otherAttrsPure;
927926
}
928927
in
929928
let fnName = getFnName binding.pvb_pat in
@@ -1152,7 +1151,7 @@ let transformStructureItem ~config item =
11521151
pstr_desc =
11531152
Pstr_primitive ({pval_attributes; pval_type} as value_description);
11541153
} as pstr -> (
1155-
match List.filter (Jsx_common.hasAttr ~config) pval_attributes with
1154+
match List.filter Jsx_common.hasAttr pval_attributes with
11561155
| [] -> [item]
11571156
| [_] ->
11581157
checkMultipleComponents ~config ~loc:pstr_loc;
@@ -1161,7 +1160,7 @@ let transformStructureItem ~config item =
11611160
let pval_type = Jsx_common.extractUncurried pval_type in
11621161
let coreTypeOfAttr = Jsx_common.coreTypeOfAttrs pval_attributes in
11631162
let typVarsOfCoreType =
1164-
coreTypeOfAttr ~config
1163+
coreTypeOfAttr
11651164
|> Option.map Jsx_common.typVarsOfCoreType
11661165
|> Option.value ~default:[]
11671166
in
@@ -1183,7 +1182,7 @@ let transformStructureItem ~config item =
11831182
let retPropsType =
11841183
Typ.constr ~loc:pstr_loc
11851184
(Location.mkloc (Lident "props") pstr_loc)
1186-
(match coreTypeOfAttr ~config with
1185+
(match coreTypeOfAttr with
11871186
| None -> makePropsTypeParams namedTypeList
11881187
| Some _ -> (
11891188
match typVarsOfCoreType with
@@ -1192,8 +1191,8 @@ let transformStructureItem ~config item =
11921191
in
11931192
(* type props<'x, 'y> = { x: 'x, y?: 'y, ... } *)
11941193
let propsRecordType =
1195-
makePropsRecordType ~coreTypeOfAttr:(coreTypeOfAttr ~config)
1196-
~typVarsOfCoreType "props" pstr_loc namedTypeList
1194+
makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType "props" pstr_loc
1195+
namedTypeList
11971196
in
11981197
(* can't be an arrow because it will defensively uncurry *)
11991198
let newExternalType =
@@ -1212,8 +1211,7 @@ let transformStructureItem ~config item =
12121211
{
12131212
value_description with
12141213
pval_type = {pval_type with ptyp_desc = newExternalType};
1215-
pval_attributes =
1216-
List.filter (otherAttrsPure ~config) pval_attributes;
1214+
pval_attributes = List.filter otherAttrsPure pval_attributes;
12171215
};
12181216
}
12191217
in
@@ -1259,7 +1257,7 @@ let transformSignatureItem ~config item =
12591257
psig_loc;
12601258
psig_desc = Psig_value ({pval_attributes; pval_type} as psig_desc);
12611259
} as psig -> (
1262-
match List.filter (Jsx_common.hasAttr ~config) pval_attributes with
1260+
match List.filter Jsx_common.hasAttr pval_attributes with
12631261
| [] -> [item]
12641262
| [_] ->
12651263
checkMultipleComponents ~config ~loc:psig_loc;
@@ -1269,7 +1267,7 @@ let transformSignatureItem ~config item =
12691267
let hasForwardRef = ref false in
12701268
let coreTypeOfAttr = Jsx_common.coreTypeOfAttrs pval_attributes in
12711269
let typVarsOfCoreType =
1272-
coreTypeOfAttr ~config
1270+
coreTypeOfAttr
12731271
|> Option.map Jsx_common.typVarsOfCoreType
12741272
|> Option.value ~default:[]
12751273
in
@@ -1298,16 +1296,16 @@ let transformSignatureItem ~config item =
12981296
let retPropsType =
12991297
Typ.constr
13001298
(Location.mkloc (Lident "props") psig_loc)
1301-
(match coreTypeOfAttr ~config with
1299+
(match coreTypeOfAttr with
13021300
| None -> makePropsTypeParams namedTypeList
13031301
| Some _ -> (
13041302
match typVarsOfCoreType with
13051303
| [] -> []
13061304
| _ -> [Typ.any ()]))
13071305
in
13081306
let propsRecordType =
1309-
makePropsRecordTypeSig ~coreTypeOfAttr:(coreTypeOfAttr ~config)
1310-
~typVarsOfCoreType "props" psig_loc
1307+
makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType "props"
1308+
psig_loc
13111309
((* If there is Nolabel arg, regard the type as ref in forwardRef *)
13121310
(if !hasForwardRef then
13131311
[(true, "ref", [], Location.none, refType Location.none)]
@@ -1331,8 +1329,7 @@ let transformSignatureItem ~config item =
13311329
{
13321330
psig_desc with
13331331
pval_type = {pval_type with ptyp_desc = newExternalType};
1334-
pval_attributes =
1335-
List.filter (otherAttrsPure ~config) pval_attributes;
1332+
pval_attributes = List.filter otherAttrsPure pval_attributes;
13361333
};
13371334
}
13381335
in

jscomp/syntax/src/reactjs_jsx_v3.ml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ let jsxMapper ~config =
594594
({pval_name = {txt = fnName}; pval_attributes; pval_type} as
595595
value_description);
596596
} as pstr -> (
597-
match List.filter (Jsx_common.hasAttr ~config) pval_attributes with
597+
match List.filter Jsx_common.hasAttr pval_attributes with
598598
| [] -> [item]
599599
| [_] ->
600600
let pval_type = Jsx_common.extractUncurried pval_type in
@@ -648,7 +648,7 @@ let jsxMapper ~config =
648648
let fileName = filenameFromLoc pstr_loc in
649649
let emptyLoc = Location.in_file fileName in
650650
let mapBinding binding =
651-
if Jsx_common.hasAttrOnBinding ~config binding then
651+
if Jsx_common.hasAttrOnBinding binding then
652652
let binding = Jsx_common.removeArity binding in
653653
let bindingLoc = binding.pvb_loc in
654654
let bindingPatLoc = binding.pvb_pat.ppat_loc in
@@ -814,9 +814,7 @@ let jsxMapper ~config =
814814
in
815815
let bindingWrapper, hasUnit, expression = modifiedBinding binding in
816816
let reactComponentAttribute =
817-
try
818-
Some
819-
(List.find (Jsx_common.hasAttr ~config) binding.pvb_attributes)
817+
try Some (List.find Jsx_common.hasAttr binding.pvb_attributes)
820818
with Not_found -> None
821819
in
822820
let _attr_loc, payload =
@@ -1039,7 +1037,7 @@ let jsxMapper ~config =
10391037
({pval_name = {txt = fnName}; pval_attributes; pval_type} as
10401038
psig_desc);
10411039
} as psig -> (
1042-
match List.filter (Jsx_common.hasAttr ~config) pval_attributes with
1040+
match List.filter Jsx_common.hasAttr pval_attributes with
10431041
| [] -> [item]
10441042
| [_] ->
10451043
let pval_type = Jsx_common.extractUncurried pval_type in

0 commit comments

Comments
 (0)