mirror of
https://github.com/mgechev/revive.git
synced 2025-02-09 13:37:14 +02:00
clean-up
This commit is contained in:
parent
b34b9c4861
commit
6ed9d2ca06
@ -1,51 +0,0 @@
|
||||
// Test of confusing-naming rule.
|
||||
|
||||
// Package pkg ...
|
||||
package pkg
|
||||
|
||||
type foo struct {}
|
||||
|
||||
func (t foo) aFoo() {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (t *foo) AFoo() { // MATCH /Method 'AFoo' differs only by capitalization to other method of 'pkg/foo'/
|
||||
return
|
||||
}
|
||||
|
||||
type bar struct {}
|
||||
|
||||
func (t *bar) aBar() {
|
||||
return
|
||||
}
|
||||
|
||||
func (t *bar) aFoo() { // Should not warn
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
|
||||
func aGlobal(){
|
||||
|
||||
}
|
||||
|
||||
func AGlobal(){ // MATCH /Function 'AGlobal' differs only by capitalization to other function/
|
||||
}
|
||||
|
||||
func ABar() { // Should not warn
|
||||
|
||||
}
|
||||
|
||||
func aFoo() { // Should not warn
|
||||
|
||||
}
|
||||
|
||||
func (t foo) ABar() { // Should not warn
|
||||
return
|
||||
}
|
||||
|
||||
func (t bar) ABar() { // MATCH /Method 'ABar' differs only by capitalization to other method of 'pkg/bar'/
|
||||
return
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
// Test of confusing-naming rule.
|
||||
|
||||
// Package pkg ...
|
||||
package pkg
|
||||
|
||||
|
||||
func aglobal(){ // MATCH /Function 'aglobal' differs only by capitalization to other function/
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
package rule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"strings"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
|
||||
// ConfusingNamingRule lints method names that differ only by capitalization
|
||||
type ConfusingNamingRule struct{}
|
||||
|
||||
// Apply applies the rule to given file.
|
||||
func (r *ConfusingNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||
var failures []lint.Failure
|
||||
|
||||
fileAst := file.AST
|
||||
walker := lintConfusingNames{
|
||||
methodNames: make(map[string][]*ast.Ident),
|
||||
onFailure: func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
},
|
||||
}
|
||||
|
||||
ast.Walk(&walker, fileAst)
|
||||
|
||||
return failures
|
||||
}
|
||||
|
||||
// Name returns the rule name.
|
||||
func (r *ConfusingNamingRule) Name() string {
|
||||
return "confusing-naming"
|
||||
}
|
||||
|
||||
//checkMethodName checks if a given method/function name is similar (just case differences) to other method/function of the same struct/file.
|
||||
func checkMethodName(holder string, id *ast.Ident, w *lintConfusingNames) {
|
||||
name := strings.ToUpper(id.Name)
|
||||
if w.methodNames[holder] != nil {
|
||||
blackList := w.methodNames[holder]
|
||||
for _, n := range blackList {
|
||||
if strings.ToUpper(n.Name) == name {
|
||||
// confusing names
|
||||
w.onFailure(lint.Failure{
|
||||
Failure: fmt.Sprintf("Method '%s' differs only by capitalization to method '%s'", id.Name, n.Name),
|
||||
Confidence: 1,
|
||||
Node: id,
|
||||
Category: "naming",
|
||||
URL: "#TODO",
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
// update the black list
|
||||
w.methodNames[holder] = append(w.methodNames[holder], id)
|
||||
}
|
||||
|
||||
type lintConfusingNames struct {
|
||||
methodNames map[string][]*ast.Ident // a map from struct names to method id nodes
|
||||
onFailure func(lint.Failure)
|
||||
}
|
||||
|
||||
const defaultStructName = "_" // used to map functions
|
||||
|
||||
//getStructName of a function receiver. Defaults to defaultStructName
|
||||
func getStructName(r *ast.FieldList) string {
|
||||
result := defaultStructName
|
||||
|
||||
if r == nil || len(r.List) < 1 {
|
||||
return result
|
||||
}
|
||||
|
||||
t := r.List[0].Type
|
||||
|
||||
if p, _ := t.(*ast.StarExpr); p != nil { // if a pointer receiver => dereference pointer receiver types
|
||||
t = p.X
|
||||
}
|
||||
|
||||
if p, _ := t.(*ast.Ident); p != nil {
|
||||
result = p.Name
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (w *lintConfusingNames) Visit(n ast.Node) ast.Visitor {
|
||||
switch v := n.(type) {
|
||||
case *ast.FuncDecl:
|
||||
// Exclude naming warnings for functions that are exported to C but
|
||||
// not exported in the Go API.
|
||||
// See https://github.com/golang/lint/issues/144.
|
||||
if ast.IsExported(v.Name.Name) || !isCgoExported(v) {
|
||||
checkMethodName(getStructName(v.Recv), v.Name, w)
|
||||
}
|
||||
default:
|
||||
// will add other checks like field names, struct names, etc.
|
||||
}
|
||||
|
||||
return w
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mgechev/revive/rule"
|
||||
)
|
||||
|
||||
// TestConfusingNaming rule.
|
||||
func TestConfusingNaming(t *testing.T) {
|
||||
testRule(t, "confusing-naming1", &rule.ConfusingNamingRule{})
|
||||
|
||||
testRule(t, "confusing-naming2", &rule.ConfusingNamingRule{})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user