Skip to content

Commit 5c2aadf

Browse files
chavacavachavacava
and
chavacava
authoredDec 7, 2024
refactor (rule/receiver-naming): replace AST walker by iteration over declarations (#1169)
Co-authored-by: chavacava <salvador.cavadini@gmail.com>
1 parent 24a9325 commit 5c2aadf

File tree

1 file changed

+54
-68
lines changed

1 file changed

+54
-68
lines changed
 

‎rule/receiver_naming.go

+54-68
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,63 @@ func (r *ReceiverNamingRule) configure(arguments lint.Arguments) {
4747
func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) []lint.Failure {
4848
r.configureOnce.Do(func() { r.configure(args) })
4949

50+
typeReceiver := map[string]string{}
5051
var failures []lint.Failure
52+
for _, decl := range file.AST.Decls {
53+
fn, ok := decl.(*ast.FuncDecl)
54+
if !ok || fn.Recv == nil || len(fn.Recv.List) == 0 {
55+
continue
56+
}
5157

52-
fileAst := file.AST
53-
walker := lintReceiverName{
54-
onFailure: func(failure lint.Failure) {
55-
failures = append(failures, failure)
56-
},
57-
typeReceiver: map[string]string{},
58-
receiverNameMaxLength: r.receiverNameMaxLength,
59-
}
58+
names := fn.Recv.List[0].Names
59+
if len(names) < 1 {
60+
continue
61+
}
62+
name := names[0].Name
63+
64+
if name == "_" {
65+
failures = append(failures, lint.Failure{
66+
Node: decl,
67+
Confidence: 1,
68+
Category: "naming",
69+
Failure: "receiver name should not be an underscore, omit the name if it is unused",
70+
})
71+
continue
72+
}
73+
74+
if name == "this" || name == "self" {
75+
failures = append(failures, lint.Failure{
76+
Node: decl,
77+
Confidence: 1,
78+
Category: "naming",
79+
Failure: `receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"`,
80+
})
81+
continue
82+
}
6083

61-
ast.Walk(walker, fileAst)
84+
if r.receiverNameMaxLength > 0 && len([]rune(name)) > r.receiverNameMaxLength {
85+
failures = append(failures, lint.Failure{
86+
Node: decl,
87+
Confidence: 1,
88+
Category: "naming",
89+
Failure: fmt.Sprintf("receiver name %s is longer than %d characters", name, r.receiverNameMaxLength),
90+
})
91+
continue
92+
}
93+
94+
recv := typeparams.ReceiverType(fn)
95+
if prev, ok := typeReceiver[recv]; ok && prev != name {
96+
failures = append(failures, lint.Failure{
97+
Node: decl,
98+
Confidence: 1,
99+
Category: "naming",
100+
Failure: fmt.Sprintf("receiver name %s should be consistent with previous receiver name %s for %s", name, prev, recv),
101+
})
102+
continue
103+
}
104+
105+
typeReceiver[recv] = name
106+
}
62107

63108
return failures
64109
}
@@ -67,62 +112,3 @@ func (r *ReceiverNamingRule) Apply(file *lint.File, args lint.Arguments) []lint.
67112
func (*ReceiverNamingRule) Name() string {
68113
return "receiver-naming"
69114
}
70-
71-
type lintReceiverName struct {
72-
onFailure func(lint.Failure)
73-
typeReceiver map[string]string
74-
receiverNameMaxLength int
75-
}
76-
77-
func (w lintReceiverName) Visit(n ast.Node) ast.Visitor {
78-
fn, ok := n.(*ast.FuncDecl)
79-
if !ok || fn.Recv == nil || len(fn.Recv.List) == 0 {
80-
return w
81-
}
82-
names := fn.Recv.List[0].Names
83-
if len(names) < 1 {
84-
return w
85-
}
86-
name := names[0].Name
87-
if name == "_" {
88-
w.onFailure(lint.Failure{
89-
Node: n,
90-
Confidence: 1,
91-
Category: "naming",
92-
Failure: "receiver name should not be an underscore, omit the name if it is unused",
93-
})
94-
return w
95-
}
96-
if name == "this" || name == "self" {
97-
w.onFailure(lint.Failure{
98-
Node: n,
99-
Confidence: 1,
100-
Category: "naming",
101-
Failure: `receiver name should be a reflection of its identity; don't use generic names such as "this" or "self"`,
102-
})
103-
return w
104-
}
105-
106-
if w.receiverNameMaxLength > 0 && len([]rune(name)) > w.receiverNameMaxLength {
107-
w.onFailure(lint.Failure{
108-
Node: n,
109-
Confidence: 1,
110-
Category: "naming",
111-
Failure: fmt.Sprintf("receiver name %s is longer than %d characters", name, w.receiverNameMaxLength),
112-
})
113-
return w
114-
}
115-
116-
recv := typeparams.ReceiverType(fn)
117-
if prev, ok := w.typeReceiver[recv]; ok && prev != name {
118-
w.onFailure(lint.Failure{
119-
Node: n,
120-
Confidence: 1,
121-
Category: "naming",
122-
Failure: fmt.Sprintf("receiver name %s should be consistent with previous receiver name %s for %s", name, prev, recv),
123-
})
124-
return w
125-
}
126-
w.typeReceiver[recv] = name
127-
return w
128-
}

0 commit comments

Comments
 (0)
Please sign in to comment.