2018-01-25 21:12:27 +02:00
|
|
|
package rule
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
|
2022-06-18 18:47:53 +02:00
|
|
|
"github.com/mgechev/revive/internal/typeparams"
|
2018-01-25 21:12:27 +02:00
|
|
|
"github.com/mgechev/revive/lint"
|
|
|
|
)
|
|
|
|
|
2018-05-27 06:28:31 +02:00
|
|
|
// ReceiverNamingRule lints given else constructs.
|
|
|
|
type ReceiverNamingRule struct{}
|
2018-01-25 21:12:27 +02:00
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*ReceiverNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
2018-01-25 21:12:27 +02:00
|
|
|
var failures []lint.Failure
|
|
|
|
|
|
|
|
fileAst := file.AST
|
|
|
|
walker := lintReceiverName{
|
|
|
|
onFailure: func(failure lint.Failure) {
|
|
|
|
failures = append(failures, failure)
|
|
|
|
},
|
|
|
|
typeReceiver: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
ast.Walk(walker, fileAst)
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*ReceiverNamingRule) Name() string {
|
2018-05-27 06:28:31 +02:00
|
|
|
return "receiver-naming"
|
2018-01-25 21:12:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type lintReceiverName struct {
|
|
|
|
onFailure func(lint.Failure)
|
|
|
|
typeReceiver map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w lintReceiverName) Visit(n ast.Node) ast.Visitor {
|
|
|
|
fn, ok := n.(*ast.FuncDecl)
|
|
|
|
if !ok || fn.Recv == nil || len(fn.Recv.List) == 0 {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
names := fn.Recv.List[0].Names
|
|
|
|
if len(names) < 1 {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
name := names[0].Name
|
|
|
|
if name == "_" {
|
|
|
|
w.onFailure(lint.Failure{
|
|
|
|
Node: n,
|
|
|
|
Confidence: 1,
|
|
|
|
Category: "naming",
|
|
|
|
Failure: "receiver name should not be an underscore, omit the name if it is unused",
|
|
|
|
})
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
if name == "this" || name == "self" {
|
|
|
|
w.onFailure(lint.Failure{
|
|
|
|
Node: n,
|
|
|
|
Confidence: 1,
|
|
|
|
Category: "naming",
|
|
|
|
Failure: `receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"`,
|
|
|
|
})
|
|
|
|
return w
|
|
|
|
}
|
2022-06-18 18:47:53 +02:00
|
|
|
recv := typeparams.ReceiverType(fn)
|
2018-01-25 21:12:27 +02:00
|
|
|
if prev, ok := w.typeReceiver[recv]; ok && prev != name {
|
|
|
|
w.onFailure(lint.Failure{
|
|
|
|
Node: n,
|
|
|
|
Confidence: 1,
|
|
|
|
Category: "naming",
|
|
|
|
Failure: fmt.Sprintf("receiver name %s should be consistent with previous receiver name %s for %s", name, prev, recv),
|
|
|
|
})
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
w.typeReceiver[recv] = name
|
|
|
|
return w
|
|
|
|
}
|