mirror of
https://github.com/mgechev/revive.git
synced 2025-07-17 01:12:27 +02:00
refactor (rule/function-length): replace AST walker by iteration over declarations (#1166)
This commit is contained in:
@ -28,17 +28,40 @@ func (r *FunctionLength) Apply(file *lint.File, arguments lint.Arguments) []lint
|
|||||||
r.configureOnce.Do(func() { r.configure(arguments) })
|
r.configureOnce.Do(func() { r.configure(arguments) })
|
||||||
|
|
||||||
var failures []lint.Failure
|
var failures []lint.Failure
|
||||||
|
for _, decl := range file.AST.Decls {
|
||||||
walker := lintFuncLength{
|
funcDecl, ok := decl.(*ast.FuncDecl)
|
||||||
file: file,
|
if !ok {
|
||||||
maxStmt: r.maxStmt,
|
continue
|
||||||
maxLines: r.maxLines,
|
|
||||||
onFailure: func(failure lint.Failure) {
|
|
||||||
failures = append(failures, failure)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ast.Walk(walker, file.AST)
|
body := funcDecl.Body
|
||||||
|
emptyBody := body == nil || len(body.List) == 0
|
||||||
|
if emptyBody {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.maxStmt > 0 {
|
||||||
|
stmtCount := r.countStmts(body.List)
|
||||||
|
if stmtCount > r.maxStmt {
|
||||||
|
failures = append(failures, lint.Failure{
|
||||||
|
Confidence: 1,
|
||||||
|
Failure: fmt.Sprintf("maximum number of statements per function exceeded; max %d but got %d", r.maxStmt, stmtCount),
|
||||||
|
Node: funcDecl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.maxLines > 0 {
|
||||||
|
lineCount := r.countLines(body, file)
|
||||||
|
if lineCount > r.maxLines {
|
||||||
|
failures = append(failures, lint.Failure{
|
||||||
|
Confidence: 1,
|
||||||
|
Failure: fmt.Sprintf("maximum number of lines per function exceeded; max %d but got %d", r.maxLines, lineCount),
|
||||||
|
Node: funcDecl,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return failures
|
return failures
|
||||||
}
|
}
|
||||||
@ -80,79 +103,35 @@ func (*FunctionLength) parseArguments(arguments lint.Arguments) (maxStmt, maxLin
|
|||||||
return maxStmt, maxLines
|
return maxStmt, maxLines
|
||||||
}
|
}
|
||||||
|
|
||||||
type lintFuncLength struct {
|
func (*FunctionLength) countLines(b *ast.BlockStmt, file *lint.File) int {
|
||||||
file *lint.File
|
return file.ToPosition(b.End()).Line - file.ToPosition(b.Pos()).Line - 1
|
||||||
maxStmt int
|
|
||||||
maxLines int
|
|
||||||
onFailure func(lint.Failure)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w lintFuncLength) Visit(n ast.Node) ast.Visitor {
|
func (r *FunctionLength) countStmts(b []ast.Stmt) int {
|
||||||
node, ok := n.(*ast.FuncDecl)
|
|
||||||
if !ok {
|
|
||||||
return w
|
|
||||||
}
|
|
||||||
|
|
||||||
body := node.Body
|
|
||||||
emptyBody := body == nil || len(node.Body.List) == 0
|
|
||||||
if emptyBody {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.maxStmt > 0 {
|
|
||||||
stmtCount := w.countStmts(node.Body.List)
|
|
||||||
if stmtCount > w.maxStmt {
|
|
||||||
w.onFailure(lint.Failure{
|
|
||||||
Confidence: 1,
|
|
||||||
Failure: fmt.Sprintf("maximum number of statements per function exceeded; max %d but got %d", w.maxStmt, stmtCount),
|
|
||||||
Node: node,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if w.maxLines > 0 {
|
|
||||||
lineCount := w.countLines(node.Body)
|
|
||||||
if lineCount > w.maxLines {
|
|
||||||
w.onFailure(lint.Failure{
|
|
||||||
Confidence: 1,
|
|
||||||
Failure: fmt.Sprintf("maximum number of lines per function exceeded; max %d but got %d", w.maxLines, lineCount),
|
|
||||||
Node: node,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w lintFuncLength) countLines(b *ast.BlockStmt) int {
|
|
||||||
return w.file.ToPosition(b.End()).Line - w.file.ToPosition(b.Pos()).Line - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w lintFuncLength) countStmts(b []ast.Stmt) int {
|
|
||||||
count := 0
|
count := 0
|
||||||
for _, s := range b {
|
for _, s := range b {
|
||||||
switch stmt := s.(type) {
|
switch stmt := s.(type) {
|
||||||
case *ast.BlockStmt:
|
case *ast.BlockStmt:
|
||||||
count += w.countStmts(stmt.List)
|
count += r.countStmts(stmt.List)
|
||||||
case *ast.IfStmt:
|
case *ast.IfStmt:
|
||||||
count += 1 + w.countBodyListStmts(stmt)
|
count += 1 + r.countBodyListStmts(stmt)
|
||||||
if stmt.Else != nil {
|
if stmt.Else != nil {
|
||||||
elseBody, ok := stmt.Else.(*ast.BlockStmt)
|
elseBody, ok := stmt.Else.(*ast.BlockStmt)
|
||||||
if ok {
|
if ok {
|
||||||
count += w.countStmts(elseBody.List)
|
count += r.countStmts(elseBody.List)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case *ast.ForStmt, *ast.RangeStmt,
|
case *ast.ForStmt, *ast.RangeStmt,
|
||||||
*ast.SwitchStmt, *ast.TypeSwitchStmt, *ast.SelectStmt:
|
*ast.SwitchStmt, *ast.TypeSwitchStmt, *ast.SelectStmt:
|
||||||
count += 1 + w.countBodyListStmts(stmt)
|
count += 1 + r.countBodyListStmts(stmt)
|
||||||
case *ast.CaseClause:
|
case *ast.CaseClause:
|
||||||
count += w.countStmts(stmt.Body)
|
count += r.countStmts(stmt.Body)
|
||||||
case *ast.AssignStmt:
|
case *ast.AssignStmt:
|
||||||
count += 1 + w.countFuncLitStmts(stmt.Rhs[0])
|
count += 1 + r.countFuncLitStmts(stmt.Rhs[0])
|
||||||
case *ast.GoStmt:
|
case *ast.GoStmt:
|
||||||
count += 1 + w.countFuncLitStmts(stmt.Call.Fun)
|
count += 1 + r.countFuncLitStmts(stmt.Call.Fun)
|
||||||
case *ast.DeferStmt:
|
case *ast.DeferStmt:
|
||||||
count += 1 + w.countFuncLitStmts(stmt.Call.Fun)
|
count += 1 + r.countFuncLitStmts(stmt.Call.Fun)
|
||||||
default:
|
default:
|
||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
@ -161,14 +140,15 @@ func (w lintFuncLength) countStmts(b []ast.Stmt) int {
|
|||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w lintFuncLength) countFuncLitStmts(stmt ast.Expr) int {
|
func (r *FunctionLength) countFuncLitStmts(stmt ast.Expr) int {
|
||||||
if block, ok := stmt.(*ast.FuncLit); ok {
|
if block, ok := stmt.(*ast.FuncLit); ok {
|
||||||
return w.countStmts(block.Body.List)
|
return r.countStmts(block.Body.List)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w lintFuncLength) countBodyListStmts(t any) int {
|
func (r *FunctionLength) countBodyListStmts(t any) int {
|
||||||
i := reflect.ValueOf(t).Elem().FieldByName(`Body`).Elem().FieldByName(`List`).Interface()
|
i := reflect.ValueOf(t).Elem().FieldByName(`Body`).Elem().FieldByName(`List`).Interface()
|
||||||
return w.countStmts(i.([]ast.Stmt))
|
return r.countStmts(i.([]ast.Stmt))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user