Skip to content

Commit 8ae8337

Browse files
authored
Generic JSX transform fixes (#6606)
* DOM -> Elements. default to the correct jsx config params. handle potentially namespaced module names * more defaults * changelog
1 parent 91096aa commit 8ae8337

File tree

4 files changed

+44
-57
lines changed

4 files changed

+44
-57
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#### :bug: Bug Fix
1616

1717
- Fix issue with async and newtype in uncurried mode. https://github.com/rescript-lang/rescript-compiler/pull/6601
18+
- Generic JSX transform: Rename expected module name for lowercase JSX to `Elements` from `DOM`. https://github.com/rescript-lang/rescript-compiler/pull/6606
19+
- Generic JSX transform: Set default config params for `jsxConfig`. https://github.com/rescript-lang/rescript-compiler/pull/6606
20+
- Generic JSX transform: Handle namespaced names. https://github.com/rescript-lang/rescript-compiler/pull/6606
1821

1922
#### :house: Internal
2023

jscomp/bsc/rescript_compiler_main.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,14 @@ 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-
Js_config.jsx_module := Js_config.jsx_module_of_string i),
254+
let isGeneric = match i |> String.lowercase_ascii with
255+
| "react" -> false
256+
| _ -> true in
257+
Js_config.jsx_module := Js_config.jsx_module_of_string i;
258+
if isGeneric then (
259+
Js_config.jsx_mode := Automatic;
260+
Js_config.jsx_version := Some Jsx_v4
261+
)),
255262
"*internal* Set jsx module";
256263

