Skip to content

Commit f3917c9

Browse files
authored
improve variadic call using spread syntax (#7030)
1 parent c15df0c commit f3917c9

28 files changed

+71
-309
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#### :nail_care: Polish
2020

2121
- Improve bigint literal comparison. https://github.com/rescript-lang/rescript-compiler/pull/7029
22+
- Improve output of `@variadic` bindings. https://github.com/rescript-lang/rescript-compiler/pull/7030
2223

2324
# 12.0.0-alpha.3
2425

jscomp/core/j.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ and expression_desc =
163163
| Undefined of {is_unit: bool}
164164
| Null
165165
| Await of expression
166+
| Spread of expression
166167

167168
and for_ident_expression = expression
168169
(* pure*)

jscomp/core/js_analyzer.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ let rec no_side_effect_expression_desc (x : J.expression_desc) =
109109
(* actually true? *) ->
110110
false
111111
| Await _ -> false
112+
| Spread _ -> false
112113

113114
and no_side_effect (x : J.expression) =
114115
no_side_effect_expression_desc x.expression_desc
@@ -212,6 +213,7 @@ let rec eq_expression ({ expression_desc = x0 } : J.expression)
212213
| Number (Uint _) ->
213214
false
214215
| Await _ -> false
216+
| Spread _ -> false
215217

216218
and eq_expression_list xs ys = Ext_list.for_all2_no_exn xs ys eq_expression
217219

jscomp/core/js_dump.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ let rec exp_need_paren ?(arrow=false) (e : J.expression) =
162162
| Js_not _ | Bool _ | New _ ->
163163
false
164164
| Await _ -> false
165+
| Spread _ -> false
165166
| Tagged_template _ -> false
166167
| Optional_block (e, true) when arrow -> exp_need_paren ~arrow e
167168
| Optional_block _ -> false
@@ -907,6 +908,10 @@ and expression_desc cxt ~(level : int) f x : cxt =
907908
P.cond_paren_group f (level > 13) (fun _ ->
908909
P.string f "await ";
909910
expression ~level:13 cxt f e)
911+
| Spread e ->
912+
P.cond_paren_group f (level > 13) (fun _ ->
913+
P.string f "...";
914+
expression ~level:13 cxt f e)
910915

911916
and property_name_and_value_list cxt f (l : J.property_map) =
912917
iter_lst cxt f l

jscomp/core/js_exp_make.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,3 +1368,9 @@ let neq_null_undefined_boolean ?comment (a : t) (b : t) =
13681368

13691369
let make_exception (s : string) =
13701370
pure_runtime_call Js_runtime_modules.exceptions Literals.create [ str s ]
1371+
1372+
let rec variadic_args (args : t list) =
1373+
match args with
1374+
| [] -> []
1375+
| [last] -> [{ last with expression_desc = Spread last }]
1376+
| arg :: args -> arg :: variadic_args args

jscomp/core/js_exp_make.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,5 @@ val for_sure_js_null_undefined : J.expression -> bool
364364
val is_null_undefined : ?comment:string -> t -> t
365365

366366
val make_exception : string -> t
367+
368+
val variadic_args : t list -> t list

jscomp/core/js_fold.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ class fold =
177177
| Await _x0 ->
178178
let _self = _self#expression _x0 in
179179
_self
180+
| Spread _x0 ->
181+
let _self = _self#expression _x0 in
182+
_self
180183

181184
method for_ident_expression : for_ident_expression -> 'self_type =
182185
_self#expression

jscomp/core/js_record_fold.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ let expression_desc : 'a. ('a, expression_desc) fn =
183183
| Await _x0 ->
184184
let st = _self.expression _self st _x0 in
185185
st
186+
| Spread _x0 ->
187+
let st = _self.expression _self st _x0 in
188+
st
186189

187190
let for_ident_expression : 'a. ('a, for_ident_expression) fn =
188191
fun _self arg -> _self.expression _self arg

jscomp/core/js_record_iter.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ let expression_desc : expression_desc fn =
136136
| Undefined _ -> ()
137137
| Null -> ()
138138
| Await _x0 -> _self.expression _self _x0
139+
| Spread _x0 -> _self.expression _self _x0
139140

140141
let for_ident_expression : for_ident_expression fn =
141142
fun _self arg -> _self.expression _self arg

jscomp/core/js_record_map.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ let expression_desc : expression_desc fn =
181181
| Await _x0 ->
182182
let _x0 = _self.expression _self _x0 in
183183
Await _x0
184+
| Spread _x0 ->
185+
let _x0 = _self.expression _self _x0 in
186+
Spread _x0
184187

185188
let for_ident_expression : for_ident_expression fn =
186189
fun _self arg -> _self.expression _self arg

0 commit comments

Comments
 (0)