Skip to content

Commit f389e42

Browse files
authored
React JSX PPX: generalize calling expressions to all identifiers (rescript-lang#67)
1 parent 76cb6d6 commit f389e42

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

jscomp/napkin/reactjs_jsx_ppx_v3.ml

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ let makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList =
275275
let jsxMapper () =
276276
let jsxVersion = ref None in
277277

278-
let transformUppercaseCall3 modulePath mapper loc attrs _ callArguments =
278+
let transformUppercaseCall3 ~caller modulePath mapper loc attrs _ callArguments =
279279
let children, argsWithLabels = extractChildren ~loc ~removeLastPositionUnit:true callArguments in
280280
let argsForMake = argsWithLabels in
281281
let childrenExpr = transformChildrenIfListUpper ~loc ~mapper children in
@@ -302,8 +302,8 @@ let jsxMapper () =
302302
in
303303
let ident =
304304
match modulePath with
305-
| Lident _ -> Ldot (modulePath, "make")
306-
| Ldot (_modulePath, value) as fullPath when isCap value -> Ldot (fullPath, "make")
305+
| Lident _ -> Ldot (modulePath, caller)
306+
| Ldot (_modulePath, value) as fullPath when isCap value -> Ldot (fullPath, caller)
307307
| modulePath -> modulePath
308308
in
309309
let propsIdent =
@@ -834,7 +834,7 @@ let jsxMapper () =
834834
(* Foo.createElement(~prop1=foo, ~prop2=bar, ~children=[], ()) *)
835835
| { loc; txt = Ldot (modulePath, ("createElement" | "make")) } -> (
836836
match !jsxVersion with
837-
| None | Some 3 -> transformUppercaseCall3 modulePath mapper loc attrs callExpression callArguments
837+
| None | Some 3 -> transformUppercaseCall3 ~caller:"make" modulePath mapper loc attrs callExpression callArguments
838838
| Some _ -> raise (Invalid_argument "JSX: the JSX version must be 3") )
839839
(* div(~prop1=foo, ~prop2=bar, ~children=[bla], ()) *)
840840
(* turn that into
@@ -843,11 +843,13 @@ let jsxMapper () =
843843
match !jsxVersion with
844844
| None | Some 3 -> transformLowercaseCall3 mapper loc attrs callArguments id
845845
| Some _ -> raise (Invalid_argument "JSX: the JSX version must be 3") )
846-
| { txt = Ldot (_, anythingNotCreateElementOrMake) } ->
847-
raise
848-
(Invalid_argument
849-
( "JSX: the JSX attribute should be attached to a `YourModuleName.createElement` or \
850-
`YourModuleName.make` call. We saw `" ^ anythingNotCreateElementOrMake ^ "` instead" ))
846+
(* Foo.bar(~prop1=foo, ~prop2=bar, ~children=[], ()) *)
847+
(* Not only "createElement" or "make". See
848+
https://github.com/reasonml/reason/pull/2541 *)
849+
| { loc; txt = Ldot (modulePath, anythingNotCreateElementOrMake) } -> (
850+
match !jsxVersion with
851+
| None | Some 3 -> transformUppercaseCall3 ~caller:anythingNotCreateElementOrMake modulePath mapper loc attrs callExpression callArguments
852+
| Some _ -> raise (Invalid_argument "JSX: the JSX version must be 3") )
851853
| { txt = Lapply _ } ->
852854
(* don't think there's ever a case where this is reached *)
853855
raise (Invalid_argument "JSX: encountered a weird case while processing the code. Please report this!") )

jscomp/test/dune.gen

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6746,6 +6746,19 @@
67466746
(run bsc -bs-cmi -bs-cmj -bs-no-version-header -bs-cross-module-opt -make-runtime-test -bs-package-output commonjs:jscomp/test -w -3-6-26-27-29-30-32..40-44-45-60-67-68-106+104 -warn-error A -I ../runtime -I ../stdlib-412/stdlib_modules -nopervasives -open Stdlib__no_aliases -I ../others -I . %{inputs})))
67476747

67486748

6749+
(rule
6750+
(targets reactjs_ppx_custom.cmi reactjs_ppx_custom.cmj reactjs_ppx_custom.js)
6751+
(deps (:inputs reactjs_ppx_custom.re) ../stdlib-412/stdlib.cmj)
6752+
6753+
(mode
6754+
(promote
6755+
(until-clean)
6756+
(only reactjs_ppx_custom.js)))
6757+
6758+
(action
6759+
(run bsc -bs-cmi -bs-cmj -bs-no-version-header -bs-cross-module-opt -make-runtime-test -bs-package-output commonjs:jscomp/test -w -3-6-26-27-29-30-32..40-44-45-60-67-68-106+104 -warn-error A -I ../runtime -I ../stdlib-412/stdlib_modules -nopervasives -open Stdlib__no_aliases -I ../others -I . %{inputs})))
6760+
6761+
67496762
(rule
67506763
(targets reasonReact.cmj reasonReact.js)
67516764
(deps (:inputs reasonReact.re) ../stdlib-412/stdlib.cmj react.cmj reasonReact.cmi reasonReactOptimizedCreateClass.cmj reasonReactRouter.cmj)

jscomp/test/reactjs_ppx_custom.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
var React = require("react");
4+
5+
function Reactjs_ppx_custom$Internal$header(Props) {
6+
throw {
7+
RE_EXN_ID: "Assert_failure",
8+
_1: [
9+
"reactjs_ppx_custom.re",
10+
5,
11+
21
12+
],
13+
Error: new Error()
14+
};
15+
}
16+
17+
var Internal = {
18+
header: Reactjs_ppx_custom$Internal$header
19+
};
20+
21+
function Reactjs_ppx_custom(Props) {
22+
return React.createElement(Reactjs_ppx_custom$Internal$header, {});
23+
}
24+
25+
var make = Reactjs_ppx_custom;
26+
27+
exports.Internal = Internal;
28+
exports.make = make;
29+
/* react Not a pure module */

jscomp/test/reactjs_ppx_custom.re

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[@bs.config {flags: [|"-bs-jsx", "3"|]}];
2+
3+
module Internal = {
4+
[@react.component]
5+
let header = () => assert(false);
6+
};
7+
8+
[@react.component]
9+
let make = () => <Internal.header />;

0 commit comments

Comments
 (0)