Skip to content

Commit fe42237

Browse files
committed
add missing args
1 parent 82ad5e8 commit fe42237

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
@@ -53,6 +53,7 @@ type action_type =
5353
| AssignToUnderscore
5454
| PipeToIgnore
5555
| PartiallyApplyFunction
56+
| InsertMissingArguments of {missing_args: Asttypes.Noloc.arg_label list}
5657

5758
(* TODO:
5859
- Unused var in patterns (and aliases )*)
@@ -98,6 +99,15 @@ let action_to_string = function
9899
| `Optional -> "RewriteArgType(Optional)"
99100
| `Unlabelled -> "RewriteArgType(Unlabelled)")
100101
| PartiallyApplyFunction -> "PartiallyApplyFunction"
102+
| InsertMissingArguments {missing_args} ->
103+
Printf.sprintf "InsertMissingArguments(%s)"
104+
(missing_args
105+
|> List.map (fun arg ->
106+
match arg with
107+
| Asttypes.Noloc.Labelled txt -> "~" ^ txt
108+
| Asttypes.Noloc.Optional txt -> "?" ^ txt
109+
| Asttypes.Noloc.Nolabel -> "<unlabelled>")
110+
|> String.concat ", ")
101111

102112
let _add_possible_action : (cmt_action -> unit) ref = ref (fun _ -> ())
103113
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
@@ -4722,7 +4722,18 @@ let report_error env loc ppf error =
47224722
if not is_fallback then fprintf ppf "@,";
47234723
47244724
if List.length missing_required_args > 0 then (
4725-
(* TODO(actions) Add missing arguments *)
4725+
Cmt_utils.add_possible_action
4726+
{
4727+
loc;
4728+
action =
4729+
InsertMissingArguments
4730+
{
4731+
missing_args =
4732+
missing_required_args
4733+
|> List.map (fun arg -> Noloc.Labelled arg);
4734+
};
4735+
description = "Insert missing arguments";
4736+
};
47264737
Cmt_utils.add_possible_action
47274738
{
47284739
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
@@ -1607,6 +1607,23 @@ 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 ({funct; args} as apply),
1611+
InsertMissingArguments {missing_args} )
1612+
when funct.pexp_loc = action.loc ->
1613+
let args_to_insert =
1614+
missing_args
1615+
|> List.map (fun (lbl : Asttypes.Noloc.arg_label) ->
1616+
( Asttypes.to_arg_label lbl,
1617+
Ast_helper.Exp.extension
1618+
(Location.mknoloc "todo", PStr []) ))
1619+
in
1620+
Some
1621+
{
1622+
expr with
1623+
pexp_desc =
1624+
Pexp_apply
1625+
{apply with args = args @ args_to_insert};
1626+
}
16101627
| ( Pexp_apply ({funct} as apply_args),
16111628
PartiallyApplyFunction )
16121629
when funct.pexp_loc = action.loc ->
@@ -1784,7 +1801,9 @@ module Actions = struct
17841801
| PipeToIgnore -> List.mem "PipeToIgnore" filter
17851802
| PartiallyApplyFunction ->
17861803
List.mem "PartiallyApplyFunction" filter
1787-
| RewriteArgType _ -> List.mem "RewriteArgType" filter)
1804+
| RewriteArgType _ -> List.mem "RewriteArgType" filter
1805+
| InsertMissingArguments _ ->
1806+
List.mem "InsertMissingArguments" filter)
17881807
in
17891808
match applyActionsToFile path possible_actions with
17901809
| Ok applied ->

0 commit comments

Comments
 (0)