Skip to content

Commit a471795

Browse files
committed
make optional arg labelled
1 parent cd5f2e2 commit a471795

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

compiler/ml/cmt_utils.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type action_type =
4242
| RewriteArrayToTuple
4343
| RewriteIdentToModule of {module_name: string}
4444
| RewriteIdent of {new_ident: Longident.t}
45+
| RewriteArgType of {to_type: [`Labelled | `Optional | `Unlabelled]}
4546
| PrefixVariableWithUnderscore
4647
| RemoveUnusedVariable
4748
| RemoveUnusedType
@@ -90,6 +91,11 @@ let action_to_string = function
9091
| RemoveRecordSpread -> "RemoveRecordSpread"
9192
| AssignToUnderscore -> "AssignToUnderscore"
9293
| PipeToIgnore -> "PipeToIgnore"
94+
| RewriteArgType {to_type} -> (
95+
match to_type with
96+
| `Labelled -> "RewriteArgType(Labelled)"
97+
| `Optional -> "RewriteArgType(Optional)"
98+
| `Unlabelled -> "RewriteArgType(Unlabelled)")
9399

94100
let _add_possible_action : (cmt_action -> unit) ref = ref (fun _ -> ())
95101
let add_possible_action action = !_add_possible_action action
@@ -133,6 +139,13 @@ let emit_possible_actions_from_warning loc w =
133139
{loc; action = PipeToIgnore; description = "Pipe to ignore()"};
134140
add_possible_action
135141
{loc; action = AssignToUnderscore; description = "Assign to let _ ="}
142+
| Nonoptional_label _ ->
143+
add_possible_action
144+
{
145+
loc;
146+
action = RewriteArgType {to_type = `Labelled};
147+
description = "Make argument optional";
148+
}
136149
(*
137150
138151
=== TODO ===
@@ -141,7 +154,6 @@ let emit_possible_actions_from_warning loc w =
141154
| Unused_pat -> (* Remove pattern *) ()
142155
| Unused_argument -> (* Remove unused argument or prefix with underscore *) ()
143156
| Unused_constructor _ -> (* Remove unused constructor *) ()
144-
| Nonoptional_label _ -> (* Add `?` to make argument optional *) ()
145157
| Bs_unused_attribute _ -> (* Remove unused attribute *) ()
146158
| _ -> ()
147159

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
@@ -1607,6 +1607,40 @@ module Actions = struct
16071607
else
16081608
(* Other cases when the loc is on something else in the expr *)
16091609
match (expr.pexp_desc, action.action) with
1610+
| Pexp_apply ({args} as apply), RewriteArgType {to_type}
1611+
->
1612+
let arg_locs =
1613+
args
1614+
|> List.filter_map (fun (lbl, _e) ->
1615+
match lbl with
1616+
| Asttypes.Labelled {loc} | Optional {loc} ->
1617+
Some loc
1618+
| Nolabel -> None)
1619+
in
1620+
if List.mem action.loc arg_locs then
1621+
Some
1622+
{
1623+
expr with
1624+
pexp_desc =
1625+
Pexp_apply
1626+
{
1627+
apply with
1628+
args =
1629+
args
1630+
|> List.map (fun (lbl, e) ->
1631+
( (match (lbl, to_type) with
1632+
| ( Asttypes.Optional {txt; loc},
1633+
`Labelled ) ->
1634+
Asttypes.Labelled {txt; loc}
1635+
| ( Asttypes.Labelled {txt; loc},
1636+
`Optional ) ->
1637+
Asttypes.Optional {txt; loc}
1638+
| _ -> lbl),
1639+
Ast_mapper.default_mapper.expr
1640+
mapper e ));
1641+
};
1642+
}
1643+
else None
16101644
| ( Pexp_let
16111645
( Recursive,
16121646
({pvb_pat = {ppat_loc}} :: _ as bindings),
@@ -1738,7 +1772,8 @@ module Actions = struct
17381772
| ForceOpen -> List.mem "ForceOpen" filter
17391773
| RemoveRecordSpread -> List.mem "RemoveRecordSpread" filter
17401774
| AssignToUnderscore -> List.mem "AssignToUnderscore" filter
1741-
| PipeToIgnore -> List.mem "PipeToIgnore" filter)
1775+
| PipeToIgnore -> List.mem "PipeToIgnore" filter
1776+
| RewriteArgType _ -> List.mem "RewriteArgType" filter)
17421777
in
17431778
match applyActionsToFile path possible_actions with
17441779
| Ok applied ->

0 commit comments

Comments
 (0)