-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimportPackageName_checker.go
47 lines (39 loc) · 1.13 KB
/
importPackageName_checker.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
package checkers
import (
"go/ast"
"strings"
"github.com/go-lintpack/lintpack"
"github.com/go-lintpack/lintpack/astwalk"
)
func init() {
var info lintpack.CheckerInfo
info.Name = "importPackageName"
info.Tags = []string{"style"}
info.Summary = "Detects when imported package names are unnecessary renamed"
info.Before = `import lint "github.com/go-critic/go-critic/lint"`
info.After = `import "github.com/go-critic/go-critic/lint"`
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
return &importPackageNameChecker{ctx: ctx}
})
}
type importPackageNameChecker struct {
astwalk.WalkHandler
ctx *lintpack.CheckerContext
}
func (c *importPackageNameChecker) WalkFile(file *ast.File) {
for _, imp := range file.Imports {
var pkgName string
for _, pkgImport := range c.ctx.Pkg.Imports() {
if pkgImport.Path() == strings.Trim(imp.Path.Value, `"`) {
pkgName = pkgImport.Name()
break
}
}
if imp.Name != nil && imp.Name.Name == pkgName {
c.warn(imp)
}
}
}
func (c *importPackageNameChecker) warn(cause ast.Node) {
c.ctx.Warn(cause, "unnecessary rename of import package")
}