internal/imports: use a regexp instead
MODIFY
internal/imports/import.go
internal/imports/import.go
@@ -13,6 +13,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
+ "regexp"
"strings"
)
@@ -21,8 +22,13 @@ import (
// If Check encounters multiple files importing deprecated imports, the
// first error is returned to the user.
func CheckBlacklisted(dir string, blacklist []string) error {
+ list, err := str2RE(blacklist)
+ if err != nil {
+ return err
+ }
+
var files []string
- err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
+ err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
switch {
case info.IsDir():
switch info.Name() {
@@ -43,7 +49,7 @@ func CheckBlacklisted(dir string, blacklist []string) error {
fset := token.NewFileSet()
for _, fname := range files {
- e := process(fname, fset, blacklist)
+ e := process(fname, fset, list)
if e != nil {
if err == nil {
err = e
@@ -53,7 +59,7 @@ func CheckBlacklisted(dir string, blacklist []string) error {
return err
}
-func process(fname string, fset *token.FileSet, blacklist []string) error {
+func process(fname string, fset *token.FileSet, blacklist []*regexp.Regexp) error {
src, err := ioutil.ReadFile(fname)
if err != nil {
return err
@@ -61,7 +67,7 @@ func process(fname string, fset *token.FileSet, blacklist []string) error {
return checkImports(fset, src, fname, blacklist)
}
-func checkImports(fset *token.FileSet, src []byte, fname string, blacklist []string) error {
+func checkImports(fset *token.FileSet, src []byte, fname string, blacklist []*regexp.Regexp) error {
f, err := parser.ParseFile(fset, fname, src, parser.ImportsOnly)
if err != nil {
return err
@@ -80,15 +86,29 @@ func checkImports(fset *token.FileSet, src []byte, fname string, blacklist []str
return nil
}
-func blacklisted(path string, blacklist []string) bool {
+func blacklisted(path string, blacklist []*regexp.Regexp) bool {
for _, v := range blacklist {
- if strings.HasPrefix(path, v) {
+ if v.MatchString(path) {
return true
}
}
return false
}
+func str2RE(vs []string) ([]*regexp.Regexp, error) {
+ var (
+ err error
+ o = make([]*regexp.Regexp, len(vs))
+ )
+ for i, v := range vs {
+ o[i], err = regexp.Compile(v)
+ if err != nil {
+ return nil, err
+ }
+ }
+ return o, nil
+}
+
// Error stores information about a deprecated import in a Go file.
type Error struct {
File string

MODIFY
internal/imports/imports_test.go
internal/imports/imports_test.go
@@ -15,17 +15,32 @@ import (
var (
blacklist = []string{
- "github.com/gonum/", // prefer gonum.org/v1/gonum
+ "github.com/gonum/.*", // prefer gonum.org/v1/gonum
"math/rand", // prefer golang.org/x/exp/rand
}
)
func TestCheck(t *testing.T) {
+ blacklist, err := str2RE(blacklist)
+ if err != nil {
+ t.Fatal(err)
+ }
fset := token.NewFileSet()
for _, tc := range []struct {
pkg string
err error
}{
+ {
+ pkg: "math/rand",
+ err: Error{
+ File: "file.go",
+ Imports: []string{"math/rand"},
+ },
+ },
+ {
+ pkg: "math",
+ err: nil,
+ },
{
pkg: "github.com/gonum/",
err: Error{

internal/imports: use a regexp instead