257264
"-bs-jsx-mode", string_call (fun i ->

jscomp/syntax/src/jsx_ppx.ml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,23 @@ let getString ~key fields = fields |> getJsxConfigByKey ~key ~type_:String
4848

4949
let updateConfig config payload =
5050
let fields = getPayloadFields payload in
51-
(match getInt ~key:"version" fields with
52-
| None -> ()
53-
| Some i -> config.Jsx_common.version <- i);
54-
(match getString ~key:"module_" fields with
51+
let moduleRaw = getString ~key:"module_" fields in
52+
let isGeneric =
53+
match moduleRaw |> Option.map (fun m -> String.lowercase_ascii m) with
54+
| Some "react" | None -> false
55+
| Some _ -> true
56+
in
57+
(match (isGeneric, getInt ~key:"version" fields) with
58+
| true, _ -> config.Jsx_common.version <- 4
59+
| false, Some i -> config.Jsx_common.version <- i
60+
| _ -> ());
61+
(match moduleRaw with
5562
| None -> ()
5663
| Some s -> config.module_ <- s);
57-
match getString ~key:"mode" fields with
58-
| None -> ()
59-
| Some s -> config.mode <- s
64+
match (isGeneric, getString ~key:"mode" fields) with
65+
| true, _ -> config.mode <- "automatic"
66+
| false, Some s -> config.mode <- s
67+
| _ -> ()
6068

6169
let isJsxConfigAttr ((loc, _) : attribute) = loc.txt = "jsxConfig"
6270

jscomp/syntax/src/jsx_v4.ml

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ open Asttypes
44
open Parsetree
55
open Longident
66

7-
let moduleAccessName config = String.capitalize_ascii config.Jsx_common.module_
7+
let moduleAccessName config value =
8+
String.capitalize_ascii config.Jsx_common.module_ ^ "." ^ value
9+
|> Longident.parse
810

911
let nolabel = Nolabel
1012

@@ -384,10 +386,7 @@ let transformUppercaseCall3 ~config modulePath mapper jsxExprLoc callExprLoc
384386
( labelled "children",
385387
Exp.apply
386388
(Exp.ident
387-
{
388-
txt = Ldot (Lident (moduleAccessName config), "array");
389-
loc = Location.none;
390-
})
389+
{txt = moduleAccessName config "array"; loc = Location.none})
391390
[(Nolabel, expression)] );
392391
]
393392
| _ ->
@@ -431,31 +430,17 @@ let transformUppercaseCall3 ~config modulePath mapper jsxExprLoc callExprLoc
431430
match (!childrenArg, keyProp) with
432431
| None, key :: _ ->
433432
( Exp.ident
434-
{
435-
loc = Location.none;
436-
txt = Ldot (Lident (moduleAccessName config), "jsxKeyed");
437-
},
433+
{loc = Location.none; txt = moduleAccessName config "jsxKeyed"},
438434
[key; (nolabel, unitExpr ~loc:Location.none)] )
439435
| None, [] ->
440-
( Exp.ident
441-
{
442-
loc = Location.none;
443-
txt = Ldot (Lident (moduleAccessName config), "jsx");
444-
},
436+
( Exp.ident {loc = Location.none; txt = moduleAccessName config "jsx"},
445437
[] )
446438
| Some _, key :: _ ->
447439
( Exp.ident
448-
{
449-
loc = Location.none;
450-
txt = Ldot (Lident (moduleAccessName config), "jsxsKeyed");
451-
},
440+
{loc = Location.none; txt = moduleAccessName config "jsxsKeyed"},
452441
[key; (nolabel, unitExpr ~loc:Location.none)] )
453442
| Some _, [] ->
454-
( Exp.ident
455-
{
456-
loc = Location.none;
457-
txt = Ldot (Lident (moduleAccessName config), "jsxs");
458-
},
443+
( Exp.ident {loc = Location.none; txt = moduleAccessName config "jsxs"},
459444
[] )
460445
in
461446
Exp.apply ~loc:jsxExprLoc ~attrs jsxExpr
@@ -500,9 +485,9 @@ let transformLowercaseCall3 ~config mapper jsxExprLoc callExprLoc attrs
500485
(* the new jsx transform *)
501486
| "automatic" ->
502487
let elementBinding =
503-
match moduleAccessName config with
504-
| "React" -> Lident "ReactDOM"
505-
| generic -> Ldot (Lident generic, "DOM")
488+
match config.module_ |> String.lowercase_ascii with
489+
| "react" -> Lident "ReactDOM"
490+
| _generic -> moduleAccessName config "Elements"
506491
in
507492

508493
let children, nonChildrenProps =
@@ -539,10 +524,7 @@ let transformLowercaseCall3 ~config mapper jsxExprLoc callExprLoc attrs
539524
( labelled "children",
540525
Exp.apply
541526
(Exp.ident
542-
{
543-
txt = Ldot (Lident (moduleAccessName config), "array");
544-
loc = Location.none;
545-
})
527+
{txt = moduleAccessName config "array"; loc = Location.none})
546528
[(Nolabel, expression)] );
547529
]
548530
in
@@ -1203,10 +1185,7 @@ let transformStructureItem ~config item =
12031185
(* can't be an arrow because it will defensively uncurry *)
12041186
let newExternalType =
12051187
Ptyp_constr
1206-
( {
1207-
loc = pstr_loc;
1208-
txt = Ldot (Lident (moduleAccessName config), "componentLike");
1209-
},
1188+
( {loc = pstr_loc; txt = moduleAccessName config "componentLike"},
12101189
[retPropsType; innerType] )
12111190
in
12121191
let newStructure =
@@ -1321,10 +1300,7 @@ let transformSignatureItem ~config item =
13211300
(* can't be an arrow because it will defensively uncurry *)
13221301
let newExternalType =
13231302
Ptyp_constr
1324-
( {
1325-
loc = psig_loc;
1326-
txt = Ldot (Lident (moduleAccessName config), "componentLike");
1327-
},
1303+
( {loc = psig_loc; txt = moduleAccessName config "componentLike"},
13281304
[retPropsType; innerType] )
13291305
in
13301306
let newStructure =
@@ -1419,8 +1395,7 @@ let expr ~config mapper expression =
14191395
let fragment =
14201396
match config.mode with
14211397
| "automatic" ->
1422-
Exp.ident ~loc
1423-
{loc; txt = Ldot (Lident (moduleAccessName config), "jsxFragment")}
1398+
Exp.ident ~loc {loc; txt = moduleAccessName config "jsxFragment"}
14241399
| "classic" | _ ->
14251400
Exp.ident ~loc {loc; txt = Ldot (Lident "React", "fragment")}
14261401
in
@@ -1431,10 +1406,7 @@ let expr ~config mapper expression =
14311406
let applyJsxArray expr =
14321407
Exp.apply
14331408
(Exp.ident
1434-
{
1435-
txt = Ldot (Lident (moduleAccessName config), "array");
1436-
loc = Location.none;
1437-
})
1409+
{txt = moduleAccessName config "array"; loc = Location.none})
14381410
[(Nolabel, expr)]
14391411
in
14401412
let countOfChildren = function
@@ -1472,11 +1444,8 @@ let expr ~config mapper expression =
14721444
(match config.mode with
14731445
| "automatic" ->
14741446
if countOfChildren childrenExpr > 1 then
1475-
Exp.ident ~loc
1476-
{loc; txt = Ldot (Lident (moduleAccessName config), "jsxs")}
1477-
else
1478-
Exp.ident ~loc
1479-
{loc; txt = Ldot (Lident (moduleAccessName config), "jsx")}
1447+
Exp.ident ~loc {loc; txt = moduleAccessName config "jsxs"}
1448+
else Exp.ident ~loc {loc; txt = moduleAccessName config "jsx"}
14801449
| "classic" | _ ->
14811450
if countOfChildren childrenExpr > 1 then
14821451
Exp.ident ~loc

0 commit comments

Comments
 (0)