Skip to content

Commit 87374f1

Browse files
authored
Fix returning object in nested Some (rescript-lang#7013)
* add broken test * fix nested optional cases
1 parent d96bafc commit 87374f1

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

jscomp/core/js_dump.ml

+5-3
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ let raw_snippet_exp_simple_enough (s : string) =
160160

161161
(* e = function(x){...}(x); is good
162162
*)
163-
let exp_need_paren (e : J.expression) =
163+
let rec exp_need_paren ?(arrow=false) (e : J.expression) =
164164
match e.expression_desc with
165165
(* | Caml_uninitialized_obj _ *)
166166
| Call ({ expression_desc = Raw_js_code _ }, _, _) -> true
@@ -178,11 +178,13 @@ let exp_need_paren (e : J.expression) =
178178
| Length _ | Call _ | Caml_block_tag _ | Seq _ | Static_index _ | Cond _
179179
| Bin _ | Is_null_or_undefined _ | String_index _ | Array_index _
180180
| String_append _ | Var _ | Undefined _ | Null | Str _ | Array _
181-
| Optional_block _ | Caml_block _ | FlatCall _ | Typeof _ | Number _
181+
| Caml_block _ | FlatCall _ | Typeof _ | Number _
182182
| Js_not _ | Bool _ | New _ ->
183183
false
184184
| Await _ -> false
185185
| Tagged_template _ -> false
186+
| Optional_block (e, true) when arrow -> exp_need_paren ~arrow e
187+
| Optional_block _ -> false
186188

187189
(** Print as underscore for unused vars, may not be
188190
needed in the future *)
@@ -411,7 +413,7 @@ and pp_function ~return_unit ~async ~is_method ?directive cxt (f : P.t) ~fn_stat
411413

412414
| [ { statement_desc = Return e } ] | [ { statement_desc = Exp e } ]
413415
when arrow && directive == None
414-
-> (if exp_need_paren e then P.paren_group f 0 else P.group f 0)
416+
-> (if exp_need_paren ~arrow e then P.paren_group f 0 else P.group f 0)
415417
(fun _ -> ignore (expression ~level:0 cxt f e))
416418

417419
| _ ->

jscomp/test/build.ninja

+2-1
Large diffs are not rendered by default.

jscomp/test/gpr_7012_test.js

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/gpr_7012_test.res

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type t = {a: int, b: int}
2+
3+
let f1 = () => () => Some({a: 1, b: 2})
4+
5+
let f2 = () => Some(Some(Some({a: 1, b: 2})))

0 commit comments

Comments
 (0)