Skip to content

Commit 1f2d463

Browse files
authored
Omit trailing undefined in external function calls (#6653)
* omit trailing undefined in external function calls * changelog * changelog
1 parent 17dc585 commit 1f2d463

5 files changed

+41
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
1313
# 11.1.0-rc.4 (Unreleased)
1414

15+
#### :nail-care: Polish
16+
- Omit `undefined` in external function calls for trailing optional arguments when not supplied. https://github.com/rescript-lang/rescript-compiler/pull/6653
17+
1518
# 11.1.0-rc.3
1619

1720
#### :nail_care: Polish

jscomp/core/lam_compile_external_call.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,16 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
270270
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
271271
else
272272
let args, eff = assemble_args_no_splice arg_types args in
273+
let rec keepNonUndefinedArgs argsList (argTypes : specs) =
274+
match (argsList, argTypes) with
275+
| ( {J.expression_desc = Undefined {isUnit = false}; _} :: rest,
276+
{External_arg_spec.arg_label = Arg_optional; _} :: argTypes ) ->
277+
keepNonUndefinedArgs rest argTypes
278+
| _ -> argsList
279+
in
280+
let args =
281+
keepNonUndefinedArgs (List.rev args) (List.rev arg_types) |> List.rev
282+
in
273283
add_eff eff
274284
@@ E.call ~info:{ arity = Full; call_info = Call_na } fn args
275285
| Js_module_as_fn { external_module_name; splice } ->

jscomp/test/build.ninja

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

jscomp/test/omit_trailing_undefined_in_external_calls.js

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@@uncurried
2+
3+
type dateFormatOptions = {someOption?: bool}
4+
5+
@module("SomeModule")
6+
external formatDate: (Js.Date.t, ~options: dateFormatOptions=?, ~done: bool=?) => string =
7+
"formatDate"
8+
9+
let x = formatDate(Js.Date.make())
10+
let x = formatDate(Js.Date.make(), ~options={someOption: true})
11+
let x = formatDate(Js.Date.make(), ~done=true)

0 commit comments

Comments
 (0)