forked from go-critic/go-critic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checkers: add dupImports checker (go-critic#814)
- Loading branch information
1 parent
a9a4a15
commit f5dab9f
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package checkers | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
|
||
"github.com/go-lintpack/lintpack" | ||
) | ||
|
||
func init() { | ||
var info lintpack.CheckerInfo | ||
info.Name = "dupImports" | ||
info.Tags = []string{"style", "experimental"} | ||
info.Summary = "Detects multiple imports of the same package under different aliases." | ||
info.Before = ` | ||
import ( | ||
"fmt" | ||
priting "fmt" | ||
) | ||
func helloWorld() { | ||
fmt.Println("Hello") | ||
printing.Println("World") | ||
}` | ||
info.After = ` | ||
import( | ||
"fmt" | ||
) | ||
func helloWorld() { | ||
fmt.Println("Hello") | ||
fmt.Println("World") | ||
}` | ||
|
||
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker { | ||
return &dupImportChecker{ctx: ctx} | ||
}) | ||
} | ||
|
||
type dupImportChecker struct { | ||
ctx *lintpack.CheckerContext | ||
} | ||
|
||
func (c *dupImportChecker) WalkFile(f *ast.File) { | ||
imports := make(map[string][]*ast.ImportSpec) | ||
for _, importDcl := range f.Imports { | ||
pkg := importDcl.Path.Value | ||
imports[pkg] = append(imports[pkg], importDcl) | ||
} | ||
|
||
fPos := c.ctx.FileSet.File(f.Pos()) | ||
if fPos == nil { | ||
// Bail out if we can't, for any reason, determine | ||
// the source file we are dealing with. | ||
return | ||
} | ||
for _, importList := range imports { | ||
if len(importList) == 1 { | ||
continue | ||
} | ||
|
||
msg := fmt.Sprintf("package is imported %d times under different aliases on lines", len(importList)) | ||
for idx, importDcl := range importList { | ||
switch { | ||
case idx == len(importList)-1: | ||
msg += " and" | ||
case idx > 0: | ||
msg += "," | ||
} | ||
msg += fmt.Sprintf(" %d", fPos.Line(importDcl.Pos())) | ||
} | ||
for _, importDcl := range importList { | ||
c.ctx.Warn(importDcl, msg) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package checker_test | ||
|
||
import "fmt" | ||
|
||
func negativeHelloworld() { | ||
fmt.Println("Hello") | ||
fmt.Println("Shiny") | ||
fmt.Println("World") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package checker_test | ||
|
||
/*! package is imported 3 times under different aliases on lines 4, 8 and 10 */ | ||
import printing "fmt" | ||
|
||
import ( | ||
/*! package is imported 3 times under different aliases on lines 4, 8 and 10 */ | ||
"fmt" | ||
/*! package is imported 3 times under different aliases on lines 4, 8 and 10 */ | ||
print "fmt" | ||
) | ||
|
||
func positiveHelloworld() { | ||
fmt.Println("Hello") | ||
print.Println("Shiny") | ||
printing.Println("World") | ||
} |