2018-01-22 04:04:41 +02:00
|
|
|
package rule
|
2017-11-27 05:28:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
|
|
|
"go/token"
|
2023-02-27 20:56:30 +02:00
|
|
|
"regexp"
|
2017-11-27 05:28:18 +02:00
|
|
|
"strings"
|
2022-04-10 09:06:59 +02:00
|
|
|
"sync"
|
2017-11-27 05:28:18 +02:00
|
|
|
|
2018-01-25 01:44:03 +02:00
|
|
|
"github.com/mgechev/revive/lint"
|
2017-11-27 05:28:18 +02:00
|
|
|
)
|
|
|
|
|
2023-02-27 20:56:30 +02:00
|
|
|
var anyCapsRE = regexp.MustCompile(`[A-Z]`)
|
|
|
|
|
2018-05-27 06:28:31 +02:00
|
|
|
// VarNamingRule lints given else constructs.
|
2021-10-17 20:34:48 +02:00
|
|
|
type VarNamingRule struct {
|
|
|
|
configured bool
|
|
|
|
whitelist []string
|
|
|
|
blacklist []string
|
2022-04-10 09:06:59 +02:00
|
|
|
sync.Mutex
|
2021-10-17 20:34:48 +02:00
|
|
|
}
|
2017-11-27 05:28:18 +02:00
|
|
|
|
2022-04-10 09:06:59 +02:00
|
|
|
func (r *VarNamingRule) configure(arguments lint.Arguments) {
|
|
|
|
r.Lock()
|
2021-10-17 20:34:48 +02:00
|
|
|
if !r.configured {
|
|
|
|
if len(arguments) >= 1 {
|
|
|
|
r.whitelist = getList(arguments[0], "whitelist")
|
|
|
|
}
|
2018-09-15 23:33:28 +02:00
|
|
|
|
2021-10-17 20:34:48 +02:00
|
|
|
if len(arguments) >= 2 {
|
|
|
|
r.blacklist = getList(arguments[1], "blacklist")
|
|
|
|
}
|
|
|
|
r.configured = true
|
2018-09-15 23:33:28 +02:00
|
|
|
}
|
2022-04-10 09:06:59 +02:00
|
|
|
r.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
|
|
|
func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
|
|
|
r.configure(arguments)
|
|
|
|
|
|
|
|
var failures []lint.Failure
|
2018-09-15 23:33:28 +02:00
|
|
|
|
2018-01-22 04:48:51 +02:00
|
|
|
fileAst := file.AST
|
2022-04-10 09:06:59 +02:00
|
|
|
|
2017-11-27 05:28:18 +02:00
|
|
|
walker := lintNames{
|
2018-09-15 23:33:28 +02:00
|
|
|
file: file,
|
|
|
|
fileAst: fileAst,
|
2021-10-17 20:34:48 +02:00
|
|
|
whitelist: r.whitelist,
|
|
|
|
blacklist: r.blacklist,
|
2018-01-25 01:44:03 +02:00
|
|
|
onFailure: func(failure lint.Failure) {
|
2017-11-27 05:28:18 +02:00
|
|
|
failures = append(failures, failure)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-01-25 01:17:19 +02:00
|
|
|
// Package names need slightly different handling than other names.
|
|
|
|
if strings.Contains(walker.fileAst.Name.Name, "_") && !strings.HasSuffix(walker.fileAst.Name.Name, "_test") {
|
2018-01-25 01:44:03 +02:00
|
|
|
walker.onFailure(lint.Failure{
|
2018-01-25 01:17:19 +02:00
|
|
|
Failure: "don't use an underscore in package name",
|
|
|
|
Confidence: 1,
|
2022-05-23 12:30:25 +02:00
|
|
|
Node: walker.fileAst.Name,
|
2018-01-25 01:17:19 +02:00
|
|
|
Category: "naming",
|
|
|
|
})
|
|
|
|
}
|
2023-02-27 20:56:30 +02:00
|
|
|
if anyCapsRE.MatchString(walker.fileAst.Name.Name) {
|
|
|
|
walker.onFailure(lint.Failure{
|
|
|
|
Failure: fmt.Sprintf("don't use MixedCaps in package name; %s should be %s", walker.fileAst.Name.Name, strings.ToLower(walker.fileAst.Name.Name)),
|
|
|
|
Confidence: 1,
|
|
|
|
Node: walker.fileAst.Name,
|
|
|
|
Category: "naming",
|
|
|
|
})
|
|
|
|
}
|
2018-01-25 01:17:19 +02:00
|
|
|
|
2017-11-27 05:28:18 +02:00
|
|
|
ast.Walk(&walker, fileAst)
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*VarNamingRule) Name() string {
|
2018-05-27 06:28:31 +02:00
|
|
|
return "var-naming"
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:17:19 +02:00
|
|
|
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, "_") {
|
2018-01-25 01:44:03 +02:00
|
|
|
w.onFailure(lint.Failure{
|
2018-01-25 01:17:19 +02:00
|
|
|
Failure: "don't use ALL_CAPS in Go names; use CamelCase",
|
|
|
|
Confidence: 0.8,
|
|
|
|
Node: id,
|
|
|
|
Category: "naming",
|
|
|
|
})
|
|
|
|
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:]
|
2018-01-25 01:44:03 +02:00
|
|
|
w.onFailure(lint.Failure{
|
2018-01-25 01:17:19 +02:00
|
|
|
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",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-15 23:33:28 +02:00
|
|
|
should := lint.Name(id.Name, w.whitelist, w.blacklist)
|
2018-01-25 01:17:19 +02:00
|
|
|
if id.Name == should {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(id.Name) > 2 && strings.Contains(id.Name[1:], "_") {
|
2018-01-25 01:44:03 +02:00
|
|
|
w.onFailure(lint.Failure{
|
2018-01-25 01:17:19 +02:00
|
|
|
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",
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2018-01-25 01:44:03 +02:00
|
|
|
w.onFailure(lint.Failure{
|
2018-01-25 01:17:19 +02:00
|
|
|
Failure: fmt.Sprintf("%s %s should be %s", thing, id.Name, should),
|
|
|
|
Confidence: 0.8,
|
|
|
|
Node: id,
|
|
|
|
Category: "naming",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-27 05:28:18 +02:00
|
|
|
type lintNames struct {
|
2022-04-10 11:55:13 +02:00
|
|
|
file *lint.File
|
|
|
|
fileAst *ast.File
|
|
|
|
onFailure func(lint.Failure)
|
|
|
|
whitelist []string
|
|
|
|
blacklist []string
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2018-01-25 01:17:19 +02:00
|
|
|
check(id, "var", w)
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case *ast.FuncDecl:
|
2022-01-18 16:26:29 +02:00
|
|
|
funcName := v.Name.Name
|
|
|
|
if w.file.IsTest() &&
|
|
|
|
(strings.HasPrefix(funcName, "Example") ||
|
|
|
|
strings.HasPrefix(funcName, "Test") ||
|
|
|
|
strings.HasPrefix(funcName, "Benchmark") ||
|
|
|
|
strings.HasPrefix(funcName, "Fuzz")) {
|
2017-11-27 05:28:18 +02:00
|
|
|
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) {
|
2018-01-25 01:17:19 +02:00
|
|
|
check(v.Name, thing, w)
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-25 01:17:19 +02:00
|
|
|
checkList(v.Type.Params, thing+" parameter", w)
|
|
|
|
checkList(v.Type.Results, thing+" result", w)
|
2017-11-27 05:28:18 +02:00
|
|
|
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:
|
2018-01-25 01:17:19 +02:00
|
|
|
check(s.Name, thing, w)
|
2017-11-27 05:28:18 +02:00
|
|
|
case *ast.ValueSpec:
|
|
|
|
for _, id := range s.Names {
|
2018-01-25 01:17:19 +02:00
|
|
|
check(id, thing, w)
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *ast.InterfaceType:
|
|
|
|
// Do not check interface method names.
|
2021-10-14 20:56:29 +02:00
|
|
|
// They are often constrained by the method names of concrete types.
|
2017-11-27 05:28:18 +02:00
|
|
|
for _, x := range v.Methods.List {
|
|
|
|
ft, ok := x.Type.(*ast.FuncType)
|
|
|
|
if !ok { // might be an embedded interface name
|
|
|
|
continue
|
|
|
|
}
|
2018-01-25 01:17:19 +02:00
|
|
|
checkList(ft.Params, "interface method parameter", w)
|
|
|
|
checkList(ft.Results, "interface method result", w)
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
case *ast.RangeStmt:
|
|
|
|
if v.Tok == token.ASSIGN {
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
if id, ok := v.Key.(*ast.Ident); ok {
|
2018-01-25 01:17:19 +02:00
|
|
|
check(id, "range var", w)
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
if id, ok := v.Value.(*ast.Ident); ok {
|
2018-01-25 01:17:19 +02:00
|
|
|
check(id, "range var", w)
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
case *ast.StructType:
|
|
|
|
for _, f := range v.Fields.List {
|
|
|
|
for _, id := range f.Names {
|
2018-01-25 01:17:19 +02:00
|
|
|
check(id, "struct field", w)
|
2017-11-27 05:28:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
2018-09-15 23:33:28 +02:00
|
|
|
|
|
|
|
func getList(arg interface{}, argName string) []string {
|
|
|
|
temp, ok := arg.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("Invalid argument to the var-naming rule. Expecting a %s of type slice with initialisms, got %T", argName, arg))
|
|
|
|
}
|
|
|
|
var list []string
|
|
|
|
for _, v := range temp {
|
|
|
|
if val, ok := v.(string); ok {
|
|
|
|
list = append(list, val)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("Invalid %s values of the var-naming rule. Expecting slice of strings but got element of type %T", val, arg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|