aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2023-05-18 03:57:48 +0200
committerTeddy Wing2023-05-18 03:57:48 +0200
commita0e54f27ebad9c46112a36dfed7b0d4b4e8da99b (patch)
tree4562d48ffb9037cd35eb49997acd3e3f05a84f80
parentc76dd021f7a3df0f524b8c9e8e710950a0b31fb6 (diff)
downloadgocapturedrefrace-a0e54f27ebad9c46112a36dfed7b0d4b4e8da99b.tar.bz2
findLocalVarDeclarations: Move AssignStmt handling to function
Match the `varDeclarations` function splitting this into a more self-contained block. Also don't return in the loop to ensure we look at all assignments in `assignStmt`.
-rw-r--r--gocapturedrefrace.go52
1 files changed, 30 insertions, 22 deletions
diff --git a/gocapturedrefrace.go b/gocapturedrefrace.go
index 5be3656..f53e4d3 100644
--- a/gocapturedrefrace.go
+++ b/gocapturedrefrace.go
@@ -156,28 +156,8 @@ func findLocalVarDeclarations(
func(node ast.Node) bool {
switch node := node.(type) {
case *ast.AssignStmt:
- // assignStmt, ok := node.(*ast.AssignStmt)
- // if !ok {
- // return true
- // }
- assignStmt := node
-
- if assignStmt.Tok != token.DEFINE {
- return true
- }
-
- for _, lhs := range assignStmt.Lhs {
- ident, ok := lhs.(*ast.Ident)
- if !ok {
- return true
- }
-
- if ident == nil {
- return true
- }
-
- declarations = append(declarations, ident)
- }
+ assignments := assignmentDefinitions(node)
+ declarations = append(declarations, assignments...)
case *ast.GenDecl:
decls := varDeclarations(node)
@@ -193,6 +173,34 @@ func findLocalVarDeclarations(
return declarations
}
+// assignmentDefinitions returns the identifiers corresponding to variable
+// assignments in assignStmt, or nil if assignStmt does not declare any
+// variables.
+func assignmentDefinitions(
+ assignStmt *ast.AssignStmt,
+) (assignments []*ast.Ident) {
+ assignments = []*ast.Ident{}
+
+ if assignStmt.Tok != token.DEFINE {
+ return nil
+ }
+
+ for _, lhs := range assignStmt.Lhs {
+ ident, ok := lhs.(*ast.Ident)
+ if !ok {
+ continue
+ }
+
+ if ident == nil {
+ continue
+ }
+
+ assignments = append(assignments, ident)
+ }
+
+ return assignments
+}
+
// varDeclarations returns the identifiers corresponding to variable
// declarations in decl, or nil if decl is not a variable declaration.
func varDeclarations(decl *ast.GenDecl) (declarations []*ast.Ident) {