2019-03-20 20:54:03 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
2024-12-01 17:44:41 +02:00
|
|
|
// DuplicatedImportsRule looks for packages that are imported two or more times.
|
2019-03-20 20:54:03 +02:00
|
|
|
type DuplicatedImportsRule struct{}
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*DuplicatedImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2019-03-20 20:54:03 +02:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
|
|
impPaths := map[string]struct{}{}
|
|
|
|
for _, imp := range file.AST.Imports {
|
|
|
|
path := imp.Path.Value
|
|
|
|
_, ok := impPaths[path]
|
|
|
|
if ok {
|
|
|
|
failures = append(failures, lint.Failure{
|
|
|
|
Confidence: 1,
|
|
|
|
Failure: fmt.Sprintf("Package %s already imported", path),
|
|
|
|
Node: imp,
|
|
|
|
Category: "imports",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
impPaths[path] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*DuplicatedImportsRule) Name() string {
|
2019-03-20 20:54:03 +02:00
|
|
|
return "duplicated-imports"
|
|
|
|
}
|