Skip to content

Commit 1d792f0

Browse files
committed
Test action if-then-else to switch.
1 parent 3968c96 commit 1d792f0

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

analysis/src/Actions.ml

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
let posInLoc ~pos ~loc =
2+
Utils.tupleOfLexing loc.Location.loc_start <= pos
3+
&& pos < Utils.tupleOfLexing loc.loc_end
4+
5+
let rec expToPat (exp : Parsetree.expression) =
6+
let mkPat ppat_desc =
7+
Ast_helper.Pat.mk ~loc:exp.pexp_loc ~attrs:exp.pexp_attributes ppat_desc
8+
in
9+
match exp.pexp_desc with
10+
| Pexp_construct (lid, None) ->
11+
Some (mkPat (Parsetree.Ppat_construct (lid, None)))
12+
| Pexp_construct (lid, Some e1) -> (
13+
match expToPat e1 with
14+
| None -> None
15+
| Some p1 -> Some (mkPat (Parsetree.Ppat_construct (lid, Some p1))))
16+
| Pexp_constant c -> Some (mkPat (Parsetree.Ppat_constant c))
17+
| _ -> None
18+
19+
let mkMapper ~pos ~changed =
20+
let value_binding (mapper : Ast_mapper.mapper) (vb : Parsetree.value_binding)
21+
=
22+
let newExp =
23+
match vb.pvb_pat.ppat_desc with
24+
| Ppat_var {txt; loc} when posInLoc ~pos ~loc -> (
25+
match vb.pvb_expr.pexp_desc with
26+
| Pexp_ifthenelse
27+
( {
28+
pexp_desc =
29+
Pexp_apply
30+
( {
31+
pexp_desc =
32+
Pexp_ident {txt = Lident (("=" | "<>") as op)};
33+
},
34+
[(Nolabel, arg1); (Nolabel, arg2)] );
35+
},
36+
e1,
37+
Some e2 ) -> (
38+
let e1, e2 = if op = "=" then (e1, e2) else (e2, e1) in
39+
let mkMatch ~arg ~pat =
40+
let cases =
41+
[
42+
Ast_helper.Exp.case pat e1;
43+
Ast_helper.Exp.case (Ast_helper.Pat.any ()) e2;
44+
]
45+
in
46+
Ast_helper.Exp.match_ ~loc:vb.pvb_expr.pexp_loc
47+
~attrs:vb.pvb_expr.pexp_attributes arg cases
48+
in
49+
50+
match expToPat arg2 with
51+
| None -> (
52+
match expToPat arg1 with
53+
| None -> None
54+
| Some pat1 ->
55+
let newExp = mkMatch ~arg:arg2 ~pat:pat1 in
56+
Printf.printf "Hit %s\n" txt;
57+
Some newExp)
58+
| Some pat2 ->
59+
let newExp = mkMatch ~arg:arg1 ~pat:pat2 in
60+
Printf.printf "Hit %s\n" txt;
61+
Some newExp)
62+
| _ -> None)
63+
| _ -> None
64+
in
65+
match newExp with
66+
| Some newExp ->
67+
changed := true;
68+
{vb with pvb_expr = newExp}
69+
| None -> Ast_mapper.default_mapper.value_binding mapper vb
70+
in
71+
72+
{Ast_mapper.default_mapper with value_binding}
73+
74+
let command ~path ~pos =
75+
if Filename.check_suffix path ".res" then
76+
let parser =
77+
Res_driver.parsingEngine.parseImplementation ~forPrinter:false
78+
in
79+
let {Res_driver.parsetree = structure; comments; filename} =
80+
parser ~filename:path
81+
in
82+
let printer =
83+
Res_driver.printEngine.printImplementation
84+
~width:!Res_cli.ResClflags.width ~comments ~filename
85+
in
86+
let changed = ref false in
87+
let mapper = mkMapper ~pos ~changed in
88+
let newStructure = mapper.structure mapper structure in
89+
if !changed then printer newStructure

analysis/src/Commands.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ let test ~path =
400400
SemanticTokens.command ~debug:true
401401
~emitter:(SemanticTokens.Token.createEmitter ())
402402
~path
403+
| "act" ->
404+
print_endline
405+
("Actions " ^ path ^ " " ^ string_of_int line ^ ":"
406+
^ string_of_int col);
407+
Actions.command ~path ~pos:(line, col)
403408
| _ -> ());
404409
print_newline ())
405410
in

