Skip to content

Commit 9abe06a

Browse files
authored
refactor: rename blacklist to blocklist and whitelist to allowlist (#946)
* refactor: rename blacklist to blocklist and whitelist to allowlist
1 parent af4f9ea commit 9abe06a

9 files changed

+159
-24
lines changed

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ var allRules = append([]lint.Rule{
5656
&rule.ConstantLogicalExprRule{},
5757
&rule.BoolLiteralRule{},
5858
&rule.ImportsBlacklistRule{},
59+
&rule.ImportsBlocklistRule{},
5960
&rule.FunctionResultsLimitRule{},
6061
&rule.MaxPublicStructsRule{},
6162
&rule.RangeValInClosureRule{},

lint/utils.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
// Name returns a different name if it should be different.
9-
func Name(name string, whitelist, blacklist []string) (should string) {
9+
func Name(name string, allowlist, blocklist []string) (should string) {
1010
// Fast path for simple cases: "_" and all lowercase.
1111
if name == "_" {
1212
return name
@@ -57,12 +57,12 @@ func Name(name string, whitelist, blacklist []string) (should string) {
5757
// [w,i) is a word.
5858
word := string(runes[w:i])
5959
ignoreInitWarnings := map[string]bool{}
60-
for _, i := range whitelist {
60+
for _, i := range allowlist {
6161
ignoreInitWarnings[i] = true
6262
}
6363

6464
extraInits := map[string]bool{}
65-
for _, i := range blacklist {
65+
for _, i := range blocklist {
6666
extraInits[i] = true
6767
}
6868

rule/add-constant.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ const (
1818
kindSTRING = "STRING"
1919
)
2020

21-
type whiteList map[string]map[string]bool
21+
type allowList map[string]map[string]bool
2222

23-
func newWhiteList() whiteList {
23+
func newAllowList() allowList {
2424
return map[string]map[string]bool{kindINT: {}, kindFLOAT: {}, kindSTRING: {}}
2525
}
2626

27-
func (wl whiteList) add(kind, list string) {
27+
func (wl allowList) add(kind, list string) {
2828
elems := strings.Split(list, ",")
2929
for _, e := range elems {
3030
wl[kind][e] = true
@@ -33,7 +33,7 @@ func (wl whiteList) add(kind, list string) {
3333

3434
// AddConstantRule lints unused params in functions.
3535
type AddConstantRule struct {
36-
whiteList whiteList
36+
allowList allowList
3737
ignoreFunctions []*regexp.Regexp
3838
strLitLimit int
3939
sync.Mutex
@@ -53,7 +53,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin
5353
onFailure: onFailure,
5454
strLits: make(map[string]int),
5555
strLitLimit: r.strLitLimit,
56-
whiteLst: r.whiteList,
56+
allowList: r.allowList,
5757
ignoreFunctions: r.ignoreFunctions,
5858
structTags: make(map[*ast.BasicLit]struct{}),
5959
}
@@ -72,7 +72,7 @@ type lintAddConstantRule struct {
7272
onFailure func(lint.Failure)
7373
strLits map[string]int
7474
strLitLimit int
75-
whiteLst whiteList
75+
allowList allowList
7676
ignoreFunctions []*regexp.Regexp
7777
structTags map[*ast.BasicLit]struct{}
7878
}
@@ -155,7 +155,7 @@ func (w *lintAddConstantRule) isIgnoredFunc(fName string) bool {
155155
}
156156

157157
func (w *lintAddConstantRule) checkStrLit(n *ast.BasicLit) {
158-
if w.whiteLst[kindSTRING][n.Value] {
158+
if w.allowList[kindSTRING][n.Value] {
159159
return
160160
}
161161

@@ -175,7 +175,7 @@ func (w *lintAddConstantRule) checkStrLit(n *ast.BasicLit) {
175175
}
176176

177177
func (w *lintAddConstantRule) checkNumLit(kind string, n *ast.BasicLit) {
178-
if w.whiteLst[kind][n.Value] {
178+
if w.allowList[kind][n.Value] {
179179
return
180180
}
181181

@@ -196,9 +196,9 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) {
196196
r.Lock()
197197
defer r.Unlock()
198198

199-
if r.whiteList == nil {
199+
if r.allowList == nil {
200200
r.strLitLimit = defaultStrLitLimit
201-
r.whiteList = newWhiteList()
201+
r.allowList = newAllowList()
202202
if len(arguments) > 0 {
203203
args, ok := arguments[0].(map[string]any)
204204
if !ok {
@@ -223,7 +223,7 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) {
223223
if !ok {
224224
panic(fmt.Sprintf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v))
225225
}
226-
r.whiteList.add(kind, list)
226+
r.allowList.add(kind, list)
227227
case "maxLitCount":
228228
sl, ok := v.(string)
229229
if !ok {

rule/confusing-naming.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func checkMethodName(holder string, id *ast.Ident, w *lintConfusingNames) {
111111
pkgm.methods[holder] = make(map[string]*referenceMethod, 1)
112112
}
113113

114-
// update the black list
114+
// update the block list
115115
if pkgm.methods[holder] == nil {
116116
println("no entry for '", holder, "'")
117117
}

rule/imports-blocklist.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package rule
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"sync"
7+
8+
"github.com/mgechev/revive/lint"
9+
)
10+
11+
// ImportsBlocklistRule lints given else constructs.
12+
type ImportsBlocklistRule struct {
13+
blocklist []*regexp.Regexp
14+
sync.Mutex
15+
}
16+
17+
var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`)
18+
19+
func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) {
20+
r.Lock()
21+
defer r.Unlock()
22+
23+
if r.blocklist == nil {
24+
r.blocklist = make([]*regexp.Regexp, 0)
25+
26+
for _, arg := range arguments {
27+
argStr, ok := arg.(string)
28+
if !ok {
29+
panic(fmt.Sprintf("Invalid argument to the imports-blocklist rule. Expecting a string, got %T", arg))
30+
}
31+
regStr, err := regexp.Compile(fmt.Sprintf(`(?m)"%s"$`, replaceImportRegexp.ReplaceAllString(argStr, `(\W|\w)*`)))
32+
if err != nil {
33+
panic(fmt.Sprintf("Invalid argument to the imports-blocklist rule. Expecting %q to be a valid regular expression, got: %v", argStr, err))
34+
}
35+
r.blocklist = append(r.blocklist, regStr)
36+
}
37+
}
38+
}
39+
40+
func (r *ImportsBlocklistRule) isBlocklisted(path string) bool {
41+
for _, regex := range r.blocklist {
42+
if regex.MatchString(path) {
43+
return true
44+
}
45+
}
46+
return false
47+
}
48+
49+
// Apply applies the rule to given file.
50+
func (r *ImportsBlocklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
51+
r.configure(arguments)
52+
53+
var failures []lint.Failure
54+
55+
for _, is := range file.AST.Imports {
56+
path := is.Path
57+
if path != nil && r.isBlocklisted(path.Value) {
58+
failures = append(failures, lint.Failure{
59+
Confidence: 1,
60+
Failure: "should not use the following blocklisted import: " + path.Value,
61+
Node: is,
62+
Category: "imports",
63+
})
64+
}
65+
}
66+
67+
return failures
68+
}
69+
70+
// Name returns the rule name.
71+
func (*ImportsBlocklistRule) Name() string {
72+
return "imports-blocklist"
73+
}

rule/var-naming.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ var upperCaseConstRE = regexp.MustCompile(`^_?[A-Z][A-Z\d]*(_[A-Z\d]+)*$`)
1919
// VarNamingRule lints given else constructs.
2020
type VarNamingRule struct {
2121
configured bool
22-
whitelist []string
23-
blacklist []string
22+
allowlist []string
23+
blocklist []string
2424
upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants
2525
skipPackageNameChecks bool
2626
sync.Mutex
@@ -35,11 +35,11 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) {
3535

3636
r.configured = true
3737
if len(arguments) >= 1 {
38-
r.whitelist = getList(arguments[0], "whitelist")
38+
r.allowlist = getList(arguments[0], "whitelist")
3939
}
4040

4141
if len(arguments) >= 2 {
42-
r.blacklist = getList(arguments[1], "blacklist")
42+
r.blocklist = getList(arguments[1], "blacklist")
4343
}
4444

4545
if len(arguments) >= 3 {
@@ -93,8 +93,8 @@ func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.
9393
walker := lintNames{
9494
file: file,
9595
fileAst: fileAst,
96-
whitelist: r.whitelist,
97-
blacklist: r.blacklist,
96+
allowlist: r.allowlist,
97+
blocklist: r.blocklist,
9898
onFailure: func(failure lint.Failure) {
9999
failures = append(failures, failure)
100100
},
@@ -151,7 +151,7 @@ func (w *lintNames) check(id *ast.Ident, thing string) {
151151
return
152152
}
153153

154-
should := lint.Name(id.Name, w.whitelist, w.blacklist)
154+
should := lint.Name(id.Name, w.allowlist, w.blocklist)
155155
if id.Name == should {
156156
return
157157
}
@@ -177,8 +177,8 @@ type lintNames struct {
177177
file *lint.File
178178
fileAst *ast.File
179179
onFailure func(lint.Failure)
180-
whitelist []string
181-
blacklist []string
180+
allowlist []string
181+
blocklist []string
182182
upperCaseConst bool
183183
}
184184

test/import-blocklist_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/mgechev/revive/lint"
7+
"github.com/mgechev/revive/rule"
8+
)
9+
10+
func TestImportsBlocklistOriginal(t *testing.T) {
11+
args := []any{"crypto/md5", "crypto/sha1"}
12+
13+
testRule(t, "imports-blocklist-original", &rule.ImportsBlocklistRule{}, &lint.RuleConfig{
14+
Arguments: args,
15+
})
16+
}
17+
18+
func TestImportsBlocklist(t *testing.T) {
19+
args := []any{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"}
20+
21+
testRule(t, "imports-blocklist", &rule.ImportsBlocklistRule{}, &lint.RuleConfig{
22+
Arguments: args,
23+
})
24+
}
25+
26+
func BenchmarkImportsBlocklist(b *testing.B) {
27+
args := []any{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"}
28+
var t *testing.T
29+
for i := 0; i <= b.N; i++ {
30+
testRule(t, "imports-blocklist", &rule.ImportsBlocklistRule{}, &lint.RuleConfig{
31+
Arguments: args,
32+
})
33+
}
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package fixtures
2+
3+
import (
4+
"crypto/md5" // MATCH /should not use the following blocklisted import: "crypto/md5"/
5+
"crypto/sha1" // MATCH /should not use the following blocklisted import: "crypto/sha1"/
6+
"strings"
7+
)
8+

testdata/imports-blocklist.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package fixtures
2+
3+
import (
4+
"github.com/full/match" // MATCH /should not use the following blocklisted import: "github.com/full/match"/
5+
"bithub.com/full/match"
6+
"github.com/full/matche"
7+
"wildcard/between" // MATCH /should not use the following blocklisted import: "wildcard/between"/
8+
"wildcard/pkg1/between" // MATCH /should not use the following blocklisted import: "wildcard/pkg1/between"/
9+
"wildcard/pkg1/pkg2/between" // MATCH /should not use the following blocklisted import: "wildcard/pkg1/pkg2/between"/
10+
"wildcard/backward" // MATCH /should not use the following blocklisted import: "wildcard/backward"/
11+
"wildcard/backward/pkg" // MATCH /should not use the following blocklisted import: "wildcard/backward/pkg"/
12+
"wildcard/backward/pkg/pkg1" // MATCH /should not use the following blocklisted import: "wildcard/backward/pkg/pkg1"/
13+
"wildcard/forward" // MATCH /should not use the following blocklisted import: "wildcard/forward"/
14+
"pkg/wildcard/forward" // MATCH /should not use the following blocklisted import: "pkg/wildcard/forward"/
15+
"pkg/pkg1/wildcard/forward" // MATCH /should not use the following blocklisted import: "pkg/pkg1/wildcard/forward"/
16+
"full" // MATCH /should not use the following blocklisted import: "full"/
17+
"github.com/partical/match/fully"
18+
"strings"
19+
)

0 commit comments

Comments
 (0)