Skip to content

Commit

Permalink
Refactor ast inspection function into a closure (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jun 9, 2024
1 parent 1241dcb commit eeeccce
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@ func Debug(value any) {
return
}

ast.Inspect(f, func(node ast.Node) bool {
ast.Inspect(f, findAndDebug(fset, line, value))
}

// findAndDebug is an AST traversal function to be passed to [go/ast.Inspect]
// and will find calls to debug.Debug, and print the debugging information.
//
// It returns a closure that can be passed directly to [go/ast.Inspect].
func findAndDebug(fset *token.FileSet, line int, value any) func(node ast.Node) bool {
return func(node ast.Node) bool {
if node == nil {
return false
}
Expand Down Expand Up @@ -83,7 +91,7 @@ func Debug(value any) {
}

return false // Found it
})
}
}

// isDebugCall takes an arbitrary AST expression and determines if it
Expand Down

0 comments on commit eeeccce

Please sign in to comment.