mirror of
https://github.com/mgechev/revive.git
synced 2024-11-24 08:32:22 +02:00
205 lines
4.9 KiB
Go
205 lines
4.9 KiB
Go
package rule
|
|
|
|
import (
|
|
"fmt"
|
|
"go/ast"
|
|
"go/token"
|
|
"strings"
|
|
|
|
"github.com/mgechev/revive/lint"
|
|
)
|
|
|
|
// VarNamingRule lints given else constructs.
|
|
type VarNamingRule struct{}
|
|
|
|
// Apply applies the rule to given file.
|
|
func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
|
var failures []lint.Failure
|
|
|
|
fileAst := file.AST
|
|
walker := lintNames{
|
|
file: file,
|
|
fileAst: fileAst,
|
|
onFailure: func(failure lint.Failure) {
|
|
failures = append(failures, failure)
|
|
},
|
|
}
|
|
|
|
// Package names need slightly different handling than other names.
|
|
if strings.Contains(walker.fileAst.Name.Name, "_") && !strings.HasSuffix(walker.fileAst.Name.Name, "_test") {
|
|
walker.onFailure(lint.Failure{
|
|
Failure: "don't use an underscore in package name",
|
|
Confidence: 1,
|
|
Node: walker.fileAst,
|
|
Category: "naming",
|
|
URL: "http://golang.org/doc/effective_go.html#package-names",
|
|
})
|
|
}
|
|
|
|
ast.Walk(&walker, fileAst)
|
|
|
|
return failures
|
|
}
|
|
|
|
// Name returns the rule name.
|
|
func (r *VarNamingRule) Name() string {
|
|
return "var-naming"
|
|
}
|
|
|
|
func checkList(fl *ast.FieldList, thing string, w *lintNames) {
|
|
if fl == nil {
|
|
return
|
|
}
|
|
for _, f := range fl.List {
|
|
for _, id := range f.Names {
|
|
check(id, thing, w)
|
|
}
|
|
}
|
|
}
|
|
|
|
func check(id *ast.Ident, thing string, w *lintNames) {
|
|
if id.Name == "_" {
|
|
return
|
|
}
|
|
if knownNameExceptions[id.Name] {
|
|
return
|
|
}
|
|
|
|
// Handle two common styles from other languages that don't belong in Go.
|
|
if len(id.Name) >= 5 && allCapsRE.MatchString(id.Name) && strings.Contains(id.Name, "_") {
|
|
w.onFailure(lint.Failure{
|
|
Failure: "don't use ALL_CAPS in Go names; use CamelCase",
|
|
Confidence: 0.8,
|
|
Node: id,
|
|
Category: "naming",
|
|
URL: "#mixed-caps",
|
|
})
|
|
return
|
|
}
|
|
if len(id.Name) > 2 && id.Name[0] == 'k' && id.Name[1] >= 'A' && id.Name[1] <= 'Z' {
|
|
should := string(id.Name[1]+'a'-'A') + id.Name[2:]
|
|
w.onFailure(lint.Failure{
|
|
Failure: fmt.Sprintf("don't use leading k in Go names; %s %s should be %s", thing, id.Name, should),
|
|
Confidence: 0.8,
|
|
Node: id,
|
|
Category: "naming",
|
|
URL: "#mixed-caps",
|
|
})
|
|
}
|
|
|
|
should := lint.Name(id.Name)
|
|
if id.Name == should {
|
|
return
|
|
}
|
|
|
|
if len(id.Name) > 2 && strings.Contains(id.Name[1:], "_") {
|
|
w.onFailure(lint.Failure{
|
|
Failure: fmt.Sprintf("don't use underscores in Go names; %s %s should be %s", thing, id.Name, should),
|
|
Confidence: 0.9,
|
|
Node: id,
|
|
Category: "naming",
|
|
URL: "http://golang.org/doc/effective_go.html#mixed-caps",
|
|
})
|
|
return
|
|
}
|
|
w.onFailure(lint.Failure{
|
|
Failure: fmt.Sprintf("%s %s should be %s", thing, id.Name, should),
|
|
Confidence: 0.8,
|
|
Node: id,
|
|
Category: "naming",
|
|
URL: "#initialisms",
|
|
})
|
|
}
|
|
|
|
type lintNames struct {
|
|
file *lint.File
|
|
fileAst *ast.File
|
|
lastGen *ast.GenDecl
|
|
genDeclMissingComments map[*ast.GenDecl]bool
|
|
onFailure func(lint.Failure)
|
|
}
|
|
|
|
func (w *lintNames) Visit(n ast.Node) ast.Visitor {
|
|
switch v := n.(type) {
|
|
case *ast.AssignStmt:
|
|
if v.Tok == token.ASSIGN {
|
|
return w
|
|
}
|
|
for _, exp := range v.Lhs {
|
|
if id, ok := exp.(*ast.Ident); ok {
|
|
check(id, "var", w)
|
|
}
|
|
}
|
|
case *ast.FuncDecl:
|
|
if w.file.IsTest() && (strings.HasPrefix(v.Name.Name, "Example") || strings.HasPrefix(v.Name.Name, "Test") || strings.HasPrefix(v.Name.Name, "Benchmark")) {
|
|
return w
|
|
}
|
|
|
|
thing := "func"
|
|
if v.Recv != nil {
|
|
thing = "method"
|
|
}
|
|
|
|
// 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) {
|
|
check(v.Name, thing, w)
|
|
}
|
|
|
|
checkList(v.Type.Params, thing+" parameter", w)
|
|
checkList(v.Type.Results, thing+" result", w)
|
|
case *ast.GenDecl:
|
|
if v.Tok == token.IMPORT {
|
|
return w
|
|
}
|
|
var thing string
|
|
switch v.Tok {
|
|
case token.CONST:
|
|
thing = "const"
|
|
case token.TYPE:
|
|
thing = "type"
|
|
case token.VAR:
|
|
thing = "var"
|
|
}
|
|
for _, spec := range v.Specs {
|
|
switch s := spec.(type) {
|
|
case *ast.TypeSpec:
|
|
check(s.Name, thing, w)
|
|
case *ast.ValueSpec:
|
|
for _, id := range s.Names {
|
|
check(id, thing, w)
|
|
}
|
|
}
|
|
}
|
|
case *ast.InterfaceType:
|
|
// Do not check interface method names.
|
|
// They are often constrainted by the method names of concrete types.
|
|
for _, x := range v.Methods.List {
|
|
ft, ok := x.Type.(*ast.FuncType)
|
|
if !ok { // might be an embedded interface name
|
|
continue
|
|
}
|
|
checkList(ft.Params, "interface method parameter", w)
|
|
checkList(ft.Results, "interface method result", w)
|
|
}
|
|
case *ast.RangeStmt:
|
|
if v.Tok == token.ASSIGN {
|
|
return w
|
|
}
|
|
if id, ok := v.Key.(*ast.Ident); ok {
|
|
check(id, "range var", w)
|
|
}
|
|
if id, ok := v.Value.(*ast.Ident); ok {
|
|
check(id, "range var", w)
|
|
}
|
|
case *ast.StructType:
|
|
for _, f := range v.Fields.List {
|
|
for _, id := range f.Names {
|
|
check(id, "struct field", w)
|
|
}
|
|
}
|
|
}
|
|
return w
|
|
}
|