Skip to content

Commit

Permalink
ruleguard: use parseError.error field for the returned error (#220)
Browse files Browse the repository at this point in the history
So we actually return an `*ImportError` instead of `*parseError`
which can't be examined on the caller side.
  • Loading branch information
quasilyte authored Mar 31, 2021
1 parent bce90d2 commit ad7bed7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ruleguard/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (p *rulesParser) parseRuleGroup(f *ast.FuncDecl) (err error) {
return
}
if parseErr, ok := rv.(parseError); ok {
err = parseErr
err = parseErr.error
return
}
panic(rv) // not our panic
Expand Down
24 changes: 24 additions & 0 deletions ruleguard/ruleguard_error_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
package ruleguard

import (
"errors"
"fmt"
"go/token"
"strings"
"testing"
)

func TestImportError(t *testing.T) {
src := `
package gorules
import "github.com/quasilyte/go-ruleguard/dsl"
func badLock(m dsl.Matcher) {
m.Import("foo/nonexisting")
m.Match("$x").Where(m["x"].Type.Implements("nonexisting.Iface")).Report("ok")
}
`
e := NewEngine()
ctx := &ParseContext{
Fset: token.NewFileSet(),
}
err := e.Load(ctx, "rules.go", strings.NewReader(src))
if err == nil {
t.Fatal("expected an error, got none")
}
var importError *ImportError
if !errors.As(err, &importError) {
t.Fatal("got import that is not ImportError")
}
}

func TestParseFilterFuncError(t *testing.T) {
type testCase struct {
src string
Expand Down

0 comments on commit ad7bed7

Please sign in to comment.