analysis/tests/src/Actions.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
type kind = First | Second | Third
2+
3+
let _ = (kind, kindStr) => {
4+
let _ifThenElse = if kind == First {
5+
// ^act
6+
"First"
7+
} else {
8+
"Not First"
9+
}
10+
11+
let _ternary = "First" != kindStr ? "Not First" : "First"
12+
// ^act
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Actions tests/src/Actions.res 3:10
2+
Hit _ifThenElse
3+
type kind = First | Second | Third
4+
5+
let _ = (kind, kindStr) => {
6+
let _ifThenElse = switch kind {
7+
| First => // ^act
8+
"First"
9+
| _ => "Not First"
10+
}
11+
12+
let _ternary = "First" != kindStr ? "Not First" : "First"
13+
// ^act
14+
}
15+
16+
Actions tests/src/Actions.res 10:9
17+
Hit _ternary
18+
type kind = First | Second | Third
19+
20+
let _ = (kind, kindStr) => {
21+
let _ifThenElse = if kind == First {
22+
// ^act
23+
"First"
24+
} else {
25+
"Not First"
26+
}
27+
28+
let _ternary = switch kindStr {
29+
| "First" => "First"
30+
| _ => "Not First"
31+
}
32+
// ^act
33+
}
34+

analysis/tests/src/expected/Debug.res.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Dependencies: @rescript/react
44
Source directories: tests/node_modules/@rescript/react/src tests/node_modules/@rescript/react/src/legacy
55
Source files: tests/node_modules/@rescript/react/src/React.res tests/node_modules/@rescript/react/src/ReactDOM.res tests/node_modules/@rescript/react/src/ReactDOMServer.res tests/node_modules/@rescript/react/src/ReactDOMStyle.res tests/node_modules/@rescript/react/src/ReactEvent.res tests/node_modules/@rescript/react/src/ReactEvent.resi tests/node_modules/@rescript/react/src/ReactTestUtils.res tests/node_modules/@rescript/react/src/ReactTestUtils.resi tests/node_modules/@rescript/react/src/RescriptReactErrorBoundary.res tests/node_modules/@rescript/react/src/RescriptReactErrorBoundary.resi tests/node_modules/@rescript/react/src/RescriptReactRouter.res tests/node_modules/@rescript/react/src/RescriptReactRouter.resi tests/node_modules/@rescript/react/src/legacy/ReactDOMRe.res tests/node_modules/@rescript/react/src/legacy/ReasonReact.res
66
Source directories: tests/src tests/src/expected
7-
Source files: tests/src/Auto.res tests/src/CompletePrioritize1.res tests/src/CompletePrioritize2.res tests/src/Completion.res tests/src/Component.res tests/src/Component.resi tests/src/Cross.res tests/src/Debug.res tests/src/Definition.res tests/src/DefinitionWithInterface.res tests/src/DefinitionWithInterface.resi tests/src/Div.res tests/src/Fragment.res tests/src/Highlight.res tests/src/Hover.res tests/src/Jsx.res tests/src/Jsx.resi tests/src/LongIdentTest.res tests/src/Obj.res tests/src/Patterns.res tests/src/RecModules.res tests/src/RecordCompletion.res tests/src/References.res tests/src/ReferencesWithInterface.res tests/src/ReferencesWithInterface.resi tests/src/Rename.res tests/src/RenameWithInterface.res tests/src/RenameWithInterface.resi tests/src/TableclothMap.ml tests/src/TableclothMap.mli tests/src/TypeDefinition.res
7+
Source files: tests/src/Actions.res tests/src/Auto.res tests/src/CompletePrioritize1.res tests/src/CompletePrioritize2.res tests/src/Completion.res tests/src/Component.res tests/src/Component.resi tests/src/Cross.res tests/src/Debug.res tests/src/Definition.res tests/src/DefinitionWithInterface.res tests/src/DefinitionWithInterface.resi tests/src/Div.res tests/src/Fragment.res tests/src/Highlight.res tests/src/Hover.res tests/src/Jsx.res tests/src/Jsx.resi tests/src/LongIdentTest.res tests/src/Obj.res tests/src/Patterns.res tests/src/RecModules.res tests/src/RecordCompletion.res tests/src/References.res tests/src/ReferencesWithInterface.res tests/src/ReferencesWithInterface.resi tests/src/Rename.res tests/src/RenameWithInterface.res tests/src/RenameWithInterface.resi tests/src/TableclothMap.ml tests/src/TableclothMap.mli tests/src/TypeDefinition.res
8+
Impl cmt:tests/lib/bs/src/Actions.cmt res:tests/src/Actions.res
89
Impl cmt:tests/lib/bs/src/Auto.cmt res:tests/src/Auto.res
910
Impl cmt:tests/lib/bs/src/CompletePrioritize1.cmt res:tests/src/CompletePrioritize1.res
1011
Impl cmt:tests/lib/bs/src/CompletePrioritize2.cmt res:tests/src/CompletePrioritize2.res

0 commit comments

Comments
 (0)