Skip to content

Commit

Permalink
Add *ast.KeyValueExpr as a source for RHS varaibles
Browse files Browse the repository at this point in the history
Key-value expressions can use variables both as keys and values so we
must parse them to check for assigned and used variables.

Fixes #106
  • Loading branch information
bombsimon committed Jun 17, 2021
1 parent bbddf87 commit a7c179d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,8 +845,7 @@ func (p *Processor) findRHS(node ast.Node) []string {
case *ast.BasicLit, *ast.SelectStmt, *ast.ChanType,
*ast.LabeledStmt, *ast.DeclStmt, *ast.BranchStmt,
*ast.TypeSpec, *ast.ArrayType, *ast.CaseClause,
*ast.CommClause, *ast.KeyValueExpr, *ast.MapType,
*ast.FuncLit:
*ast.CommClause, *ast.MapType, *ast.FuncLit:
// Nothing to add to RHS
case *ast.Ident:
return []string{t.Name}
Expand Down Expand Up @@ -905,6 +904,9 @@ func (p *Processor) findRHS(node ast.Node) []string {
rhs = append(rhs, p.findRHS(t.X)...)
rhs = append(rhs, p.findRHS(t.Low)...)
rhs = append(rhs, p.findRHS(t.High)...)
case *ast.KeyValueExpr:
rhs = p.findRHS(t.Key)
rhs = append(rhs, p.findRHS(t.Value)...)
default:
if x, ok := maybeX(t); ok {
return p.findRHS(x)
Expand Down
14 changes: 14 additions & 0 deletions wsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1921,6 +1921,20 @@ func TestWithConfig(t *testing.T) {
reasonShortDeclNotExclusive,
},
},
{
description: "key value pairs can use variables",
code: []byte(`package main
func main() {
// Network constants
someData := GetSomeData()
log.WithFields(log.Fields{
"data1": someData.One,
"data2": someData.Two,
"data3": someData.Three,
}).Debug("Loaded Network Constants")
}`),
},
}

for _, tc := range cases {
Expand Down

0 comments on commit a7c179d

Please sign in to comment.