Skip to content

Commit 4dfa741

Browse files
committed
add missing args
1 parent f78269f commit 4dfa741

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

compiler/ml/cmt_utils.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type action_type =
2222
| AssignToUnderscore
2323
| PipeToIgnore
2424
| PartiallyApplyFunction
25+
| InsertMissingArguments of {missing_args: Asttypes.Noloc.arg_label list}
2526

2627
(* TODO:
2728
- Unused var in patterns (and aliases )*)
@@ -67,6 +68,15 @@ let action_to_string = function
6768
| `Optional -> "RewriteArgType(Optional)"
6869
| `Unlabelled -> "RewriteArgType(Unlabelled)")
6970
| PartiallyApplyFunction -> "PartiallyApplyFunction"
71+
| InsertMissingArguments {missing_args} ->
72+
Printf.sprintf "InsertMissingArguments(%s)"
73+
(missing_args
74+
|> List.map (fun arg ->
75+
match arg with
76+
| Asttypes.Noloc.Labelled txt -> "~" ^ txt
77+
| Asttypes.Noloc.Optional txt -> "?" ^ txt
78+
| Asttypes.Noloc.Nolabel -> "<unlabelled>")
79+
|> String.concat ", ")
7080

7181
let _add_possible_action : (cmt_action -> unit) ref = ref (fun _ -> ())
7282
let add_possible_action action = !_add_possible_action action

compiler/ml/typecore.ml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4656,7 +4656,18 @@ let report_error env loc ppf error =
46564656
if not is_fallback then fprintf ppf "@,";
46574657
46584658
if List.length missing_required_args > 0 then (
4659-
(* TODO(actions) Add missing arguments *)
4659+
Cmt_utils.add_possible_action
4660+
{
4661+
loc;
4662+
action =
4663+
InsertMissingArguments
4664+
{
4665+
missing_args =
4666+
missing_required_args
4667+
|> List.map (fun arg -> Noloc.Labelled arg);
4668+
};
4669+
description = "Insert missing arguments";
4670+
};
46604671
Cmt_utils.add_possible_action
46614672
{
46624673
loc;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// actionFilter=InsertMissingArguments
2+
let x = (~a, ~b) => a + b
3+
let y = x(~a=2, ~b=%todo) + 2
4+
5+
/* === AVAILABLE ACTIONS:
6+
- PartiallyApplyFunction - Partially apply function
7+
- InsertMissingArguments(~b) - Insert missing arguments
8+
*/

tests/build_tests/actions/expected/Actions_PartiallyApplyFunction_applied.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ let y = x(~a=2, ...) + 2
44

55
/* === AVAILABLE ACTIONS:
66
- PartiallyApplyFunction - Partially apply function
7+
- InsertMissingArguments(~b) - Insert missing arguments
78
*/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// actionFilter=InsertMissingArguments
2+
let x = (~a, ~b) => a + b
3+
let y = x(~a=2) + 2

tools/src/tools.ml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,23 @@ 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 ({funct; args} as apply),
1610+
InsertMissingArguments {missing_args} )
1611+
when funct.pexp_loc = action.loc ->
1612+
let args_to_insert =
1613+
missing_args
1614+
|> List.map (fun (lbl : Asttypes.Noloc.arg_label) ->
1615+
( Asttypes.to_arg_label lbl,
1616+
Ast_helper.Exp.extension
1617+
(Location.mknoloc "todo", PStr []) ))
1618+
in
1619+
Some
1620+
{
1621+
expr with
1622+
pexp_desc =
1623+
Pexp_apply
1624+
{apply with args = args @ args_to_insert};
1625+
}
16091626
| ( Pexp_apply ({funct} as apply_args),
16101627
PartiallyApplyFunction )
16111628
when funct.pexp_loc = action.loc ->
@@ -1783,7 +1800,9 @@ module Actions = struct
17831800
| PipeToIgnore -> List.mem "PipeToIgnore" filter
17841801
| PartiallyApplyFunction ->
17851802
List.mem "PartiallyApplyFunction" filter
1786-
| RewriteArgType _ -> List.mem "RewriteArgType" filter)
1803+
| RewriteArgType _ -> List.mem "RewriteArgType" filter
1804+
| InsertMissingArguments _ ->
1805+
List.mem "InsertMissingArguments" filter)
17871806
in
17881807
match applyActionsToFile path possible_actions with
17891808
| Ok applied ->

0 commit comments

Comments
 (0)