-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_file.go
133 lines (109 loc) · 2.62 KB
/
ast_file.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
package safechange
import (
"bytes"
"go/ast"
"go/format"
"go/token"
"reflect"
"sort"
"github.com/go-toolsmith/astcopy"
)
// AstFile is a wrapper for *ast.File which extends with additional methods.
type AstFile struct {
*ast.File
}
// NewAstFile wraps an *ast.File to AstFile.
func NewAstFile(file *ast.File) *AstFile {
return &AstFile{
File: file,
}
}
// Equals returns true if ASTs has exactly the same content.
func (file *AstFile) Equals(cmp *AstFile) bool {
return reflect.DeepEqual(file, cmp)
}
// Copy returns a deep copy.
func (file *AstFile) Copy() *AstFile {
return NewAstFile(astcopy.File(file.File))
}
// EquivalentTo returns true if ASTs are expected to be compiled into the same
// machine code.
func (file *AstFile) EquivalentTo(cmp *AstFile) bool {
// copy ASTs, remove everything non-essential (like comments) and then compare
fileCopy := file.Copy()
fileCopy.stripNonEssentials()
cmpCopy := cmp.Copy()
cmpCopy.stripNonEssentials()
// format ("go fmt")
var outA bytes.Buffer
{
err := format.Node(&outA, token.NewFileSet(), fileCopy.File)
assertNoError(err)
}
var outB bytes.Buffer
{
err := format.Node(&outB, token.NewFileSet(), cmpCopy.File)
assertNoError(err)
}
return outA.String() == outB.String()
}
func assertNoError(err error) {
if err != nil {
panic(err)
}
}
func (file *AstFile) stripNonEssentials() {
// remove all comments
ast.Inspect(file.File, func(node ast.Node) bool {
switch node := node.(type) {
case *ast.CommentGroup, *ast.Comment:
panic("should not have happened")
case *ast.Field:
node.Doc = nil
node.Comment = nil
case *ast.ValueSpec:
node.Doc = nil
node.Comment = nil
case *ast.TypeSpec:
node.Doc = nil
node.Comment = nil
case *ast.GenDecl:
node.Doc = nil
case *ast.FuncDecl:
node.Doc = nil
case *ast.File:
node.Doc = nil
node.Comments = nil
case *ast.ImportSpec:
node.Doc = nil
}
return true
})
// group all imports together and sort
var decls []ast.Decl
importsDecl := &ast.GenDecl{
Tok: token.IMPORT,
}
decls = append(decls, importsDecl)
for _, decl := range file.Decls {
switch decl := decl.(type) {
case *ast.GenDecl:
isImportOnly := true
for _, spec := range decl.Specs {
if spec, ok := spec.(*ast.ImportSpec); ok {
importsDecl.Specs = append(importsDecl.Specs, spec)
} else {
isImportOnly = false
}
}
if isImportOnly {
continue
}
}
decls = append(decls, decl)
}
sort.Slice(importsDecl.Specs, func(i, j int) bool {
return importsDecl.Specs[i].(*ast.ImportSpec).Path.Value < importsDecl.Specs[j].(*ast.ImportSpec).Path.Value
})
file.Decls = decls
}