Skip to content

Commit 9c27553

Browse files
committed
make optional arg labelled
1 parent 3e56f63 commit 9c27553

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

compiler/ml/cmt_utils.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type action_type =
1111
| RewriteArrayToTuple
1212
| RewriteIdentToModule of {module_name: string}
1313
| RewriteIdent of {new_ident: Longident.t}
14+
| RewriteArgType of {to_type: [`Labelled | `Optional | `Unlabelled]}
1415
| PrefixVariableWithUnderscore
1516
| RemoveUnusedVariable
1617
| RemoveUnusedType
@@ -59,6 +60,11 @@ let action_to_string = function
5960
| RemoveRecordSpread -> "RemoveRecordSpread"
6061
| AssignToUnderscore -> "AssignToUnderscore"
6162
| PipeToIgnore -> "PipeToIgnore"
63+
| RewriteArgType {to_type} -> (
64+
match to_type with
65+
| `Labelled -> "RewriteArgType(Labelled)"
66+
| `Optional -> "RewriteArgType(Optional)"
67+
| `Unlabelled -> "RewriteArgType(Unlabelled)")
6268

6369
let _add_possible_action : (cmt_action -> unit) ref = ref (fun _ -> ())
6470
let add_possible_action action = !_add_possible_action action
@@ -102,6 +108,13 @@ let emit_possible_actions_from_warning loc w =
102108
{loc; action = PipeToIgnore; description = "Pipe to ignore()"};
103109
add_possible_action
104110
{loc; action = AssignToUnderscore; description = "Assign to let _ ="}
111+
| Nonoptional_label _ ->
112+
add_possible_action
113+
{
114+
loc;
115+
action = RewriteArgType {to_type = `Labelled};
116+
description = "Make argument optional";
117+
}
105118
(*
106119
107120
=== TODO ===
@@ -110,7 +123,6 @@ let emit_possible_actions_from_warning loc w =
110123
| Unused_pat -> (* Remove pattern *) ()
111124
| Unused_argument -> (* Remove unused argument or prefix with underscore *) ()
112125
| Unused_constructor _ -> (* Remove unused constructor *) ()
113-
| Nonoptional_label _ -> (* Add `?` to make argument optional *) ()
114126
| Bs_unused_attribute _ -> (* Remove unused attribute *) ()
115127
| _ -> ()
116128

compiler/ml/typecore.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4422,7 +4422,6 @@ let report_error env loc ppf error =
44224422
| Apply_wrong_label (l, ty) ->
44234423
let print_message ppf = function
44244424
| Noloc.Nolabel ->
4425-
(* ?TODO(actions) Make labelled *)
44264425
fprintf ppf "The argument at this position should be labelled."
44274426
| l ->
44284427
fprintf ppf "This function does not take the argument @{<info>%s@}."
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let myFunction = (~name: string) => {
2+
ignore(name)
3+
}
4+
let name = "John"
5+
myFunction(~name)
6+
7+
/* === AVAILABLE ACTIONS:
8+
- RewriteArgType(Labelled) - Make argument optional
9+
*/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let myFunction = (~name: string) => {
2+
ignore(name)
3+
}
4+
let name = "John"
5+
myFunction(~name?)

tools/src/tools.ml

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,40 @@ module Actions = struct
16061606
else
16071607
(* Other cases when the loc is on something else in the expr *)
16081608
match (expr.pexp_desc, action.action) with
1609+
| Pexp_apply ({args} as apply), RewriteArgType {to_type}
1610+
->
1611+
let arg_locs =
1612+
args
1613+
|> List.filter_map (fun (lbl, _e) ->
1614+
match lbl with
1615+
| Asttypes.Labelled {loc} | Optional {loc} ->
1616+
Some loc
1617+
| Nolabel -> None)
1618+
in
1619+
if List.mem action.loc arg_locs then
1620+
Some
1621+
{
1622+
expr with
1623+
pexp_desc =
1624+
Pexp_apply
1625+
{
1626+
apply with
1627+
args =
1628+
args
1629+
|> List.map (fun (lbl, e) ->
1630+
( (match (lbl, to_type) with
1631+
| ( Asttypes.Optional {txt; loc},
1632+
`Labelled ) ->
1633+
Asttypes.Labelled {txt; loc}
1634+
| ( Asttypes.Labelled {txt; loc},
1635+
`Optional ) ->
1636+
Asttypes.Optional {txt; loc}
1637+
| _ -> lbl),
1638+
Ast_mapper.default_mapper.expr
1639+
mapper e ));
1640+
};
1641+
}
1642+
else None
16091643
| ( Pexp_let
16101644
( Recursive,
16111645
({pvb_pat = {ppat_loc}} :: _ as bindings),
@@ -1737,7 +1771,8 @@ module Actions = struct
17371771
| ForceOpen -> List.mem "ForceOpen" filter
17381772
| RemoveRecordSpread -> List.mem "RemoveRecordSpread" filter
17391773
| AssignToUnderscore -> List.mem "AssignToUnderscore" filter
1740-
| PipeToIgnore -> List.mem "PipeToIgnore" filter)
1774+
| PipeToIgnore -> List.mem "PipeToIgnore" filter
1775+
| RewriteArgType _ -> List.mem "RewriteArgType" filter)
17411776
in
17421777
match applyActionsToFile path possible_actions with
17431778
| Ok applied ->

0 commit comments

Comments
 (0)