summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mgechev/revive/rule/receiver-naming.go
blob: 589d5f0ef3adda7c11bb0324eba45deb004f0221 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package rule

import (
	"fmt"
	"go/ast"

	"github.com/mgechev/revive/lint"
)

// ReceiverNamingRule lints given else constructs.
type ReceiverNamingRule struct{}

// Apply applies the rule to given file.
func (r *ReceiverNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
	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.
func (r *ReceiverNamingRule) Name() string {
	return "receiver-naming"
}

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
	const ref = styleGuideBase + "#receiver-names"
	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
	}
	recv := receiverType(fn)
	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
}