Skip to content

Commit 32ad5cb

Browse files
committed
Fix issue with as module(...)
Functions of the form `(~comp as module(Comp: Comp))=>` are represented internally as a `Pexp_constraint`, which makes them look like `~comp : t`. Now special-case this pattern so it's treated just like the other forms of `as ...`. Fixes #5976
1 parent f5715db commit 32ad5cb

9 files changed

+64
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ These are only breaking changes for unformatted code.
7676
- Fix issue with JSX V4 and newtype https://github.com/rescript-lang/rescript-compiler/pull/6029
7777
- Fix issue with JSX V4 when components are nested https://github.com/rescript-lang/rescript-compiler/pull/6031
7878
- Improve code generated for default arguments in JSX V4 https://github.com/rescript-lang/rescript-compiler/pull/6041
79+
- Fix issue with JSX V4 props of the form `~p as module(...)` https://github.com/rescript-lang/rescript-compiler/pull/6041
7980

8081
#### :nail_care: Polish
8182

jscomp/test/alias_default_value_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,18 @@ var C4 = {
5959
make: Alias_default_value_test$C4
6060
};
6161

62+
function Alias_default_value_test$C6(props) {
63+
return props.comp.xx;
64+
}
65+
66+
var C6 = {
67+
make: Alias_default_value_test$C6
68+
};
69+
6270
exports.C0 = C0;
6371
exports.C1 = C1;
6472
exports.C2 = C2;
6573
exports.C3 = C3;
6674
exports.C4 = C4;
75+
exports.C6 = C6;
6776
/* No side effect */

jscomp/test/alias_default_value_test.res

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,15 @@ module C3 = {
3131
module C4 = {
3232
@react.component
3333
let make = (~a as b, ~x=true) => b
34-
}
34+
}
35+
36+
module C6 = {
37+
module type Comp = {
38+
let xx : int
39+
@react.component
40+
let make: unit => React.element
41+
}
42+
43+
@react.component
44+
let make = (~comp as module(Comp: Comp), ~x as (a, b)) => Comp.xx
45+
}

res_syntax/src/reactjs_jsx_v4.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ let rec recursivelyTransformNamedArgsForMake expr args newtypes coreType =
640640
in
641641
let type_ =
642642
match pattern with
643+
| {ppat_desc = Ppat_constraint (_, {ptyp_desc = Ptyp_package _})} -> None
643644
| {ppat_desc = Ppat_constraint (_, type_)} -> Some type_
644645
| _ -> None
645646
in
@@ -965,11 +966,10 @@ let mapBinding ~config ~emptyLoc ~pstr_loc ~fileName ~recFlag binding =
965966
in
966967
let rec stripConstraintUnpack ~label pattern =
967968
match pattern with
969+
| {ppat_desc = Ppat_constraint (_, {ptyp_desc = Ptyp_package _})} ->
970+
pattern
968971
| {ppat_desc = Ppat_constraint (pattern, _)} ->
969972
stripConstraintUnpack ~label pattern
970-
| {ppat_desc = Ppat_unpack _; ppat_loc} ->
971-
(* remove unpack e.g. model: module(T) *)
972-
Pat.var ~loc:ppat_loc {txt = label; loc = ppat_loc}
973973
| _ -> pattern
974974
in
975975
let rec returnedExpression patternsWithLabel patternsWithNolabel

res_syntax/tests/ppx/react/aliasProps.res

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ module C4 = {
2929

3030
module C5 = {
3131
@react.component
32-
let make = (~a as (x,y), ~z=3) => x+y+z
32+
let make = (~a as (x, y), ~z=3) => x + y + z
3333
}
3434

35+
module C6 = {
36+
module type Comp = {
37+
@react.component
38+
let make: unit => React.element
39+
}
3540

41+
@react.component
42+
let make = (~comp as module(Comp: Comp), ~x as (a, b)) => <Comp />
43+
}

res_syntax/tests/ppx/react/expected/aliasProps.res.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,19 @@ module C5 = {
113113
\"AliasProps$C5"
114114
}
115115
}
116+
117+
module C6 = {
118+
module type Comp = {
119+
type props = {}
120+
121+
let make: React.componentLike<props, React.element>
122+
}
123+
type props<'comp, 'x> = {comp: 'comp, x: 'x}
124+
125+
let make = ({comp: module(Comp: Comp), x: (a, b), _}: props<_, _>) => React.jsx(Comp.make, {})
126+
let make = {
127+
let \"AliasProps$C6" = (props: props<_>) => make(props)
128+
129+
\"AliasProps$C6"
130+
}
131+
}

res_syntax/tests/ppx/react/expected/firstClassModules.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ module Select = {
6666

6767
let make = (
6868
type a key,
69-
{model, selected, onChange, items, _}: props<
70-
module(T with type t = a and type key = key),
69+
{model: module(T: T with type t = a and type key = key), selected, onChange, items, _}: props<
70+
_,
7171
option<key>,
7272
option<key> => unit,
7373
array<a>,

res_syntax/tests/ppx/react/expected/newtype.res.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ module type T = {
6767
module V4A2 = {
6868
type props<'foo> = {foo: 'foo}
6969

70-
let make = (type a, {foo, _}: props<module(T with type t = a)>) => {
70+
let make = (type a, {foo: (foo: module(T with type t = a)), _}: props<_>) => {
7171
module T = unpack(foo)
7272
ReactDOM.jsx("div", {})
7373
}
@@ -91,3 +91,11 @@ module V4A3 = {
9191
\"Newtype$V4A3"
9292
}
9393
}
94+
type props<'x, 'q> = {x: 'x, q: 'q}
95+
96+
let make = ({x, q, _}: props<('a, 'b), 'a>) => [fst(x), q]
97+
let make = {
98+
let \"Newtype" = (props: props<_>) => make(props)
99+
100+
\"Newtype"
101+
}

res_syntax/tests/ppx/react/newtype.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ module V4A3 = {
4343
foo
4444
}
4545
}
46+
47+
@react.component
48+
let make =(~x : ('a,'b), ~q:'a ) => [fst(x), q]

0 commit comments

Comments
 (0)