1
0
mirror of https://github.com/mgechev/revive.git synced 2025-10-30 23:37:49 +02:00

refactor: move functions out from utils.go (#1139)

This commit is contained in:
Oleksandr Redko
2024-11-19 23:58:14 +02:00
committed by GitHub
parent 1425e2f44c
commit 0e8acea6ce
6 changed files with 62 additions and 69 deletions

View File

@@ -73,3 +73,7 @@ func (*BlankImportsRule) fileHasValidEmbedComment(fileAst *ast.File) bool {
return false
}
// isBlank returns whether id is the blank identifier "_".
// If id == nil, the answer is false.
func isBlank(id *ast.Ident) bool { return id != nil && id.Name == "_" }

View File

@@ -70,3 +70,23 @@ func (w lintBoolLiteral) addFailure(node ast.Node, msg, cat string) {
Failure: msg,
})
}
// isBoolOp returns true if the given token corresponds to a bool operator.
func isBoolOp(t token.Token) bool {
switch t {
case token.LAND, token.LOR, token.EQL, token.NEQ:
return true
}
return false
}
func isExprABooleanLit(n ast.Node) (lexeme string, ok bool) {
oper, ok := n.(*ast.Ident)
if !ok {
return "", false
}
return oper.Name, (oper.Name == "true" || oper.Name == "false")
}

View File

@@ -53,6 +53,15 @@ func (dc *disabledChecks) isDisabled(checkName string) bool {
}
}
var commonMethods = map[string]bool{
"Error": true,
"Read": true,
"ServeHTTP": true,
"String": true,
"Write": true,
"Unwrap": true,
}
// ExportedRule lints given else constructs.
type ExportedRule struct {
stuttersMsg string

View File

@@ -6,31 +6,11 @@ import (
"go/ast"
"go/printer"
"go/token"
"go/types"
"regexp"
"strings"
"github.com/mgechev/revive/lint"
)
// isBlank returns whether id is the blank identifier "_".
// If id == nil, the answer is false.
func isBlank(id *ast.Ident) bool { return id != nil && id.Name == "_" }
var commonMethods = map[string]bool{
"Error": true,
"Read": true,
"ServeHTTP": true,
"String": true,
"Write": true,
"Unwrap": true,
}
var knownNameExceptions = map[string]bool{
"LastInsertId": true, // must match database/sql
"kWh": true,
}
func isCgoExported(f *ast.FuncDecl) bool {
if f.Recv != nil || f.Doc == nil {
return false
@@ -45,34 +25,11 @@ func isCgoExported(f *ast.FuncDecl) bool {
return false
}
var allCapsRE = regexp.MustCompile(`^[A-Z0-9_]+$`)
func isIdent(expr ast.Expr, ident string) bool {
id, ok := expr.(*ast.Ident)
return ok && id.Name == ident
}
var zeroLiteral = map[string]bool{
"false": true, // bool
// runes
`'\x00'`: true,
`'\000'`: true,
// strings
`""`: true,
"``": true,
// numerics
"0": true,
"0.": true,
"0.0": true,
"0i": true,
}
func validType(t types.Type) bool {
return t != nil &&
t != types.Typ[types.Invalid] &&
!strings.Contains(t.String(), "invalid type") // good but not foolproof
}
// isPkgDot checks if the expression is <pkg>.<name>
func isPkgDot(expr ast.Expr, pkg, name string) bool {
sel, ok := expr.(*ast.SelectorExpr)
@@ -125,32 +82,6 @@ func (p picker) Visit(node ast.Node) ast.Visitor {
return p
}
// isBoolOp returns true if the given token corresponds to
// a bool operator
func isBoolOp(t token.Token) bool {
switch t {
case token.LAND, token.LOR, token.EQL, token.NEQ:
return true
}
return false
}
const (
trueName = "true"
falseName = "false"
)
func isExprABooleanLit(n ast.Node) (lexeme string, ok bool) {
oper, ok := n.(*ast.Ident)
if !ok {
return "", false
}
return oper.Name, (oper.Name == trueName || oper.Name == falseName)
}
// gofmt returns a string representation of an AST subtree.
func gofmt(x any) string {
buf := bytes.Buffer{}

View File

@@ -5,10 +5,26 @@ import (
"go/ast"
"go/token"
"go/types"
"strings"
"github.com/mgechev/revive/lint"
)
var zeroLiteral = map[string]bool{
"false": true, // bool
// runes
`'\x00'`: true,
`'\000'`: true,
// strings
`""`: true,
"``": true,
// numerics
"0": true,
"0.": true,
"0.0": true,
"0i": true,
}
// VarDeclarationsRule lints given else constructs.
type VarDeclarationsRule struct{}
@@ -120,3 +136,9 @@ func (w *lintVarDeclarations) Visit(node ast.Node) ast.Visitor {
}
return w
}
func validType(t types.Type) bool {
return t != nil &&
t != types.Typ[types.Invalid] &&
!strings.Contains(t.String(), "invalid type") // good but not foolproof
}

View File

@@ -13,9 +13,16 @@ import (
var anyCapsRE = regexp.MustCompile(`[A-Z]`)
var allCapsRE = regexp.MustCompile(`^[A-Z0-9_]+$`)
// regexp for constant names like `SOME_CONST`, `SOME_CONST_2`, `X123_3`, `_SOME_PRIVATE_CONST` (#851, #865)
var upperCaseConstRE = regexp.MustCompile(`^_?[A-Z][A-Z\d]*(_[A-Z\d]+)*$`)
var knownNameExceptions = map[string]bool{
"LastInsertId": true, // must match database/sql
"kWh": true,
}
// VarNamingRule lints given else constructs.
type VarNamingRule struct {
allowList []string