Skip to content

Commit cdb57c5

Browse files
committed
handle top level
1 parent 603b066 commit cdb57c5

File tree

7 files changed

+82
-4
lines changed

7 files changed

+82
-4
lines changed

compiler/ext/warnings.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,6 @@ let message = function
502502
"Integer literal exceeds the range of representable integers of type int"
503503
| Bs_uninterpreted_delimiters s -> "Uninterpreted delimiters " ^ s
504504
| Bs_toplevel_expression_unit help ->
505-
(* TODO(actions) Assign to `let _ =` *)
506-
(* TODO(actions) Ignore *)
507505
Printf.sprintf
508506
"This%sis at the top level and is expected to return `unit`. But it's \
509507
returning %s.\n\n\

compiler/ml/cmt_utils.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type action_type =
1818
| RemoveRecFlag
1919
| RemoveRecordSpread
2020
| ForceOpen
21+
| AssignToUnderscore
22+
| PipeToIgnore
2123

2224
(* TODO:
2325
- Unused var in patterns (and aliases )*)
@@ -55,6 +57,8 @@ let action_to_string = function
5557
| RemoveRecFlag -> "RemoveRecFlag"
5658
| ForceOpen -> "ForceOpen"
5759
| RemoveRecordSpread -> "RemoveRecordSpread"
60+
| AssignToUnderscore -> "AssignToUnderscore"
61+
| PipeToIgnore -> "PipeToIgnore"
5862

5963
let _add_possible_action : (cmt_action -> unit) ref = ref (fun _ -> ())
6064
let add_possible_action action = !_add_possible_action action
@@ -102,7 +106,10 @@ let emit_possible_actions_from_warning loc w =
102106
| Unused_argument -> (* Remove unused argument or prefix with underscore *) ()
103107
| Nonoptional_label _ -> (* Add `?` to make argument optional *) ()
104108
| Bs_toplevel_expression_unit _ ->
105-
(* Assign to let _ = or pipe to ignore() *) ()
109+
add_possible_action
110+
{loc; action = PipeToIgnore; description = "Pipe to ignore()"};
111+
add_possible_action
112+
{loc; action = AssignToUnderscore; description = "Assign to let _ ="}
106113
| _ -> ()
107114

108115
let _ =
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let _ = // actionFilter=AssignToUnderscore
2+
switch 1 {
3+
| _ => "one"
4+
}
5+
6+
/* === AVAILABLE ACTIONS:
7+
- AssignToUnderscore - Assign to let _ =
8+
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// actionFilter=PipeToIgnore
2+
switch 1 {
3+
| _ => "one"
4+
}->ignore
5+
6+
/* === AVAILABLE ACTIONS:
7+
- PipeToIgnore - Pipe to ignore()
8+
*/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// actionFilter=AssignToUnderscore
2+
switch 1 {
3+
| _ => "one"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// actionFilter=PipeToIgnore
2+
switch 1 {
3+
| _ => "one"
4+
}

tools/src/tools.ml

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,32 @@ module Actions = struct
13161316
| _ -> None)
13171317
actions
13181318
in
1319+
let assign_to_underscore_action_locs =
1320+
List.filter_map
1321+
(fun (action : Cmt_utils.cmt_action) ->
1322+
match action.action with
1323+
| AssignToUnderscore -> Some action.loc
1324+
| _ -> None)
1325+
actions
1326+
in
13191327
match str_item.pstr_desc with
1328+
| Pstr_eval (({pexp_loc} as e), attrs)
1329+
when List.mem pexp_loc assign_to_underscore_action_locs ->
1330+
let str_item =
1331+
Ast_mapper.default_mapper.structure_item mapper str_item
1332+
in
1333+
let loc = str_item.pstr_loc in
1334+
{
1335+
str_item with
1336+
pstr_desc =
1337+
Pstr_value
1338+
( Nonrecursive,
1339+
[
1340+
Ast_helper.Vb.mk ~loc ~attrs
1341+
(Ast_helper.Pat.var ~loc (Location.mkloc "_" loc))
1342+
e;
1343+
] );
1344+
}
13201345
| Pstr_open ({popen_override = Fresh} as open_desc)
13211346
when List.mem str_item.pstr_loc force_open_action_locs ->
13221347
let str_item =
@@ -1457,6 +1482,28 @@ module Actions = struct
14571482
if action.loc = expr.pexp_loc then
14581483
let expr = Ast_mapper.default_mapper.expr mapper expr in
14591484
match action.action with
1485+
| PipeToIgnore ->
1486+
Some
1487+
{
1488+
expr with
1489+
pexp_desc =
1490+
Pexp_apply
1491+
{
1492+
funct =
1493+
Ast_helper.Exp.ident
1494+
(Location.mknoloc (Longident.Lident "->"));
1495+
partial = false;
1496+
transformed_jsx = false;
1497+
args =
1498+
[
1499+
(Nolabel, expr);
1500+
( Nolabel,
1501+
Ast_helper.Exp.ident
1502+
(Location.mknoloc
1503+
(Longident.Lident "ignore")) );
1504+
];
1505+
};
1506+
}
14601507
| RemoveRecordSpread -> (
14611508
match expr with
14621509
| {pexp_desc = Pexp_record (fields, Some _)} ->
@@ -1688,7 +1735,9 @@ module Actions = struct
16881735
| RemoveUnusedModule -> List.mem "RemoveUnusedModule" filter
16891736
| RemoveRecFlag -> List.mem "RemoveRecFlag" filter
16901737
| ForceOpen -> List.mem "ForceOpen" filter
1691-
| RemoveRecordSpread -> List.mem "RemoveRecordSpread" filter)
1738+
| RemoveRecordSpread -> List.mem "RemoveRecordSpread" filter
1739+
| AssignToUnderscore -> List.mem "AssignToUnderscore" filter
1740+
| PipeToIgnore -> List.mem "PipeToIgnore" filter)
16921741
in
16931742
match applyActionsToFile path possible_actions with
16941743
| Ok applied ->

0 commit comments

Comments
 (0)