-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: rename blacklist to blocklist and whitelist to allowlist (#946
- Loading branch information
1 parent
af4f9ea
commit 9abe06a
Showing
9 changed files
with
159 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"sync" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// ImportsBlocklistRule lints given else constructs. | ||
type ImportsBlocklistRule struct { | ||
blocklist []*regexp.Regexp | ||
sync.Mutex | ||
} | ||
|
||
var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`) | ||
|
||
func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
if r.blocklist == nil { | ||
r.blocklist = make([]*regexp.Regexp, 0) | ||
|
||
for _, arg := range arguments { | ||
argStr, ok := arg.(string) | ||
if !ok { | ||
panic(fmt.Sprintf("Invalid argument to the imports-blocklist rule. Expecting a string, got %T", arg)) | ||
} | ||
regStr, err := regexp.Compile(fmt.Sprintf(`(?m)"%s"$`, replaceImportRegexp.ReplaceAllString(argStr, `(\W|\w)*`))) | ||
if err != nil { | ||
panic(fmt.Sprintf("Invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %v", argStr, err)) | ||
} | ||
r.blocklist = append(r.blocklist, regStr) | ||
} | ||
} | ||
} | ||
|
||
func (r *ImportsBlocklistRule) isBlocklisted(path string) bool { | ||
for _, regex := range r.blocklist { | ||
if regex.MatchString(path) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// Apply applies the rule to given file. | ||
func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { | ||
r.configure(arguments) | ||
|
||
var failures []lint.Failure | ||
|
||
for _, is := range file.AST.Imports { | ||
path := is.Path | ||
if path != nil && r.isBlocklisted(path.Value) { | ||
failures = append(failures, lint.Failure{ | ||
Confidence: 1, | ||
Failure: "should not use the following blocklisted import: " + path.Value, | ||
Node: is, | ||
Category: "imports", | ||
}) | ||
} | ||
} | ||
|
||
return failures | ||
} | ||
|
||
// Name returns the rule name. | ||
func (*ImportsBlocklistRule) Name() string { | ||
return "imports-blocklist" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestImportsBlocklistOriginal(t *testing.T) { | ||
args := []any{"crypto/md5", "crypto/sha1"} | ||
|
||
testRule(t, "imports-blocklist-original", &rule.ImportsBlocklistRule{}, &lint.RuleConfig{ | ||
Arguments: args, | ||
}) | ||
} | ||
|
||
func TestImportsBlocklist(t *testing.T) { | ||
args := []any{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"} | ||
|
||
testRule(t, "imports-blocklist", &rule.ImportsBlocklistRule{}, &lint.RuleConfig{ | ||
Arguments: args, | ||
}) | ||
} | ||
|
||
func BenchmarkImportsBlocklist(b *testing.B) { | ||
args := []any{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"} | ||
var t *testing.T | ||
for i := 0; i <= b.N; i++ { | ||
testRule(t, "imports-blocklist", &rule.ImportsBlocklistRule{}, &lint.RuleConfig{ | ||
Arguments: args, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package fixtures | ||
|
||
import ( | ||
"crypto/md5" // MATCH /should not use the following blocklisted import: "crypto/md5"/ | ||
"crypto/sha1" // MATCH /should not use the following blocklisted import: "crypto/sha1"/ | ||
"strings" | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package fixtures | ||
|
||
import ( | ||
"github.com/full/match" // MATCH /should not use the following blocklisted import: "github.com/full/match"/ | ||
"bithub.com/full/match" | ||
"github.com/full/matche" | ||
"wildcard/between" // MATCH /should not use the following blocklisted import: "wildcard/between"/ | ||
"wildcard/pkg1/between" // MATCH /should not use the following blocklisted import: "wildcard/pkg1/between"/ | ||
"wildcard/pkg1/pkg2/between" // MATCH /should not use the following blocklisted import: "wildcard/pkg1/pkg2/between"/ | ||
"wildcard/backward" // MATCH /should not use the following blocklisted import: "wildcard/backward"/ | ||
"wildcard/backward/pkg" // MATCH /should not use the following blocklisted import: "wildcard/backward/pkg"/ | ||
"wildcard/backward/pkg/pkg1" // MATCH /should not use the following blocklisted import: "wildcard/backward/pkg/pkg1"/ | ||
"wildcard/forward" // MATCH /should not use the following blocklisted import: "wildcard/forward"/ | ||
"pkg/wildcard/forward" // MATCH /should not use the following blocklisted import: "pkg/wildcard/forward"/ | ||
"pkg/pkg1/wildcard/forward" // MATCH /should not use the following blocklisted import: "pkg/pkg1/wildcard/forward"/ | ||
"full" // MATCH /should not use the following blocklisted import: "full"/ | ||
"github.com/partical/match/fully" | ||
"strings" | ||
) |