2018-01-22 04:04:41 +02:00
|
|
|
package rule
|
2017-11-27 04:58:15 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"go/ast"
|
2021-03-06 15:04:57 +02:00
|
|
|
"strings"
|
2017-11-27 04:58:15 +02:00
|
|
|
|
2018-01-25 01:44:03 +02:00
|
|
|
"github.com/mgechev/revive/lint"
|
2017-11-27 04:58:15 +02:00
|
|
|
)
|
|
|
|
|
2024-12-01 17:44:41 +02:00
|
|
|
// BlankImportsRule lints blank imports.
|
2017-11-27 04:58:15 +02:00
|
|
|
type BlankImportsRule struct{}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*BlankImportsRule) Name() string {
|
2017-11-27 05:06:02 +02:00
|
|
|
return "blank-imports"
|
2017-11-27 04:58:15 +02:00
|
|
|
}
|
|
|
|
|
2021-03-06 15:04:57 +02:00
|
|
|
// Apply applies the rule to given file.
|
|
|
|
func (r *BlankImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
|
|
|
if file.Pkg.IsMain() || file.IsTest() {
|
2017-11-27 04:58:15 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-06 15:04:57 +02:00
|
|
|
const (
|
2024-10-01 12:14:02 +02:00
|
|
|
message = "a blank import should be only in a main or test package, or have a comment justifying it"
|
|
|
|
category = "imports"
|
2021-03-06 15:04:57 +02:00
|
|
|
embedImportPath = `"embed"`
|
|
|
|
)
|
|
|
|
|
|
|
|
var failures []lint.Failure
|
|
|
|
|
2017-11-27 04:58:15 +02:00
|
|
|
// The first element of each contiguous group of blank imports should have
|
|
|
|
// an explanatory comment of some kind.
|
2021-03-06 15:04:57 +02:00
|
|
|
for i, imp := range file.AST.Imports {
|
|
|
|
pos := file.ToPosition(imp.Pos())
|
2017-11-27 04:58:15 +02:00
|
|
|
|
|
|
|
if !isBlank(imp.Name) {
|
|
|
|
continue // Ignore non-blank imports.
|
|
|
|
}
|
2021-03-06 15:04:57 +02:00
|
|
|
|
2024-10-01 12:14:02 +02:00
|
|
|
isNotFirstElement := i > 0
|
|
|
|
if isNotFirstElement {
|
2021-03-06 15:04:57 +02:00
|
|
|
prev := file.AST.Imports[i-1]
|
|
|
|
prevPos := file.ToPosition(prev.Pos())
|
|
|
|
|
2021-10-23 13:25:41 +02:00
|
|
|
isSubsequentBlancInAGroup := prevPos.Line+1 == pos.Line && prev.Path.Value != embedImportPath && isBlank(prev.Name)
|
2021-03-06 15:04:57 +02:00
|
|
|
if isSubsequentBlancInAGroup {
|
|
|
|
continue
|
2017-11-27 04:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 15:04:57 +02:00
|
|
|
if imp.Path.Value == embedImportPath && r.fileHasValidEmbedComment(file.AST) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-27 04:58:15 +02:00
|
|
|
// This is the first blank import of a group.
|
|
|
|
if imp.Doc == nil && imp.Comment == nil {
|
2021-03-06 15:04:57 +02:00
|
|
|
failures = append(failures, lint.Failure{Failure: message, Category: category, Node: imp, Confidence: 1})
|
2017-11-27 04:58:15 +02:00
|
|
|
}
|
|
|
|
}
|
2021-03-06 15:04:57 +02:00
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*BlankImportsRule) fileHasValidEmbedComment(fileAst *ast.File) bool {
|
2021-03-06 15:04:57 +02:00
|
|
|
for _, commentGroup := range fileAst.Comments {
|
|
|
|
for _, comment := range commentGroup.List {
|
|
|
|
if strings.HasPrefix(comment.Text, "//go:embed ") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2017-11-27 04:58:15 +02:00
|
|
|
}
|
2024-11-19 23:58:14 +02:00
|
|
|
|
|
|
|
// isBlank returns whether id is the blank identifier "_".
|
|
|
|
// If id == nil, the answer is false.
|
|
|
|
func isBlank(id *ast.Ident) bool { return id != nil && id.Name == "_" }
|