Skip to content

Commit 1c10e4e

Browse files
committed
parse jsx config to variants
1 parent 9cb6590 commit 1c10e4e

11 files changed

+501
-102
lines changed

jscomp/common/js_config.ml

+35-5
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@
2525

2626
(** Browser is not set via command line only for internal use *)
2727

28+
type jsx_version = Jsx_v3 | Jsx_v4 | NotSelected
29+
type jsx_module = React
30+
type jsx_mode = Classic | Automatic
31+
2832
let no_version_header = ref false
2933
let cross_module_inline = ref false
3034
let diagnose = ref false
3135

3236
let get_diagnose () =
3337
!diagnose
3438

35-
# 34 "common/js_config.pp.ml"
39+
# 38 "common/js_config.pp.ml"
3640
|| Sys.getenv_opt "RES_DEBUG_FILE" <> None
3741

38-
# 37 "common/js_config.pp.ml"
42+
# 41 "common/js_config.pp.ml"
3943
(* let (//) = Filename.concat *)
4044

4145
(* let get_packages_info () = !packages_info *)
@@ -51,15 +55,41 @@ let cmi_only = ref false
5155
let cmj_only = ref false
5256
let force_cmi = ref false
5357
let force_cmj = ref false
54-
let jsx_version = ref (-1)
55-
let jsx_module = ref "react"
56-
let jsx_mode = ref "classic"
58+
let jsx_version = ref NotSelected
59+
let jsx_module = ref React
60+
let jsx_mode = ref Classic
5761
let js_stdout = ref true
5862
let all_module_aliases = ref false
5963
let no_stdlib = ref false
6064
let no_export = ref false
6165
let as_ppx = ref false
6266

67+
let int_of_jsx_version = function
68+
| Jsx_v3 -> 3
69+
| Jsx_v4 -> 4
70+
| NotSelected -> -1
71+
72+
let string_of_jsx_module = function
73+
| React -> "react"
74+
75+
let string_of_jsx_mode = function
76+
| Classic -> "classic"
77+
| Automatic -> "automatic"
78+
79+
let jsx_version_of_int = function
80+
| 3 -> Jsx_v3
81+
| 4 -> Jsx_v4
82+
| _ -> NotSelected
83+
84+
let jsx_module_of_string = function
85+
| "react" -> React
86+
| _ -> React
87+
88+
let jsx_mode_of_string = function
89+
| "classic" -> Classic
90+
| "automatic" -> Automatic
91+
| _ -> Classic
92+
6393
(* option to config `@rescript/std`*)
6494
let customize_runtime : string option ref = ref None
6595
let as_pp = ref false

jscomp/common/js_config.mli

+19-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
type jsx_version = Jsx_v3 | Jsx_v4 | NotSelected
26+
type jsx_module = React
27+
type jsx_mode = Classic | Automatic
28+
2529
(* val get_packages_info :
2630
unit -> Js_packages_info.t *)
2731

@@ -73,11 +77,11 @@ val force_cmi : bool ref
7377

7478
val force_cmj : bool ref
7579

76-
val jsx_version : int ref
80+
val jsx_version : jsx_version ref
7781

78-
val jsx_module: string ref
82+
val jsx_module: jsx_module ref
7983

80-
val jsx_mode: string ref
84+
val jsx_mode: jsx_mode ref
8185

8286
val js_stdout : bool ref
8387

@@ -89,6 +93,18 @@ val no_export : bool ref
8993

9094
val as_ppx : bool ref
9195

96+
val int_of_jsx_version : jsx_version -> int
97+
98+
val string_of_jsx_module : jsx_module -> string
99+
100+
val string_of_jsx_mode : jsx_mode -> string
101+
102+
val jsx_version_of_int : int -> jsx_version
103+
104+
val jsx_module_of_string : string -> jsx_module
105+
106+
val jsx_mode_of_string : string -> jsx_mode
107+
92108
val customize_runtime : string option ref
93109

94110
val as_pp : bool ref

jscomp/common/js_config.pp.ml

+33-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424

2525
(** Browser is not set via command line only for internal use *)
2626

27+
type jsx_version = Jsx_v3 | Jsx_v4 | NotSelected
28+
type jsx_module = React
29+
type jsx_mode = Classic | Automatic
30+
2731
let no_version_header = ref false
2832
let cross_module_inline = ref false
2933
let diagnose = ref false
@@ -49,15 +53,41 @@ let cmi_only = ref false
4953
let cmj_only = ref false
5054
let force_cmi = ref false
5155
let force_cmj = ref false
52-
let jsx_version = ref (-1)
53-
let jsx_module = ref "react"
54-
let jsx_mode = ref "classic"
56+
let jsx_version = ref NotSelected
57+
let jsx_module = ref React
58+
let jsx_mode = ref Classic
5559
let js_stdout = ref true
5660
let all_module_aliases = ref false
5761
let no_stdlib = ref false
5862
let no_export = ref false
5963
let as_ppx = ref false
6064

65+
let int_of_jsx_version = function
66+
| Jsx_v3 -> 3
67+
| Jsx_v4 -> 4
68+
| NotSelected -> -1
69+
70+
let string_of_jsx_module = function
71+
| React -> "react"
72+
73+
let string_of_jsx_mode = function
74+
| Classic -> "classic"
75+
| Automatic -> "automatic"
76+
77+
let jsx_version_of_int = function
78+
| 3 -> Jsx_v3
79+
| 4 -> Jsx_v4
80+
| _ -> NotSelected
81+
82+
let jsx_module_of_string = function
83+
| "react" -> React
84+
| _ -> React
85+
86+
let jsx_mode_of_string = function
87+
| "classic" -> Classic
88+
| "automatic" -> Automatic
89+
| _ -> Classic
90+
6191
(* option to config `@rescript/std`*)
6292
let customize_runtime : string option ref = ref None
6393
let as_pp = ref false

jscomp/frontend/ppx_driver.ml

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ let main impl intf =
3333
(Array.sub Sys.argv 0 (n - 2))
3434
[
3535
( "-bs-jsx",
36-
Arg.Int (fun i -> Js_config.jsx_version := i),
36+
Arg.Int
37+
(fun i -> Js_config.jsx_version := Js_config.jsx_version_of_int i),
3738
" Set jsx version" );
3839
( "-bs-jsx-module",
39-
Arg.String (fun i -> Js_config.jsx_module := i),
40+
Arg.String
41+
(fun i ->
42+
Js_config.jsx_module := Js_config.jsx_module_of_string i),
4043
" Set jsx module" );
4144
( "-bs-jsx-mode",
42-
Arg.String (fun i -> Js_config.jsx_mode := i),
45+
Arg.String
46+
(fun i -> Js_config.jsx_mode := Js_config.jsx_mode_of_string i),
4347
" Set jsx mode" );
4448
]
4549
ignore usage;

jscomp/frontend/ppx_entry.ml

+17-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ let rewrite_signature (ast : Parsetree.signature) : Parsetree.signature =
2828
Bs_ast_invariant.iter_warnings_on_sigi ast;
2929
Ast_config.iter_on_bs_config_sigi ast;
3030
let ast =
31-
Reactjs_jsx_ppx.rewrite_signature ~jsxVersion:!Js_config.jsx_version
32-
~jsxModule:!Js_config.jsx_module ~jsxMode:!Js_config.jsx_mode ast
31+
match !Js_config.jsx_version with
32+
| NotSelected -> ast
33+
| _ ->
34+
let open Js_config in
35+
let jsxVersion = int_of_jsx_version !jsx_version in
36+
let jsxModule = string_of_jsx_module !jsx_module in
37+
let jsxMode = string_of_jsx_mode !jsx_mode in
38+
Reactjs_jsx_ppx.rewrite_signature ~jsxVersion ~jsxModule ~jsxMode ast
3339
in
3440
if !Js_config.no_builtin_ppx then ast
3541
else
@@ -42,8 +48,15 @@ let rewrite_implementation (ast : Parsetree.structure) : Parsetree.structure =
4248
Bs_ast_invariant.iter_warnings_on_stru ast;
4349
Ast_config.iter_on_bs_config_stru ast;
4450
let ast =
45-
Reactjs_jsx_ppx.rewrite_implementation ~jsxVersion:!Js_config.jsx_version
46-
~jsxModule:!Js_config.jsx_module ~jsxMode:!Js_config.jsx_mode ast
51+
match !Js_config.jsx_version with
52+
| NotSelected -> ast
53+
| _ ->
54+
let open Js_config in
55+
let jsxVersion = int_of_jsx_version !jsx_version in
56+
let jsxModule = string_of_jsx_module !jsx_module in
57+
let jsxMode = string_of_jsx_mode !jsx_mode in
58+
Reactjs_jsx_ppx.rewrite_implementation ~jsxVersion ~jsxModule ~jsxMode
59+
ast
4760
in
4861
if !Js_config.no_builtin_ppx then ast
4962
else

jscomp/main/jsoo_main.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ let implementation ~use_super_errors impl str : Js.Unsafe.obj =
5959
Lazy.force Super_main.setup);
6060

6161
try
62-
Js_config.jsx_version := 3;
62+
Js_config.jsx_version := Js_config.Jsx_v3;
6363
(* default *)
6464
let ast = impl (Lexing.from_string str) in
6565
let ast = Ppx_entry.rewrite_implementation ast in

jscomp/main/jsoo_playground_main.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ module Compile = struct
474474
let env = Res_compmisc.initial_env () in (* Question ?? *)
475475
(* let finalenv = ref Env.empty in *)
476476
let types_signature = ref [] in
477-
Js_config.jsx_version := 3; (* default *)
477+
Js_config.jsx_version := Js_config.Jsx_v3; (* default *)
478478
let ast = impl (str) in
479479
let ast = Ppx_entry.rewrite_implementation ast in
480480
let typed_tree =

jscomp/main/rescript_compiler_main.ml

+7-4
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,21 @@ let buckle_script_flags : (string * Bsc_args.spec * string) array =
275275
"*internal* <module> Opens the module <module> before typing";
276276

277277
"-bs-jsx", string_call (fun i ->
278-
(if i <> "3" && i <> "4" then Bsc_args.bad_arg (" Not supported jsx version : " ^ i));
279-
Js_config.jsx_version := int_of_string i),
278+
(if i <> "3" && i <> "4" then Bsc_args.bad_arg (" Not supported jsx version : " ^ i));
279+
let open Js_config in
280+
jsx_version := jsx_version_of_int @@ int_of_string i),
280281
"*internal* Set jsx version";
281282

282283
"-bs-jsx-module", string_call (fun i ->
283284
(if i <> "react" then Bsc_args.bad_arg (" Not supported jsx-module : " ^ i));
284-
Js_config.jsx_module := i),
285+
let open Js_config in
286+
Js_config.jsx_module := jsx_module_of_string i),
285287
"*internal* Set jsx module";
286288

287289
"-bs-jsx-mode", string_call (fun i ->
288290
(if i <> "classic" && i <> "automatic" then Bsc_args.bad_arg (" Not supported jsx-mode : " ^ i));
289-
Js_config.jsx_mode := i),
291+
let open Js_config in
292+
Js_config.jsx_mode := jsx_mode_of_string i),
290293
"*internal* Set jsx mode";
291294

292295
"-bs-package-output", string_call Js_packages_state.update_npm_package_path,

0 commit comments

Comments
 (0)