Skip to content

Commit ffebcc0

Browse files
committed
Trigger on the body of if-then-else.
1 parent 357436e commit ffebcc0

File tree

3 files changed

+42
-52
lines changed

3 files changed

+42
-52
lines changed

analysis/src/Actions.ml

+34-42
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,51 @@ let rec expToPat (exp : Parsetree.expression) =
4040
| _ -> None
4141

4242
let mkMapper ~pos ~changed =
43-
let value_binding (mapper : Ast_mapper.mapper) (vb : Parsetree.value_binding)
44-
=
43+
let expr (mapper : Ast_mapper.mapper) (e : Parsetree.expression) =
4544
let newExp =
46-
match vb.pvb_pat.ppat_desc with
47-
| Ppat_var {txt; loc} when posInLoc ~pos ~loc -> (
48-
match vb.pvb_expr.pexp_desc with
49-
| Pexp_ifthenelse
50-
( {
51-
pexp_desc =
52-
Pexp_apply
53-
( {
54-
pexp_desc =
55-
Pexp_ident {txt = Lident (("=" | "<>") as op)};
56-
},
57-
[(Nolabel, arg1); (Nolabel, arg2)] );
58-
},
59-
e1,
60-
Some e2 ) -> (
61-
let e1, e2 = if op = "=" then (e1, e2) else (e2, e1) in
62-
let mkMatch ~arg ~pat =
63-
let cases =
64-
[
65-
Ast_helper.Exp.case pat e1;
66-
Ast_helper.Exp.case (Ast_helper.Pat.any ()) e2;
67-
]
68-
in
69-
Ast_helper.Exp.match_ ~loc:vb.pvb_expr.pexp_loc
70-
~attrs:vb.pvb_expr.pexp_attributes arg cases
45+
match e.pexp_desc with
46+
| Pexp_ifthenelse
47+
( {
48+
pexp_desc =
49+
Pexp_apply
50+
( {pexp_desc = Pexp_ident {txt = Lident (("=" | "<>") as op)}},
51+
[(Nolabel, arg1); (Nolabel, arg2)] );
52+
},
53+
e1,
54+
Some e2 )
55+
when posInLoc ~pos ~loc:e.pexp_loc -> (
56+
let e1, e2 = if op = "=" then (e1, e2) else (e2, e1) in
57+
let mkMatch ~arg ~pat =
58+
let cases =
59+
[
60+
Ast_helper.Exp.case pat e1;
61+
Ast_helper.Exp.case (Ast_helper.Pat.any ()) e2;
62+
]
7163
in
64+
Ast_helper.Exp.match_ ~loc:e.pexp_loc ~attrs:e.pexp_attributes arg
65+
cases
66+
in
7267

73-
match expToPat arg2 with
74-
| None -> (
75-
match expToPat arg1 with
76-
| None -> None
77-
| Some pat1 ->
78-
let newExp = mkMatch ~arg:arg2 ~pat:pat1 in
79-
Printf.printf "Hit %s\n" txt;
80-
Some newExp)
81-
| Some pat2 ->
82-
let newExp = mkMatch ~arg:arg1 ~pat:pat2 in
83-
Printf.printf "Hit %s\n" txt;
68+
match expToPat arg2 with
69+
| None -> (
70+
match expToPat arg1 with
71+
| None -> None
72+
| Some pat1 ->
73+
let newExp = mkMatch ~arg:arg2 ~pat:pat1 in
8474
Some newExp)
85-
| _ -> None)
75+
| Some pat2 ->
76+
let newExp = mkMatch ~arg:arg1 ~pat:pat2 in
77+
Some newExp)
8678
| _ -> None
8779
in
8880
match newExp with
8981
| Some newExp ->
9082
changed := true;
91-
{vb with pvb_expr = newExp}
92-
| None -> Ast_mapper.default_mapper.value_binding mapper vb
83+
newExp
84+
| None -> Ast_mapper.default_mapper.expr mapper e
9385
in
9486

95-
{Ast_mapper.default_mapper with value_binding}
87+
{Ast_mapper.default_mapper with expr}
9688

9789
let command ~path ~pos =
9890
if Filename.check_suffix path ".res" then

analysis/tests/src/Actions.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ type r = {name: string, age: int}
33

44
let _ = (kind, kindStr) => {
55
let _ifThenElse = if kind == First {
6-
// ^act
6+
// ^act
77
"First"
88
} else {
99
"Not First"
1010
}
1111

1212
let _ternary = #kind("First", {name: "abc", age: 3}) != kindStr ? "Not First" : "First"
13-
// ^act
13+
// ^act
1414
}
+6-8
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
Actions tests/src/Actions.res 4:10
2-
Hit _ifThenElse
1+
Actions tests/src/Actions.res 4:20
32
Formatted:
43
type kind = First | Second | Third
54
type r = {name: string, age: int}
65

76
let _ = (kind, kindStr) => {
87
let _ifThenElse = switch kind {
9-
| First => // ^act
8+
| First => // ^act
109
"First"
1110
| _ => "Not First"
1211
}
1312

1413
let _ternary = #kind("First", {name: "abc", age: 3}) != kindStr ? "Not First" : "First"
15-
// ^act
14+
// ^act
1615
}
1716

18-
Actions tests/src/Actions.res 11:9
19-
Hit _ternary
17+
Actions tests/src/Actions.res 11:17
2018
Formatted:
2119
type kind = First | Second | Third
2220
type r = {name: string, age: int}
2321

2422
let _ = (kind, kindStr) => {
2523
let _ifThenElse = if kind == First {
26-
// ^act
24+
// ^act
2725
"First"
2826
} else {
2927
"Not First"
@@ -33,6 +31,6 @@ let _ = (kind, kindStr) => {
3331
| #kind("First", {name: "abc", age: 3}) => "First"
3432
| _ => "Not First"
3533
}
36-
// ^act
34+
// ^act
3735
}
3836

0 commit comments

Comments
 (0)