Skip to content

Commit 23a728b

Browse files
committed
Rename/cleanup.
1 parent f260b54 commit 23a728b

File tree

6 files changed

+132
-118
lines changed

6 files changed

+132
-118
lines changed

analysis/src/Actions.ml

-103
This file was deleted.

analysis/src/Commands.ml

+3-3
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,11 @@ let test ~path =
352352
SemanticTokens.command ~debug:true
353353
~emitter:(SemanticTokens.Token.createEmitter ())
354354
~path
355-
| "act" ->
355+
| "xfm" ->
356356
print_endline
357-
("Actions " ^ path ^ " " ^ string_of_int line ^ ":"
357+
("Xform " ^ path ^ " " ^ string_of_int line ^ ":"
358358
^ string_of_int col);
359-
Actions.command ~path ~pos:(line, col)
359+
Xform.command ~path ~pos:(line, col)
360360
| _ -> ());
361361
print_newline ())
362362
in

analysis/src/Xform.ml

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
(** Code transformations using the parser/printer and ast operations *)
2+
3+
let posInLoc ~pos ~loc =
4+
Utils.tupleOfLexing loc.Location.loc_start <= pos
5+
&& pos < Utils.tupleOfLexing loc.loc_end
6+
7+
module IfThenElse = struct
8+
(* Convert if-then-else to switch *)
9+
10+
let rec listToPat ~itemToPat = function
11+
| [] -> Some []
12+
| x :: xList -> (
13+
match (itemToPat x, listToPat ~itemToPat xList) with
14+
| Some p, Some pList -> Some (p :: pList)
15+
| _ -> None)
16+
17+
let rec expToPat (exp : Parsetree.expression) =
18+
let mkPat ppat_desc =
19+
Ast_helper.Pat.mk ~loc:exp.pexp_loc ~attrs:exp.pexp_attributes ppat_desc
20+
in
21+
match exp.pexp_desc with
22+
| Pexp_construct (lid, None) -> Some (mkPat (Ppat_construct (lid, None)))
23+
| Pexp_construct (lid, Some e1) -> (
24+
match expToPat e1 with
25+
| None -> None
26+
| Some p1 -> Some (mkPat (Ppat_construct (lid, Some p1))))
27+
| Pexp_variant (label, None) -> Some (mkPat (Ppat_variant (label, None)))
28+
| Pexp_variant (label, Some e1) -> (
29+
match expToPat e1 with
30+
| None -> None
31+
| Some p1 -> Some (mkPat (Ppat_variant (label, Some p1))))
32+
| Pexp_constant c -> Some (mkPat (Ppat_constant c))
33+
| Pexp_tuple eList -> (
34+
match listToPat ~itemToPat:expToPat eList with
35+
| None -> None
36+
| Some patList -> Some (mkPat (Ppat_tuple patList)))
37+
| Pexp_record (items, None) -> (
38+
let itemToPat (x, e) =
39+
match expToPat e with None -> None | Some p -> Some (x, p)
40+
in
41+
match listToPat ~itemToPat items with
42+
| None -> None
43+
| Some patItems -> Some (mkPat (Ppat_record (patItems, Closed))))
44+
| Pexp_record (_, Some _) -> None
45+
| _ -> None
46+
47+
let mkMapper ~pos ~changed =
48+
let expr (mapper : Ast_mapper.mapper) (e : Parsetree.expression) =
49+
let newExp =
50+
match e.pexp_desc with
51+
| Pexp_ifthenelse
52+
( {
53+
pexp_desc =
54+
Pexp_apply
55+
( {
56+
pexp_desc =
57+
Pexp_ident {txt = Lident (("=" | "<>") as op)};
58+
},
59+
[(Nolabel, arg1); (Nolabel, arg2)] );
60+
},
61+
e1,
62+
Some e2 )
63+
when posInLoc ~pos ~loc:e.pexp_loc -> (
64+
let e1, e2 = if op = "=" then (e1, e2) else (e2, e1) in
65+
let mkMatch ~arg ~pat =
66+
let cases =
67+
[
68+
Ast_helper.Exp.case pat e1;
69+
Ast_helper.Exp.case (Ast_helper.Pat.any ()) e2;
70+
]
71+
in
72+
Ast_helper.Exp.match_ ~loc:e.pexp_loc ~attrs:e.pexp_attributes arg
73+
cases
74+
in
75+
76+
match expToPat arg2 with
77+
| None -> (
78+
match expToPat arg1 with
79+
| None -> None
80+
| Some pat1 ->
81+
let newExp = mkMatch ~arg:arg2 ~pat:pat1 in
82+
Some newExp)
83+
| Some pat2 ->
84+
let newExp = mkMatch ~arg:arg1 ~pat:pat2 in
85+
Some newExp)
86+
| _ -> None
87+
in
88+
match newExp with
89+
| Some newExp ->
90+
changed := true;
91+
newExp
92+
| None -> Ast_mapper.default_mapper.expr mapper e
93+
in
94+
95+
{Ast_mapper.default_mapper with expr}
96+
97+
let xform ~pos structure =
98+
let changed = ref false in
99+
let mapper = mkMapper ~pos ~changed in
100+
let newStructure = mapper.structure mapper structure in
101+
if !changed then Some newStructure else None
102+
end
103+
104+
let command ~path ~pos =
105+
if Filename.check_suffix path ".res" then
106+
let parser =
107+
Res_driver.parsingEngine.parseImplementation ~forPrinter:false
108+
in
109+
let {Res_driver.parsetree = structure; comments} = parser ~filename:path in
110+
let printer =
111+
Res_printer.printImplementation ~width:!Res_cli.ResClflags.width ~comments
112+
in
113+
match IfThenElse.xform ~pos structure with
114+
| None -> ()
115+
| Some newStructure ->
116+
let formatted = printer newStructure in
117+
Printf.printf "Hit IfThenElse. Formatted:\n%s" formatted

