Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard: fix interpolation of vars with common prefix #123

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions ruleguard/ruleguard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package ruleguard

import (
"go/ast"
"go/token"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestRenderMessage(t *testing.T) {
tests := []struct {
msg string
want string
vars []string
}{
{
`$x`,
`xvar`,
[]string{`x`},
},
{
`$x$x`,
`xvarxvar`,
[]string{`x`},
},
{
`$f $foo`,
`fvar foovar`,
[]string{`f`, `foo`},
},
{
`$foo $f`,
`foovar fvar`,
[]string{`f`, `foo`},
},
{
`$foo $f`,
`foovar fvar`,
[]string{`foo`, `f`},
},
{
`$foo $foo $f`,
`foovar foovar fvar`,
[]string{`foo`, `f`},
},
{
`$foo$f`,
`foovarfvar`,
[]string{`foo`, `f`},
},
{
`$foo($f) + $f.$foo`,
`foovar(fvar) + fvar.foovar`,
[]string{`foo`, `f`},
},

// Do we care about finding a proper variable border?
{
`$fooo`,
`foovaro`,
[]string{`foo`},
},

// Unknown $-expressions are not interpolated.
{
`$nonexisting`,
`$nonexisting`,
[]string{`x`},
},

// Double dollar interpolation.
{
`$$`,
`dd`,
[]string{`x`},
},
{
`$$[$x]`,
`dd[xvar]`,
[]string{`x`},
},
}

var rr rulesRunner
rr.ctx = &Context{
Fset: token.NewFileSet(),
}
for _, test := range tests {
nodes := make(map[string]ast.Node, len(test.vars))
for _, v := range test.vars {
nodes[v] = &ast.Ident{Name: v + "var"}
}

have := rr.renderMessage(test.msg, &ast.Ident{Name: "dd"}, nodes, false)
if diff := cmp.Diff(have, test.want); diff != "" {
t.Errorf("render %s %v:\n(+want -have)\n%s", test.msg, test.vars, diff)
}
}
}
16 changes: 15 additions & 1 deletion ruleguard/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type rulesRunner struct {
filename string
src []byte

// A slice that is used to do a nodes keys sorting in renderMessage().
sortScratch []string

fileFilterParams fileFilterParams
nodeFilterParams nodeFilterParams
}
Expand All @@ -35,6 +38,7 @@ func newRulesRunner(ctx *Context, rules *GoRuleSet) *rulesRunner {
nodeFilterParams: nodeFilterParams{
ctx: ctx,
},
sortScratch: make([]string, 0, 8),
}
rr.nodeFilterParams.nodeText = rr.nodeText
return rr
Expand Down Expand Up @@ -224,7 +228,17 @@ func (rr *rulesRunner) renderMessage(msg string, n ast.Node, nodes map[string]as
if len(nodes) == 0 {
return msg
}
for name, n := range nodes {

rr.sortScratch = rr.sortScratch[:0]
for name := range nodes {
rr.sortScratch = append(rr.sortScratch, name)
}
sort.Slice(rr.sortScratch, func(i, j int) bool {
return len(rr.sortScratch[i]) > len(rr.sortScratch[j])
})

for _, name := range rr.sortScratch {
n := nodes[name]
key := "$" + name
if !strings.Contains(msg, key) {
continue
Expand Down