Skip to content

Commit 1aa0d34

Browse files
committed
generalize more
1 parent 366f1b1 commit 1aa0d34

File tree

5 files changed

+123
-76
lines changed

5 files changed

+123
-76
lines changed

jscomp/bsc/rescript_compiler_main.ml

-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
251251
"*internal* Set jsx version";
252252

253253
"-bs-jsx-module", string_call (fun i ->
254-
(if i <> "react" then Bsc_args.bad_arg (" Not supported jsx-module : " ^ i));
255254
Js_config.jsx_module := Js_config.jsx_module_of_string i),
256255
"*internal* Set jsx module";
257256

jscomp/syntax/src/jsx_common.ml

+12-7
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ type jsxConfig = {
66
mutable module_: string;
77
mutable mode: string;
88
mutable nestedModules: string list;
9-
mutable hasReactComponent: bool;
9+
mutable hasComponent: bool;
1010
}
1111

12+
let mkModuleAccessName config = String.capitalize_ascii config.module_
13+
14+
let mkJsxComponentName config =
15+
String.lowercase_ascii config.module_ ^ ".component"
16+
1217
(* Helper method to look up the [@react.component] attribute *)
13-
let hasAttr (loc, _) = loc.txt = "react.component"
18+
let hasAttr ~config (loc, _) = loc.txt = mkJsxComponentName config
1419

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

19-
let coreTypeOfAttrs attributes =
24+
let coreTypeOfAttrs ~config attributes =
2025
List.find_map
2126
(fun ({txt}, payload) ->
2227
match (txt, payload) with
23-
| "react.component", PTyp coreType -> Some coreType
28+
| txt, PTyp coreType when txt = mkJsxComponentName config -> Some coreType
2429
| _ -> None)
2530
attributes
2631

@@ -37,7 +42,7 @@ let typVarsOfCoreType {ptyp_desc} =
3742

3843
let raiseError ~loc msg = Location.raise_errorf ~loc msg
3944

40-
let raiseErrorMultipleReactComponent ~loc =
45+
let raiseErrorMultipleComponent ~loc =
4146
raiseError ~loc
4247
"Only one component definition is allowed for each module. Move to a \
4348
submodule or other file if necessary."

jscomp/syntax/src/jsx_ppx.ml

+7-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ let updateConfig config payload =
5151
(match getInt ~key:"version" fields with
5252
| None -> ()
5353
| Some i -> config.Jsx_common.version <- i);
54-
(match getString ~key:"module" fields with
54+
(match getString ~key:"module_" fields with
5555
| None -> ()
5656
| Some s -> config.module_ <- s);
5757
match getString ~key:"mode" fields with
@@ -89,18 +89,18 @@ let getMapper ~config =
8989
version = config.version;
9090
module_ = config.module_;
9191
mode = config.mode;
92-
hasReactComponent = config.hasReactComponent;
92+
hasComponent = config.hasComponent;
9393
}
9494
in
9595
let restoreConfig oldConfig =
9696
config.version <- oldConfig.Jsx_common.version;
9797
config.module_ <- oldConfig.module_;
9898
config.mode <- oldConfig.mode;
99-
config.hasReactComponent <- oldConfig.hasReactComponent
99+
config.hasComponent <- oldConfig.hasComponent
100100
in
101101
let signature mapper items =
102102
let oldConfig = saveConfig () in
103-
config.hasReactComponent <- false;
103+
config.hasComponent <- false;
104104
let result =
105105
List.map
106106
(fun item ->
@@ -119,7 +119,7 @@ let getMapper ~config =
119119
in
120120
let structure mapper items =
121121
let oldConfig = saveConfig () in
122-
config.hasReactComponent <- false;
122+
config.hasComponent <- false;
123123
let result =
124124
List.map
125125
(fun item ->
@@ -147,7 +147,7 @@ let rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
147147
module_ = jsxModule;
148148
mode = jsxMode;
149149
nestedModules = [];
150-
hasReactComponent = false;
150+
hasComponent = false;
151151
}
152152
in
153153
let mapper = getMapper ~config in
@@ -161,7 +161,7 @@ let rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
161161
module_ = jsxModule;
162162
mode = jsxMode;
163163
nestedModules = [];
164-
hasReactComponent = false;
164+
hasComponent = false;
165165
}
166166
in
167167
let mapper = getMapper ~config in

0 commit comments

Comments
 (0)