analysis/tests/src/Actions.res analysis/tests/src/Xform.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ let ret = _ => assert false
55
let kind = assert false
66

77
if kind == First {
8-
// ^act
8+
// ^xfm
99
ret("First")
1010
} else {
1111
ret("Not First")
1212
}
1313

1414
#kind("First", {name: "abc", age: 3}) != kind ? ret("Not First") : ret("First")
15-
// ^act
15+
// ^xfm

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ 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/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
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 tests/src/Xform.res
98
Impl cmt:tests/lib/bs/src/Auto.cmt res:tests/src/Auto.res
109
Impl cmt:tests/lib/bs/src/CompletePrioritize1.cmt res:tests/src/CompletePrioritize1.res
1110
Impl cmt:tests/lib/bs/src/CompletePrioritize2.cmt res:tests/src/CompletePrioritize2.res
@@ -31,6 +30,7 @@ Impl cmt:tests/lib/bs/src/Rename.cmt res:tests/src/Rename.res
3130
IntfAndImpl cmti:tests/lib/bs/src/RenameWithInterface.cmti resi:tests/src/RenameWithInterface.resi cmt:tests/lib/bs/src/RenameWithInterface.cmt res:tests/src/RenameWithInterface.res
3231
IntfAndImpl cmti:tests/lib/bs/src/TableclothMap.cmti resi:tests/src/TableclothMap.mli cmt:tests/lib/bs/src/TableclothMap.cmt res:tests/src/TableclothMap.ml
3332
Impl cmt:tests/lib/bs/src/TypeDefinition.cmt res:tests/src/TypeDefinition.res
33+
Impl cmt:tests/lib/bs/src/Xform.cmt res:tests/src/Xform.res
3434
Dependency dirs: tests/node_modules/@rescript/react/lib/bs/src tests/node_modules/@rescript/react/lib/bs/src/legacy
3535
Opens from bsconfig:
3636
locItems:
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Actions tests/src/Actions.res 6:5
2-
Formatted:
1+
Xform tests/src/Xform.res 6:5
2+
Hit IfThenElse. Formatted:
33
type kind = First | Second | Third
44
type r = {name: string, age: int}
55

@@ -8,32 +8,32 @@ let kind = assert false
88

99
switch kind {
1010
| First =>
11-
// ^act
11+
// ^xfm
1212
ret("First")
1313
| _ => ret("Not First")
1414
}
1515

1616
#kind("First", {name: "abc", age: 3}) != kind ? ret("Not First") : ret("First")
17-
// ^act
17+
// ^xfm
1818

19-
Actions tests/src/Actions.res 13:15
20-
Formatted:
19+
Xform tests/src/Xform.res 13:15
20+
Hit IfThenElse. Formatted:
2121
type kind = First | Second | Third
2222
type r = {name: string, age: int}
2323

2424
let ret = _ => assert false
2525
let kind = assert false
2626

2727
if kind == First {
28-
// ^act
28+
// ^xfm
2929
ret("First")
3030
} else {
3131
ret("Not First")
3232
}
3333

3434
switch kind {
3535
| #kind("First", {name: "abc", age: 3}) => ret("First")
36-
// ^act
36+
// ^xfm
3737
| _ => ret("Not First")
3838
}
3939

0 commit comments

Comments
 (0)