Skip to content

Commit

Permalink
Fix nested object get and set not working in res files (#533)
Browse files Browse the repository at this point in the history
* test: add repro for 489

* test: fix dune file for gh489

* ppx_rescript_compat: process nested pexp_send

* test: add rescript-syntax to gh489

* add applies_to

* disable rescript-syntax tests

* add rescript-syntax test to melange package

* Revert "add rescript-syntax test to melange package"

This reverts commit e2073f8.

* fix rescript-syntax tests

* cleanup

* more cleanup

* add one more level to rescript test
  • Loading branch information
jchavarri authored May 5, 2023
1 parent c9734fe commit dfe9990
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dev:

.PHONY: test
test:
opam exec -- dune runtest -p melange,reactjs-jsx-ppx
opam exec -- dune runtest -p melange,reactjs-jsx-ppx,rescript-syntax

.PHONY: opam-create-switch
opam-create-switch: ## Create opam switch
Expand Down
3 changes: 2 additions & 1 deletion rescript-syntax/ppx_rescript_compat/ppx_rescript_compat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ let expr_mapper (self : mapper) (expr : Parsetree.expression) =
args;
}
| Pexp_send
( ({ pexp_desc = Pexp_apply _ | Pexp_ident _; pexp_loc; _ } as subexpr),
( ({ pexp_desc = Pexp_apply _ | Pexp_ident _ | Pexp_send _; pexp_loc; _ } as subexpr),
arg ) ->
(* ReScript removed the OCaml object system and abuses `Pexp_send` for
`obj##property`. Here, we make that conversion. *)
let subexpr = self.expr self subexpr in
{
expr with
pexp_desc =
Expand Down
3 changes: 3 additions & 0 deletions test/rescript-syntax/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(cram
(package rescript-syntax)
(deps %{bin:melc} %{bin:rescript_syntax}))
42 changes: 42 additions & 0 deletions test/rescript-syntax/gh489.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Repro for GitHub issue 489, first try setting values in nested objects with OCaml syntax

$ cat > foo.ml <<EOF
> type person = < age :int [@bs.set ] > Js.t
> type entry = < person :person [@bs.set ] > Js.t
> external entry : entry = "entry"[@@bs.val ]
> let () = ((entry ## person) ## age) #= 99
> EOF
$ melc foo.ml
// Generated by Melange
'use strict';
entry.person.age = 99;
/* Not a pure module */
Now let's try with ReScript syntax
$ cat > foo.res <<EOF
> type person = {@set "age": int}
> type entry = {@set "person": person}
> type deep = {@set "deep": entry}
> @val external deep: deep = "deep"
> deep["deep"]["person"]["age"] = 99
$ rescript_syntax -print=ml foo.res
type nonrec person = < age: int [@set ] > Js.t
type nonrec entry = < person: person [@set ] > Js.t
type nonrec deep = < deep: entry [@set ] > Js.t
external deep : deep = "deep"[@@val ]
;;(((deep ## deep) ## person) ## age) #= 99
$ rescript_syntax -print=ml foo.res > foo.ml
$ melc foo.ml
// Generated by Melange
'use strict';
deep.deep.person.age = 99;
/* Not a pure module */

0 comments on commit dfe9990

Please sign in to comment.