-
Notifications
You must be signed in to change notification settings - Fork 0
/
refactor.go
170 lines (145 loc) · 3.52 KB
/
refactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package gofactor
import (
"bytes"
"fmt"
"go/format"
"go/printer"
"go/token"
"io/ioutil"
"strings"
"github.com/bblfsh/sdk/v3/uast"
"github.com/bblfsh/sdk/v3/uast/nodes"
"github.com/bblfsh/sdk/v3/uast/transformer"
"github.com/bblfsh/sdk/v3/uast/uastyaml"
"github.com/lwsanty/gofactor/golang"
"github.com/lwsanty/gofactor/transform/matroshka"
"github.com/lwsanty/gofactor/transform/vartransform"
)
const mainTemplate = `package main
func main() {
%s
}`
type Refactor struct {
before string
after string
m transformer.Transformer
}
func NewRefactor(before, after string) (*Refactor, error) {
r := &Refactor{
before: before,
after: after,
}
if err := r.prepare(); err != nil {
return nil, err
}
return r, nil
}
func (r *Refactor) prepare() error {
in, err := parseNodeHack(r.before)
if err != nil {
return err
}
out, err := parseNodeHack(r.after)
if err != nil {
return err
}
// debug
// dump(in, "../out/1.yml")
// dump(out, "../out/2.yml")
// left side always does Check and the right side performs Construct
matrOpIn := &matroshka.MatroshkaArray{nodeToOp(in).(transformer.ArrayOp)}
matrOpOut := &matroshka.MatroshkaArray{nodeToOp(out).(transformer.ArrayOp)}
r.m = transformer.Mappings(transformer.Map(matrOpIn, matrOpOut))
return nil
}
func (r *Refactor) Apply(code string) (string, error) {
test, err := golang.Parse(code)
if err != nil {
return "", err
}
test, err = trimPositions(test)
if err != nil {
return "", err
}
// debug
// dump(test, "../out/test.yml")
res, err := r.m.Do(test)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
if err := printer.Fprint(buf, token.NewFileSet(), golang.NodeToAST(res)); err != nil {
return "", err
}
fdata, err := format.Source(buf.Bytes())
if err != nil {
return "", err
}
return string(fdata), nil
}
func trimPositions(n nodes.Node) (nodes.Node, error) {
return transformer.Mappings(transformer.Map(
transformer.Part("_", transformer.Obj{uast.KeyPos: transformer.Any()}),
transformer.Part("_", transformer.Obj{}),
)).Do(n)
}
func nodeToOp(n nodes.Node) transformer.Op {
switch o := n.(type) {
case nil:
return transformer.Is(o)
case nodes.Value:
return transformer.Is(o)
case nodes.Object:
if uast.TypeOf(o) == "Ident" {
name := o["Name"]
str := name.(nodes.String)
if strings.HasPrefix(string(str), "X") {
return vartransform.Var(string(str))
}
}
// transformer.Fields is extended version of transformer.Obj
var res transformer.Fields
for k, v := range o {
// conditions to drop pos
var field = transformer.Field{Name: k}
if k == uast.KeyPos {
field.Drop = true
field.Op = transformer.Any()
} else {
field.Op = nodeToOp(v)
}
res = append(res, field)
}
return res
case nodes.Array:
var res []transformer.Op
for _, node := range o {
res = append(res, nodeToOp(node))
}
return transformer.Arr(res...)
default:
panic("not supported type " + o.Kind().String())
}
}
// TODO functions
func parseNodeHack(snippet string) (nodes.Node, error) {
wrapped, err := golang.Parse(wrapInMain(snippet))
if err != nil {
return nil, err
}
list := wrapped.(nodes.Object)["Decls"].(nodes.Array)[0].(nodes.Object)["Body"].(nodes.Object)["List"].(nodes.Array)
return trimPositions(list)
}
// TODO gofmt
func wrapInMain(code string) string {
return fmt.Sprintf(mainTemplate, code)
}
func dump(n nodes.Node, filePath string) {
data, err := uastyaml.Marshal(n)
if err != nil {
panic(err)
}
if err := ioutil.WriteFile(filePath, data, 0644); err != nil {
panic(err)
}
}