1
1
mirror of https://github.com/go-gitea/gitea synced 2024-12-30 12:34:28 +00:00
gitea/vendor/github.com/mgechev/revive/rule/unexported-naming.go
techknowlogick 1c3278c2fa
update revive lint to latest commit (#12921)
* update revive lint to latest commit

* make fmt

* change import
2020-09-22 20:02:16 +03:00

116 lines
2.1 KiB
Go
Vendored

package rule
import (
"fmt"
"go/ast"
"go/token"
"github.com/mgechev/revive/lint"
)
// UnexportedNamingRule lints wrongly named unexported symbols.
type UnexportedNamingRule struct{}
// Apply applies the rule to given file.
func (r *UnexportedNamingRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
ba := &unexportablenamingLinter{onFailure}
ast.Walk(ba, file.AST)
return failures
}
// Name returns the rule name.
func (r *UnexportedNamingRule) Name() string {
return "unexported-naming"
}
type unexportablenamingLinter struct {
onFailure func(lint.Failure)
}
func (unl unexportablenamingLinter) Visit(node ast.Node) ast.Visitor {
switch n := node.(type) {
case *ast.FuncDecl:
unl.lintFunction(n.Type, n.Body)
return nil
case *ast.FuncLit:
unl.lintFunction(n.Type, n.Body)
return nil
case *ast.AssignStmt:
if n.Tok != token.DEFINE {
return nil
}
ids := []*ast.Ident{}
for _, e := range n.Lhs {
id, ok := e.(*ast.Ident)
if !ok {
continue
}
ids = append(ids, id)
}
unl.lintIDs(ids)
case *ast.DeclStmt:
gd, ok := n.Decl.(*ast.GenDecl)
if !ok {
return nil
}
if len(gd.Specs) < 1 {
return nil
}
vs, ok := gd.Specs[0].(*ast.ValueSpec)
if !ok {
return nil
}
unl.lintIDs(vs.Names)
}
return unl
}
func (unl unexportablenamingLinter) lintFunction(ft *ast.FuncType, body *ast.BlockStmt) {
unl.lintFields(ft.Params)
unl.lintFields(ft.Results)
if body != nil {
ast.Walk(unl, body)
}
}
func (unl unexportablenamingLinter) lintFields(fields *ast.FieldList) {
if fields == nil {
return
}
ids := []*ast.Ident{}
for _, field := range fields.List {
ids = append(ids, field.Names...)
}
unl.lintIDs(ids)
}
func (unl unexportablenamingLinter) lintIDs(ids []*ast.Ident) {
for _, id := range ids {
if id.IsExported() {
unl.onFailure(lint.Failure{
Node: id,
Confidence: 1,
Category: "naming",
Failure: fmt.Sprintf("the symbol %s is local, its name should start with a lowercase letter", id.String()),
})
}
}
}