Skip to content

Commit f5dab9f

Browse files
Helcaraxanquasilyte
authored andcommitted
checkers: add dupImports checker (go-critic#814)
1 parent a9a4a15 commit f5dab9f

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

checkers/dupImports_checker.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package checkers
2+
3+
import (
4+
"fmt"
5+
"go/ast"
6+
7+
"github.com/go-lintpack/lintpack"
8+
)
9+
10+
func init() {
11+
var info lintpack.CheckerInfo
12+
info.Name = "dupImports"
13+
info.Tags = []string{"style", "experimental"}
14+
info.Summary = "Detects multiple imports of the same package under different aliases."
15+
info.Before = `
16+
import (
17+
"fmt"
18+
priting "fmt"
19+
)
20+
21+
func helloWorld() {
22+
fmt.Println("Hello")
23+
printing.Println("World")
24+
}`
25+
info.After = `
26+
import(
27+
"fmt"
28+
)
29+
30+
func helloWorld() {
31+
fmt.Println("Hello")
32+
fmt.Println("World")
33+
}`
34+
35+
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker {
36+
return &dupImportChecker{ctx: ctx}
37+
})
38+
}
39+
40+
type dupImportChecker struct {
41+
ctx *lintpack.CheckerContext
42+
}
43+
44+
func (c *dupImportChecker) WalkFile(f *ast.File) {
45+
imports := make(map[string][]*ast.ImportSpec)
46+
for _, importDcl := range f.Imports {
47+
pkg := importDcl.Path.Value
48+
imports[pkg] = append(imports[pkg], importDcl)
49+
}
50+
51+
fPos := c.ctx.FileSet.File(f.Pos())
52+
if fPos == nil {
53+
// Bail out if we can't, for any reason, determine
54+
// the source file we are dealing with.
55+
return
56+
}
57+
for _, importList := range imports {
58+
if len(importList) == 1 {
59+
continue
60+
}
61+
62+
msg := fmt.Sprintf("package is imported %d times under different aliases on lines", len(importList))
63+
for idx, importDcl := range importList {
64+
switch {
65+
case idx == len(importList)-1:
66+
msg += " and"
67+
case idx > 0:
68+
msg += ","
69+
}
70+
msg += fmt.Sprintf(" %d", fPos.Line(importDcl.Pos()))
71+
}
72+
for _, importDcl := range importList {
73+
c.ctx.Warn(importDcl, msg)
74+
}
75+
}
76+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package checker_test
2+
3+
import "fmt"
4+
5+
func negativeHelloworld() {
6+
fmt.Println("Hello")
7+
fmt.Println("Shiny")
8+
fmt.Println("World")
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package checker_test
2+
3+
/*! package is imported 3 times under different aliases on lines 4, 8 and 10 */
4+
import printing "fmt"
5+
6+
import (
7+
/*! package is imported 3 times under different aliases on lines 4, 8 and 10 */
8+
"fmt"
9+
/*! package is imported 3 times under different aliases on lines 4, 8 and 10 */
10+
print "fmt"
11+
)
12+
13+
func positiveHelloworld() {
14+
fmt.Println("Hello")
15+
print.Println("Shiny")
16+
printing.Println("World")
17+
}

0 commit comments

Comments
 (0)