Skip to content

Commit

Permalink
fix: handle duplicate var names in assignment (#1558)
Browse files Browse the repository at this point in the history
* fix: handle duplicate var names in assignment

* fix: update snapshots
  • Loading branch information
elsapet committed Apr 9, 2024
1 parent e267aa9 commit b27b488
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
19 changes: 12 additions & 7 deletions internal/languages/golang/analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ func (analyzer *analyzer) analyzeShortVarDeclaration(node *sitter.Node, visitChi
left := node.ChildByFieldName("left")
right := node.ChildByFieldName("right")

err := visitChildren()
if err != nil {
return err
}

for _, child := range analyzer.builder.ChildrenFor(left) {
if !slices.Contains([]string{"_", ",", "err"}, analyzer.builder.ContentFor(child)) {
analyzer.scope.Declare(analyzer.builder.ContentFor(child), child)
Expand All @@ -161,22 +166,22 @@ func (analyzer *analyzer) analyzeShortVarDeclaration(node *sitter.Node, visitChi
analyzer.lookupVariable(child)
}

err := visitChildren()

return err
return nil
}

// var a, b string
func (analyzer *analyzer) analyzeVarSpecDeclaration(node *sitter.Node, visitChildren func() error) error {
err := visitChildren()
if err != nil {
return err
}

for _, child := range analyzer.builder.ChildrenFor(node) {
if child.Type() == "identifier" {
analyzer.scope.Declare(analyzer.builder.ContentFor(child), child)
}
}

err := visitChildren()

return err
return nil
}

// foo(1, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ children:
id: 22
range: 6:2 - 6:6
content: user
alias_of:
- 20
- type: '":="'
id: 23
range: 6:7 - 6:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ children:
id: 41
range: 11:2 - 11:3
content: s
alias_of:
- 39
- type: '":="'
id: 42
range: 11:4 - 11:6
Expand Down Expand Up @@ -363,8 +361,6 @@ children:
id: 72
range: 15:2 - 15:4
content: s2
alias_of:
- 70
- type: '":="'
id: 73
range: 15:5 - 15:7
Expand Down Expand Up @@ -578,8 +574,6 @@ children:
id: 114
range: 20:2 - 20:4
content: s3
alias_of:
- 112
- type: '":="'
id: 115
range: 20:5 - 20:7
Expand Down

0 comments on commit b27b488

Please sign in to comment.