Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit a69d3c4

Browse files
committed
use React for new jsx transform, modify cli arg
- use rescript-react for the new jsx transform - change cli arg, react-runtime -> jsx-mode - add cli arg, jsx-module
1 parent 41d02e0 commit a69d3c4

10 files changed

+44
-35
lines changed

cli/reactjs_jsx_ppx_v4.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ let transformUppercaseCall3 jsxRuntime modulePath mapper loc attrs
351351
( labelled "children",
352352
Exp.apply
353353
(Exp.ident
354-
{txt = Ldot (Lident "Jsx", "array"); loc = Location.none})
354+
{txt = Ldot (Lident "React", "array"); loc = Location.none})
355355
[(Nolabel, expression)] );
356356
]
357357
| _ ->
@@ -395,14 +395,15 @@ let transformUppercaseCall3 jsxRuntime modulePath mapper loc attrs
395395
let jsxExpr, key =
396396
match (!childrenArg, keyProp) with
397397
| None, (_, keyExpr) :: _ ->
398-
( Exp.ident ~loc {loc; txt = Ldot (Lident "Jsx", "jsxKeyed")},
398+
( Exp.ident ~loc {loc; txt = Ldot (Lident "React", "jsxKeyed")},
399399
[(nolabel, keyExpr)] )
400-
| None, [] -> (Exp.ident ~loc {loc; txt = Ldot (Lident "Jsx", "jsx")}, [])
400+
| None, [] ->
401+
(Exp.ident ~loc {loc; txt = Ldot (Lident "React", "jsx")}, [])
401402
| Some _, (_, keyExpr) :: _ ->
402-
( Exp.ident ~loc {loc; txt = Ldot (Lident "Jsx", "jsxsKeyed")},
403+
( Exp.ident ~loc {loc; txt = Ldot (Lident "React", "jsxsKeyed")},
403404
[(nolabel, keyExpr)] )
404405
| Some _, [] ->
405-
(Exp.ident ~loc {loc; txt = Ldot (Lident "Jsx", "jsxs")}, [])
406+
(Exp.ident ~loc {loc; txt = Ldot (Lident "React", "jsxs")}, [])
406407
in
407408
Exp.apply ~loc ~attrs jsxExpr
408409
([(nolabel, Exp.ident ~loc {txt = ident; loc}); (nolabel, props)] @ key)
@@ -461,7 +462,7 @@ let transformLowercaseCall3 jsxRuntime mapper loc attrs callExpression
461462
( labelled "children",
462463
Exp.apply
463464
(Exp.ident
464-
{txt = Ldot (Lident "Jsx", "array"); loc = Location.none})
465+
{txt = Ldot (Lident "React", "array"); loc = Location.none})
465466
[(Nolabel, expression)] );
466467
]
467468
in
@@ -480,15 +481,15 @@ let transformLowercaseCall3 jsxRuntime mapper loc attrs callExpression
480481
let jsxExpr, key =
481482
match (!childrenArg, keyProp) with
482483
| None, (_, keyExpr) :: _ ->
483-
( Exp.ident ~loc {loc; txt = Ldot (Lident "JsxDOM", "jsxKeyed")},
484+
( Exp.ident ~loc {loc; txt = Ldot (Lident "ReactDOM", "jsxKeyed")},
484485
[(nolabel, keyExpr)] )
485486
| None, [] ->
486-
(Exp.ident ~loc {loc; txt = Ldot (Lident "JsxDOM", "jsx")}, [])
487+
(Exp.ident ~loc {loc; txt = Ldot (Lident "ReactDOM", "jsx")}, [])
487488
| Some _, (_, keyExpr) :: _ ->
488-
( Exp.ident ~loc {loc; txt = Ldot (Lident "JsxDOM", "jsxsKeyed")},
489+
( Exp.ident ~loc {loc; txt = Ldot (Lident "ReactDOM", "jsxsKeyed")},
489490
[(nolabel, keyExpr)] )
490491
| Some _, [] ->
491-
(Exp.ident ~loc {loc; txt = Ldot (Lident "JsxDOM", "jsxs")}, [])
492+
(Exp.ident ~loc {loc; txt = Ldot (Lident "ReactDOM", "jsxs")}, [])
492493
in
493494
Exp.apply ~loc ~attrs jsxExpr
494495
([(nolabel, componentNameExpr); (nolabel, props)] @ key)

cli/res_cli.ml

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ module ResClflags : sig
163163
val file : string ref
164164
val interface : bool ref
165165
val ppx : string ref
166-
val jsxRuntime : string ref
166+
val jsxModule : string ref
167+
val jsxMode : string ref
167168
val typechecker : bool ref
168169

169170
val parse : unit -> unit
@@ -175,7 +176,8 @@ end = struct
175176
let origin = ref ""
176177
let interface = ref false
177178
let ppx = ref ""
178-
let jsxRuntime = ref "automatic"
179+
let jsxModule = ref "react"
180+
let jsxMode = ref "classic"
179181
let file = ref ""
180182
let typechecker = ref false
181183

@@ -207,10 +209,12 @@ end = struct
207209
Arg.String (fun txt -> ppx := txt),
208210
"Apply a specific built-in ppx before parsing, none or jsx3, jsx4. \
209211
Default: none" );
210-
( "-jsx-runtime",
211-
Arg.String (fun txt -> jsxRuntime := txt),
212-
"Specify the jsx runtime for React, classic or automatic. Default: \
213-
automatic" );
212+
( "-jsx-module",
213+
Arg.String (fun txt -> jsxModule := txt),
214+
"Specify the jsx module. Default: react" );
215+
( "-jsx-mode",
216+
Arg.String (fun txt -> jsxMode := txt),
217+
"Specify the jsx mode, classic or automatic. Default: classic" );
214218
( "-typechecker",
215219
Arg.Unit (fun () -> typechecker := true),
216220
"Parses the ast as it would be passed to the typechecker and not the \
@@ -224,7 +228,7 @@ module CliArgProcessor = struct
224228
type backend = Parser : 'diagnostics Res_driver.parsingEngine -> backend
225229
[@@unboxed]
226230

227-
let processFile ~isInterface ~width ~recover ~origin ~target ~ppx ~jsxRuntime
231+
let processFile ~isInterface ~width ~recover ~origin ~target ~ppx ~jsxMode
228232
~typechecker filename =
229233
let len = String.length filename in
230234
let processInterface =
@@ -285,8 +289,7 @@ module CliArgProcessor = struct
285289
match ppx with
286290
| "jsx3" -> Reactjs_jsx_ppx_v3.rewrite_signature parseResult.parsetree
287291
| "jsx4" ->
288-
Reactjs_jsx_ppx_v4.rewrite_signature jsxRuntime
289-
parseResult.parsetree
292+
Reactjs_jsx_ppx_v4.rewrite_signature jsxMode parseResult.parsetree
290293
| _ -> parseResult.parsetree
291294
in
292295
printEngine.printInterface ~width ~filename
@@ -306,7 +309,7 @@ module CliArgProcessor = struct
306309
| "jsx3" ->
307310
Reactjs_jsx_ppx_v3.rewrite_implementation parseResult.parsetree
308311
| "jsx4" ->
309-
Reactjs_jsx_ppx_v4.rewrite_implementation jsxRuntime
312+
Reactjs_jsx_ppx_v4.rewrite_implementation jsxMode
310313
parseResult.parsetree
311314
| _ -> parseResult.parsetree
312315
in
@@ -321,5 +324,5 @@ let[@raises exit] () =
321324
CliArgProcessor.processFile ~isInterface:!ResClflags.interface
322325
~width:!ResClflags.width ~recover:!ResClflags.recover
323326
~target:!ResClflags.print ~origin:!ResClflags.origin ~ppx:!ResClflags.ppx
324-
~jsxRuntime:!ResClflags.jsxRuntime ~typechecker:!ResClflags.typechecker
327+
~jsxMode:!ResClflags.jsxMode ~typechecker:!ResClflags.typechecker
325328
!ResClflags.file)

scripts/test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ done <temp/files.txt
4747
# printing with ppx v4 classic
4848
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
4949
while read file; do
50-
rescript -ppx jsx4 -jsx-runtime classic $file &> $(exp2 $file "_v4_cls") & maybeWait
50+
rescript -ppx jsx4 -jsx-mode classic $file &> $(exp2 $file "_v4_cls") & maybeWait
5151
done <temp/files.txt
5252

5353
# printing with ppx v4 automatic
5454
find tests/ppx/react -name "*.res" -o -name "*.resi" >temp/files.txt
5555
while read file; do
56-
rescript -ppx jsx4 -jsx-runtime automatic $file &> $(exp2 $file "_v4_auto") & maybeWait
56+
rescript -ppx jsx4 -jsx-mode automatic $file &> $(exp2 $file "_v4_auto") & maybeWait
5757
done <temp/files.txt
5858

5959
wait

tests/ppx/react/expected/commentAtTop.res_v4_auto.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type props<'msg> = {key?: string, msg: 'msg} // test React JSX file
22

33
let make = ({msg, _}: props<'msg>) => {
4-
JsxDOM.jsx("div", {children: {msg->React.string}})
4+
ReactDOM.jsx("div", {children: {msg->React.string}})
55
}
66
let make = {
77
let \"CommentAtTop" = (props: props<_>) => make(props)

tests/ppx/react/expected/externalWithCustomName.res_v4_auto.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ module Foo = {
44
external component: React.componentLike<props<'a, 'b>, React.element> = "component"
55
}
66

7-
let t = Jsx.jsx(Foo.component, {a: 1, b: "1"})
7+
let t = React.jsx(Foo.component, {a: 1, b: "1"})

tests/ppx/react/expected/forwardRef.res_v4_auto.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ module FancyInput = {
99
let ref = Js.Nullable.fromOption(ref)
1010
let _ = ref
1111

12-
JsxDOM.jsxs(
12+
ReactDOM.jsxs(
1313
"div",
1414
{
15-
children: Jsx.array([
16-
JsxDOM.jsx(
15+
children: React.array([
16+
ReactDOM.jsx(
1717
"input",
1818
{
1919
type_: "text",
@@ -37,9 +37,14 @@ type props = {key?: string}
3737
let make = (_: props) => {
3838
let input = React.useRef(Js.Nullable.null)
3939

40-
JsxDOM.jsx(
40+
ReactDOM.jsx(
4141
"div",
42-
{children: Jsx.jsx(FancyInput.make, {ref: input, children: {React.string("Click to focus")}})},
42+
{
43+
children: React.jsx(
44+
FancyInput.make,
45+
{ref: input, children: {React.string("Click to focus")}},
46+
),
47+
},
4348
)
4449
}
4550
let make = {

tests/ppx/react/expected/innerModule.res_v4_auto.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module Bar = {
22
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
33
let make = ({a, b, _}: props<'a, 'b>) => {
44
Js.log("This function should be named `InnerModule.react$Bar`")
5-
JsxDOM.jsx("div", {key: ?None})
5+
ReactDOM.jsx("div", {key: ?None})
66
}
77
let make = {
88
let \"InnerModule$Bar" = (props: props<_>) => make(props)
@@ -12,7 +12,7 @@ module Bar = {
1212

1313
let component = ({a, b, _}: props<'a, 'b>) => {
1414
Js.log("This function should be named `InnerModule.react$Bar$component`")
15-
JsxDOM.jsx("div", {key: ?None})
15+
ReactDOM.jsx("div", {key: ?None})
1616
}
1717
let component = {
1818
let \"InnerModule$Bar$component" = (props: props<_>) => make(props)

tests/ppx/react/expected/newtype.res_v4_auto.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type props<'a, 'b, 'c> = {key?: string, a: 'a, b: 'b, c: 'c}
22
let make = (_: props<'a, 'b, 'c>, type a, ~a: a, ~b: array<option<[#Foo(a)]>>, ~c: 'a, _) =>
3-
JsxDOM.jsx("div", {key: ?None})
3+
ReactDOM.jsx("div", {key: ?None})
44
let make = {
55
let \"Newtype" = (props: props<_>) => make(props)
66
\"Newtype"

tests/ppx/react/expected/topLevel.res_v4_auto.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
22
let make = ({a, b, _}: props<'a, 'b>) => {
33
Js.log("This function should be named 'TopLevel.react'")
4-
JsxDOM.jsx("div", {key: ?None})
4+
ReactDOM.jsx("div", {key: ?None})
55
}
66
let make = {
77
let \"TopLevel" = (props: props<_>) => make(props)

tests/ppx/react/expected/typeConstraint.res_v4_auto.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
type props<'a, 'b> = {key?: string, a: 'a, b: 'b}
22
let make: 'a. (~a: 'a, ~b: 'a, 'a) => React.element = (_: props<'a, 'b>, type a): (
33
(~a: a, ~b: a, a) => React.element
4-
) => (~a, ~b, _) => JsxDOM.jsx("div", {key: ?None})
4+
) => (~a, ~b, _) => ReactDOM.jsx("div", {key: ?None})
55
let make = {
66
let \"TypeConstraint" = (props: props<_>) => make(props)
77
\"TypeConstraint"

0 commit comments

Comments
 (0)