-
Notifications
You must be signed in to change notification settings - Fork 13
/
analyzer.go
149 lines (125 loc) · 3.62 KB
/
analyzer.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
package importas
import (
"fmt"
"go/ast"
"go/types"
"strconv"
"strings"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
var config = &Config{
RequiredAlias: make([][]string, 0),
}
var Analyzer = &analysis.Analyzer{
Name: "importas",
Doc: "Enforces consistent import aliases",
Run: run,
Flags: flags(config),
Requires: []*analysis.Analyzer{inspect.Analyzer},
}
func run(pass *analysis.Pass) (interface{}, error) {
return runWithConfig(config, pass)
}
func runWithConfig(config *Config, pass *analysis.Pass) (interface{}, error) {
if err := config.CompileRegexp(); err != nil {
return nil, err
}
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
inspect.Preorder([]ast.Node{(*ast.ImportSpec)(nil)}, func(n ast.Node) {
visitImportSpecNode(config, n.(*ast.ImportSpec), pass)
})
return nil, nil
}
func visitImportSpecNode(config *Config, node *ast.ImportSpec, pass *analysis.Pass) {
if !config.DisallowUnaliased && node.Name == nil {
return
}
alias := ""
if node.Name != nil {
alias = node.Name.String()
}
if alias == "." {
return // Dot aliases are generally used in tests, so ignore.
}
if strings.HasPrefix(alias, "_") {
return // Used by go test and for auto-includes, not a conflict.
}
path, err := strconv.Unquote(node.Path.Value)
if err != nil {
pass.Reportf(node.Pos(), "import not quoted")
}
if required, exists := config.AliasFor(path); exists && required != alias {
message := fmt.Sprintf("import %q imported as %q but must be %q according to config", path, alias, required)
if alias == "" {
message = fmt.Sprintf("import %q imported without alias but must be with alias %q according to config", path, required)
}
pass.Report(analysis.Diagnostic{
Pos: node.Pos(),
End: node.End(),
Message: message,
SuggestedFixes: []analysis.SuggestedFix{{
Message: "Use correct alias",
TextEdits: findEdits(node, pass.TypesInfo.Uses, path, alias, required),
}},
})
} else if !exists && config.DisallowExtraAliases {
pass.Report(analysis.Diagnostic{
Pos: node.Pos(),
End: node.End(),
Message: fmt.Sprintf("import %q has alias %q which is not part of config", path, alias),
SuggestedFixes: []analysis.SuggestedFix{{
Message: "remove alias",
TextEdits: findEdits(node, pass.TypesInfo.Uses, path, alias, ""),
}},
})
}
}
func findEdits(node ast.Node, uses map[*ast.Ident]types.Object, importPath, original, required string) []analysis.TextEdit {
// Edit the actual import line.
importLine := strconv.Quote(importPath)
if required != "" {
importLine = required + " " + importLine
}
result := []analysis.TextEdit{{
Pos: node.Pos(),
End: node.End(),
NewText: []byte(importLine),
}}
packageReplacement := required
if required == "" {
packageParts := strings.Split(importPath, "/")
if len(packageParts) != 0 {
packageReplacement = packageParts[len(packageParts)-1]
} else {
// fall back to original
packageReplacement = original
}
}
// Edit all the uses of the alias in the code.
for use, pkg := range uses {
pkgName, ok := pkg.(*types.PkgName)
if !ok {
// skip identifiers that aren't pointing at a PkgName.
continue
}
if pkgName.Pos() != node.Pos() {
// skip identifiers pointing to a different import statement.
continue
}
pos := use.Pos()
end := use.End()
replacement := packageReplacement
if packageReplacement == "." {
replacement = ""
end = end + 1
}
result = append(result, analysis.TextEdit{
Pos: pos,
End: end,
NewText: []byte(replacement),
})
}
return result
}