/
mutate.go
87 lines (74 loc) · 1.7 KB
/
mutate.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
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"io"
"os"
"reflect"
"strings"
)
var m map[reflect.Type]func(ast.Node)
type Visitor struct {
}
func (v *Visitor) Visit(node ast.Node) (w ast.Visitor) {
if node != nil {
fmt.Println(fset.Position(node.Pos()))
fmt.Println(fset.Position(node.End()))
fmt.Println(reflect.TypeOf(node))
if f := m[reflect.TypeOf(node)]; f != nil {
f(node)
}
}
return v
}
var fset *token.FileSet
func main() {
fset = token.NewFileSet() // positions are relative to fset
m = make(map[reflect.Type]func(ast.Node))
// m[reflect.TypeOf((*ast.FuncDecl)(nil))] = func(node ast.Node) {
// f := node.(*ast.FuncDecl)
// fmt.Println(f.Body)
// f.Body = &ast.BlockStmt{token.NoPos, make([]ast.Stmt, 0), token.NoPos}
// }
m[reflect.TypeOf((*ast.BasicLit)(nil))] = func(node ast.Node) {
f := node.(*ast.BasicLit)
fmt.Println(f.Value)
fmt.Println(f.Kind)
f.Value = "0"
}
dir := "./test_fixtures"
f, err := parser.ParseDir(fset, dir, nil, 0)
if err != nil {
fmt.Println(err)
return
}
for pkgName, pkg := range f {
fmt.Println(pkgName)
fmt.Println(pkg.Files)
for filename, file := range pkg.Files {
if isTestFile(filename) {
continue
}
ast.Walk(&Visitor{}, file)
var w io.Writer
f, err := os.Create(filename)
if err != nil {
fmt.Println(err)
w = os.Stdout
} else {
w = f
defer f.Close()
}
printer.Fprint(w, fset, file)
}
}
}
// isTestFile reports whether the source file is a set of tests and should therefore
// be excluded from coverage analysis.
func isTestFile(file string) bool {
// We don't cover tests, only the code they test.
return strings.HasSuffix(file, "_test.go")
}