This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
importer.go
193 lines (178 loc) · 4.32 KB
/
importer.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package importer
import (
"fmt"
"go/ast"
"go/token"
stdtypes "go/types"
"sort"
stdstrings "strings"
"github.com/swipe-io/swipe/pkg/astcopy"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)
type ImportInfo struct {
Name string
Differs bool
}
type Importer struct {
pkg *packages.Package
imports map[string]ImportInfo
}
func (i *Importer) Pkg() *packages.Package {
return i.pkg
}
func (i *Importer) Import(name, path string) string {
if path == i.pkg.PkgPath {
return ""
}
const vendorPart = "vendor/"
unvendored := path
if i := stdstrings.LastIndex(path, vendorPart); i != -1 && (i == 0 || path[i-1] == '/') {
unvendored = path[i+len(vendorPart):]
}
if info, ok := i.imports[unvendored]; ok {
return info.Name
}
newName := disambiguate(name, func(n string) bool {
return n == "err" || i.nameInFileScope(n)
})
i.imports[unvendored] = ImportInfo{
Name: newName,
Differs: newName != name,
}
return newName
}
func (i *Importer) RewritePkgRefs(node ast.Node) ast.Node {
start, end := node.Pos(), node.End()
node = astcopy.CopyAST(node)
node = astutil.Apply(node, func(c *astutil.Cursor) bool {
switch node := c.Node().(type) {
case *ast.Ident:
obj := i.pkg.TypesInfo.ObjectOf(node)
if obj == nil {
return false
}
if pkg := obj.Pkg(); pkg != nil && obj.Parent() == pkg.Scope() && pkg.Path() != i.pkg.PkgPath {
newPkgID := i.Import(pkg.Name(), pkg.Path())
c.Replace(&ast.SelectorExpr{
X: ast.NewIdent(newPkgID),
Sel: ast.NewIdent(node.Name),
})
return false
}
return true
case *ast.SelectorExpr:
pkgIdent, ok := node.X.(*ast.Ident)
if !ok {
return true
}
pkgName, ok := i.pkg.TypesInfo.ObjectOf(pkgIdent).(*stdtypes.PkgName)
if !ok {
return true
}
imported := pkgName.Imported()
newPkgID := i.Import(imported.Name(), imported.Path())
c.Replace(&ast.SelectorExpr{
X: ast.NewIdent(newPkgID),
Sel: ast.NewIdent(node.Sel.Name),
})
return false
default:
return true
}
}, nil)
newNames := make(map[stdtypes.Object]string)
inNewNames := func(n string) bool {
for _, other := range newNames {
if other == n {
return true
}
}
return false
}
var scopeStack []*stdtypes.Scope
pkgScope := i.pkg.Types.Scope()
node = astutil.Apply(node, func(c *astutil.Cursor) bool {
if scope := i.pkg.TypesInfo.Scopes[c.Node()]; scope != nil {
scopeStack = append(scopeStack, scope)
}
id, ok := c.Node().(*ast.Ident)
if !ok {
return true
}
obj := i.pkg.TypesInfo.ObjectOf(id)
if obj == nil {
return true
}
if n, ok := newNames[obj]; ok {
c.Replace(ast.NewIdent(n))
return false
}
if par := obj.Parent(); par == nil || par == pkgScope {
return true
}
objName := obj.Name()
if pos := obj.Pos(); pos < start || end <= pos || !(i.nameInFileScope(objName) || inNewNames(objName)) {
return true
}
newName := disambiguate(objName, func(n string) bool {
if i.nameInFileScope(n) || inNewNames(n) {
return true
}
if len(scopeStack) > 0 {
_, obj := scopeStack[len(scopeStack)-1].LookupParent(n, token.NoPos)
if obj != nil {
return true
}
}
return false
})
newNames[obj] = newName
c.Replace(ast.NewIdent(newName))
return false
}, func(c *astutil.Cursor) bool {
if i.pkg.TypesInfo.Scopes[c.Node()] != nil {
scopeStack = scopeStack[:len(scopeStack)-1]
}
return true
})
return node
}
func (i *Importer) nameInFileScope(name string) bool {
for _, other := range i.imports {
if other.Name == name {
return true
}
}
_, obj := i.pkg.Types.Scope().LookupParent(name, token.NoPos)
return obj != nil
}
func (i *Importer) HasImports() bool {
return len(i.imports) > 0
}
func (i *Importer) SortedImports() (result []string) {
imps := make([]string, 0, len(i.imports))
for impPath := range i.imports {
imps = append(imps, impPath)
}
sort.Strings(imps)
result = make([]string, len(imps))
for j, impPath := range imps {
info := i.imports[impPath]
if info.Differs {
result[j] = fmt.Sprintf("\t%s %q\n", info.Name, impPath)
} else {
result[j] = fmt.Sprintf("\t%q\n", impPath)
}
}
return
}
func (i *Importer) QualifyPkg(pkg *stdtypes.Package) string {
return i.Import(pkg.Name(), pkg.Path())
}
func NewImporter(pkg *packages.Package) *Importer {
return &Importer{
pkg: pkg,
imports: map[string]ImportInfo{},
}
}