Skip to content

Commit b88e74f

Browse files
authored
[RFC] Generic JSX transform (#6565)
* rename, config, etc * generalize more * allow simplified @@jsxConfig * move to support react.component and generic jsx.component only * Revert "allow simplified @@jsxConfig" This reverts commit 7dcd211. * move fn * better name * changelog
1 parent 28b3b9a commit b88e74f

13 files changed

+177
-134
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
#### :rocket: New Feature
1616

17-
- experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250
17+
- Experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250
18+
- Experimental support for generic/custom JSX transforms. https://github.com/rescript-lang/rescript-compiler/pull/6565
1819

1920
#### :bug: Bug Fix
2021

jscomp/bsb/bsb_jsx.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
type version = Jsx_v3 | Jsx_v4
2-
type module_ = React
2+
type module_ = React | Generic of {moduleName: string}
33
type mode = Classic | Automatic
44
type dependencies = string list
55

@@ -15,7 +15,7 @@ let encode_no_nl jsx =
1515
| None -> ""
1616
| Some Jsx_v3 -> "3"
1717
| Some Jsx_v4 -> "4")
18-
^ (match jsx.module_ with None -> "" | Some React -> "React")
18+
^ (match jsx.module_ with None -> "" | Some React -> "React" | Some Generic {moduleName} -> moduleName)
1919
^
2020
match jsx.mode with
2121
| None -> ""
@@ -55,10 +55,10 @@ let from_map map =
5555
`Obj
5656
(fun m ->
5757
match m.?(Bsb_build_schemas.jsx_module) with
58-
| Some (Str { loc; str }) -> (
58+
| Some (Str { str }) -> (
5959
match str with
6060
| "react" -> module_ := Some React
61-
| _ -> Bsb_exception.errorf ~loc "Unsupported jsx module %s" str)
61+
| moduleName -> module_ := Some (Generic {moduleName}))
6262
| Some x ->
6363
Bsb_exception.config_error x
6464
"Unexpected input (jsx module name) for jsx module"

jscomp/bsb/bsb_ninja_rule.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ let make_custom_rules ~(gentype_config : Bsb_config_types.gentype_config)
169169
| None, None -> ());
170170
(match jsx.module_ with
171171
| None -> ()
172-
| Some React -> Ext_buffer.add_string buf " -bs-jsx-module react");
172+
| Some React -> Ext_buffer.add_string buf " -bs-jsx-module react"
173+
| Some Generic {moduleName} -> Ext_buffer.add_string buf (" -bs-jsx-module " ^ moduleName));
173174
(match jsx.mode with
174175
| None -> ()
175176
| Some Classic -> Ext_buffer.add_string buf " -bs-jsx-mode classic"

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/common/js_config.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
(** Browser is not set via command line only for internal use *)
2626

2727
type jsx_version = Jsx_v3 | Jsx_v4
28-
type jsx_module = React
28+
type jsx_module = React | Generic of {moduleName: string}
2929
type jsx_mode = Classic | Automatic
3030

3131
let no_version_header = ref false
@@ -64,6 +64,7 @@ let int_of_jsx_version = function
6464

6565
let string_of_jsx_module = function
6666
| React -> "react"
67+
| Generic {moduleName} -> moduleName
6768

6869
let string_of_jsx_mode = function
6970
| Classic -> "classic"
@@ -76,7 +77,7 @@ let jsx_version_of_int = function
7677

7778
let jsx_module_of_string = function
7879
| "react" -> React
79-
| _ -> React
80+
| moduleName -> Generic {moduleName}
8081

8182
let jsx_mode_of_string = function
8283
| "classic" -> Classic

jscomp/common/js_config.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
type jsx_version = Jsx_v3 | Jsx_v4
26-
type jsx_module = React
26+
type jsx_module = React | Generic of {moduleName: string}
2727
type jsx_mode = Classic | Automatic
2828

2929
(* val get_packages_info :

jscomp/frontend/ppx_entry.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
3535
let jsxVersion = int_of_jsx_version jsxVersion in
3636
let jsxModule = string_of_jsx_module !jsx_module in
3737
let jsxMode = string_of_jsx_mode !jsx_mode in
38-
Reactjs_jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode ast
38+
Jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode ast
3939
in
4040
if !Js_config.no_builtin_ppx then ast
4141
else
@@ -55,7 +55,7 @@ let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
5555
let jsxVersion = int_of_jsx_version jsxVersion in
5656
let jsxModule = string_of_jsx_module !jsx_module in
5757
let jsxMode = string_of_jsx_mode !jsx_mode in
58-
Reactjs_jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode ast
58+
Jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode ast
5959
in
6060
if !Js_config.no_builtin_ppx then ast
6161
else

jscomp/syntax/cli/res_cli.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ module CliArgProcessor = struct
284284
else exit 1)
285285
else
286286
let parsetree =
287-
Reactjs_jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
287+
Jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
288288
parseResult.parsetree
289289
in
290290
printEngine.printInterface ~width ~filename
@@ -300,7 +300,7 @@ module CliArgProcessor = struct
300300
else exit 1)
301301
else
302302
let parsetree =
303-
Reactjs_jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
303+
Jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
304304
parseResult.parsetree
305305
in
306306
printEngine.printImplementation ~width ~filename

jscomp/syntax/src/react_jsx_common.ml jscomp/syntax/src/jsx_common.ml

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ 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

1212
(* Helper method to look up the [@react.component] attribute *)
13-
let hasAttr (loc, _) = loc.txt = "react.component"
13+
let hasAttr (loc, _) =
14+
match loc.txt with
15+
| "react.component" | "jsx.component" -> true
16+
| _ -> false
1417

1518
(* Iterate over the attributes and try to find the [@react.component] attribute *)
1619
let hasAttrOnBinding {pvb_attributes} =
@@ -20,7 +23,7 @@ let coreTypeOfAttrs attributes =
2023
List.find_map
2124
(fun ({txt}, payload) ->
2225
match (txt, payload) with
23-
| "react.component", PTyp coreType -> Some coreType
26+
| ("react.component" | "jsx.component"), PTyp coreType -> Some coreType
2427
| _ -> None)
2528
attributes
2629

@@ -37,7 +40,7 @@ let typVarsOfCoreType {ptyp_desc} =
3740

3841
let raiseError ~loc msg = Location.raise_errorf ~loc msg
3942

40-
let raiseErrorMultipleReactComponent ~loc =
43+
let raiseErrorMultipleComponent ~loc =
4144
raiseError ~loc
4245
"Only one component definition is allowed for each module. Move to a \
4346
submodule or other file if necessary."

jscomp/syntax/src/reactjs_jsx_ppx.ml jscomp/syntax/src/jsx_ppx.ml

+12-12
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ let updateConfig config payload =
5050
let fields = getPayloadFields payload in
5151
(match getInt ~key:"version" fields with
5252
| None -> ()
53-
| Some i -> config.React_jsx_common.version <- i);
54-
(match getString ~key:"module" fields with
53+
| Some i -> config.Jsx_common.version <- i);
54+
(match getString ~key:"module_" fields with
5555
| None -> ()
5656
| Some s -> config.module_ <- s);
5757
match getString ~key:"mode" fields with
@@ -68,7 +68,7 @@ let getMapper ~config =
6868
Reactjs_jsx_v3.jsxMapper ~config
6969
in
7070
let expr4, module_binding4, transformSignatureItem4, transformStructureItem4 =
71-
Reactjs_jsx_v4.jsxMapper ~config
71+
Jsx_v4.jsxMapper ~config
7272
in
7373

7474
let expr mapper e =
@@ -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 =
96-
config.version <- oldConfig.React_jsx_common.version;
96+
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 ->
@@ -143,11 +143,11 @@ let rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
143143
(code : Parsetree.structure) : Parsetree.structure =
144144
let config =
145145
{
146-
React_jsx_common.version = jsxVersion;
146+
Jsx_common.version = jsxVersion;
147147
module_ = jsxModule;
148148
mode = jsxMode;
149149
nestedModules = [];
150-
hasReactComponent = false;
150+
hasComponent = false;
151151
}
152152
in
153153
let mapper = getMapper ~config in
@@ -157,11 +157,11 @@ let rewrite_signature ~jsxVersion ~jsxModule ~jsxMode
157157
(code : Parsetree.signature) : Parsetree.signature =
158158
let config =
159159
{
160-
React_jsx_common.version = jsxVersion;
160+
Jsx_common.version = jsxVersion;
161161
module_ = jsxModule;
162162
mode = jsxMode;
163163
nestedModules = [];
164-
hasReactComponent = false;
164+
hasComponent = false;
165165
}
166166
in
167167
let mapper = getMapper ~config in
File renamed without changes.

0 commit comments

Comments
 (0)