Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix trailing undefined for optional parameters not omitted with @send
  • Loading branch information
cknitt committed Apr 6, 2024
commit a133a38179b794cf1c0c8ed855e83bd4324f15f5
22 changes: 12 additions & 10 deletions jscomp/core/lam_compile_external_call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ let translate_scoped_access scopes obj =
| [] -> obj
| x :: xs -> Ext_list.fold_left xs (E.dot obj x) E.dot

let keep_non_undefined_args args arg_types =
let rec aux argsList (argTypes : specs) =
match (argsList, argTypes) with
| ( {J.expression_desc = Undefined {isUnit = false}; _} :: rest,
{External_arg_spec.arg_label = Arg_optional; _} :: argTypes ) ->
aux rest argTypes
| _ -> argsList
in
aux (List.rev args) (List.rev arg_types) |> List.rev

let translate_ffi (cxt : Lam_compile_context.t) arg_types
(ffi : External_ffi_types.external_spec) (args : J.expression list) ~dynamic_import =
match ffi with
Expand All @@ -271,16 +281,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
else E.call ~info:{ arity = Full; call_info = Call_na } fn args)
else
let args, eff = assemble_args_no_splice arg_types args in
let rec keepNonUndefinedArgs argsList (argTypes : specs) =
match (argsList, argTypes) with
| ( {J.expression_desc = Undefined {isUnit = false}; _} :: rest,
{External_arg_spec.arg_label = Arg_optional; _} :: argTypes ) ->
keepNonUndefinedArgs rest argTypes
| _ -> argsList
in
let args =
keepNonUndefinedArgs (List.rev args) (List.rev arg_types) |> List.rev
in
let args = keep_non_undefined_args args arg_types in
add_eff eff
@@ E.call ~info:{ arity = Full; call_info = Call_na } fn args
| Js_module_as_fn { external_module_name; splice } ->
Expand Down Expand Up @@ -341,6 +342,7 @@ let translate_ffi (cxt : Lam_compile_context.t) arg_types
(E.dot self name) args)
else
let args, eff = assemble_args_no_splice arg_types args in
let args = keep_non_undefined_args args arg_types in
add_eff eff
(let self = translate_scoped_access js_send_scopes self in
E.call
Expand Down
4 changes: 3 additions & 1 deletion jscomp/test/omit_trailing_undefined_in_external_calls.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions jscomp/test/omit_trailing_undefined_in_external_calls.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ external formatDate: (Js.Date.t, ~options: dateFormatOptions=?, ~done: bool=?) =
let x = formatDate(Js.Date.make())
let x = formatDate(Js.Date.make(), ~options={someOption: true})
let x = formatDate(Js.Date.make(), ~done=true)

@send external toString: (float, ~radix: int=?) => string = "toString"

let x = toString(42.)