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

refactor: rename blacklist to blocklist and whitelist to allowlist #946

Merged
merged 21 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
3facb4d
refactor: rename blacklist to blocklist and whitelist to allowlist
mfederowicz Nov 28, 2023
1d733c6
add deprecatedRules list
mfederowicz Nov 30, 2023
0188281
Merge remote-tracking branch 'origin/master' into deprecate-whitelist…
mfederowicz Nov 30, 2023
1d89e03
cleanup code and revert changes related with deprecations steps
mfederowicz Nov 30, 2023
66e1d2a
review cleanups
mfederowicz Dec 1, 2023
9d73a9f
revert blacklist test and testdata
mfederowicz Dec 14, 2023
825dcbe
revert blacklist test and testdata
mfederowicz Dec 14, 2023
cec76c7
add test to import-blocklist
mfederowicz Dec 14, 2023
fd10d4c
fix merge conflict
mfederowicz Jan 14, 2024
1a3d9d6
fix pointer
mfederowicz Jan 14, 2024
878c126
fix pointer
mfederowicz Jan 14, 2024
a9461ee
fix: enable tests in the test/utils.go file (#930)
alexandear Dec 2, 2023
61d13e1
fix(deps): update module golang.org/x/tools to v0.16.1 (#949)
renovate[bot] Dec 12, 2023
bc83faa
feat: add support for enforce-repeated-arg-type-style rule (#953)
denisvmedia Dec 27, 2023
384dfb7
fix: remove errored replace directive, fix go install (#957)
EXHades Jan 4, 2024
8d79dac
Update list of contributors (#960)
denisvmedia Jan 8, 2024
c7d2941
unhandled-error: use full function name in error message (#962)
mmcloughlin Jan 8, 2024
bbb8127
fix(deps): update module golang.org/x/tools to v0.17.0 (#963)
renovate[bot] Jan 12, 2024
f033e16
fix pointer
mfederowicz Jan 14, 2024
b7630f7
fix pointer
mfederowicz Jan 14, 2024
f1775bb
Merge branch 'deprecate-whitelist-blacklist' of gh:mfederowicz/revive…
mfederowicz Jan 14, 2024
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
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var allRules = append([]lint.Rule{
&rule.ConstantLogicalExprRule{},
&rule.BoolLiteralRule{},
&rule.ImportsBlacklistRule{},
&rule.ImportsBlocklistRule{},
&rule.FunctionResultsLimitRule{},
&rule.MaxPublicStructsRule{},
&rule.RangeValInClosureRule{},
Expand Down
6 changes: 3 additions & 3 deletions lint/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

// Name returns a different name if it should be different.
func Name(name string, whitelist, blacklist []string) (should string) {
func Name(name string, allowlist, blocklist []string) (should string) {
// Fast path for simple cases: "_" and all lowercase.
if name == "_" {
return name
Expand Down Expand Up @@ -57,12 +57,12 @@ func Name(name string, whitelist, blacklist []string) (should string) {
// [w,i) is a word.
word := string(runes[w:i])
ignoreInitWarnings := map[string]bool{}
for _, i := range whitelist {
for _, i := range allowlist {
ignoreInitWarnings[i] = true
}

extraInits := map[string]bool{}
for _, i := range blacklist {
for _, i := range blocklist {
extraInits[i] = true
}

Expand Down
22 changes: 11 additions & 11 deletions rule/add-constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const (
kindSTRING = "STRING"
)

type whiteList map[string]map[string]bool
type allowList map[string]map[string]bool

func newWhiteList() whiteList {
func newAllowList() allowList {
return map[string]map[string]bool{kindINT: {}, kindFLOAT: {}, kindSTRING: {}}
}

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

// AddConstantRule lints unused params in functions.
type AddConstantRule struct {
whiteList whiteList
allowList allowList
ignoreFunctions []*regexp.Regexp
strLitLimit int
sync.Mutex
Expand All @@ -53,7 +53,7 @@ func (r *AddConstantRule) Apply(file *lint.File, arguments lint.Arguments) []lin
onFailure: onFailure,
strLits: make(map[string]int),
strLitLimit: r.strLitLimit,
whiteLst: r.whiteList,
allowList: r.allowList,
ignoreFunctions: r.ignoreFunctions,
structTags: make(map[*ast.BasicLit]struct{}),
}
Expand All @@ -72,7 +72,7 @@ type lintAddConstantRule struct {
onFailure func(lint.Failure)
strLits map[string]int
strLitLimit int
whiteLst whiteList
allowList allowList
ignoreFunctions []*regexp.Regexp
structTags map[*ast.BasicLit]struct{}
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (w *lintAddConstantRule) isIgnoredFunc(fName string) bool {
}

func (w *lintAddConstantRule) checkStrLit(n *ast.BasicLit) {
if w.whiteLst[kindSTRING][n.Value] {
if w.allowList[kindSTRING][n.Value] {
return
}

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

func (w *lintAddConstantRule) checkNumLit(kind string, n *ast.BasicLit) {
if w.whiteLst[kind][n.Value] {
if w.allowList[kind][n.Value] {
return
}

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

if r.whiteList == nil {
if r.allowList == nil {
r.strLitLimit = defaultStrLitLimit
r.whiteList = newWhiteList()
r.allowList = newAllowList()
if len(arguments) > 0 {
args, ok := arguments[0].(map[string]any)
if !ok {
Expand All @@ -223,7 +223,7 @@ func (r *AddConstantRule) configure(arguments lint.Arguments) {
if !ok {
panic(fmt.Sprintf("Invalid argument to the add-constant rule, string expected. Got '%v' (%T)", v, v))
}
r.whiteList.add(kind, list)
r.allowList.add(kind, list)
case "maxLitCount":
sl, ok := v.(string)
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion rule/confusing-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func checkMethodName(holder string, id *ast.Ident, w *lintConfusingNames) {
pkgm.methods[holder] = make(map[string]*referenceMethod, 1)
}

// update the black list
// update the block list
if pkgm.methods[holder] == nil {
println("no entry for '", holder, "'")
}
Expand Down
73 changes: 73 additions & 0 deletions rule/imports-blocklist.go
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"
}
18 changes: 9 additions & 9 deletions rule/var-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var upperCaseConstRE = regexp.MustCompile(`^_?[A-Z][A-Z\d]*(_[A-Z\d]+)*$`)
// VarNamingRule lints given else constructs.
type VarNamingRule struct {
configured bool
whitelist []string
blacklist []string
allowlist []string
blocklist []string
upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants
skipPackageNameChecks bool
sync.Mutex
Expand All @@ -35,11 +35,11 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) {

r.configured = true
if len(arguments) >= 1 {
r.whitelist = getList(arguments[0], "whitelist")
r.allowlist = getList(arguments[0], "whitelist")
}

if len(arguments) >= 2 {
r.blacklist = getList(arguments[1], "blacklist")
r.blocklist = getList(arguments[1], "blacklist")
}

if len(arguments) >= 3 {
Expand Down Expand Up @@ -93,8 +93,8 @@ func (r *VarNamingRule) Apply(file *lint.File, arguments lint.Arguments) []lint.
walker := lintNames{
file: file,
fileAst: fileAst,
whitelist: r.whitelist,
blacklist: r.blacklist,
allowlist: r.allowlist,
blocklist: r.blocklist,
onFailure: func(failure lint.Failure) {
failures = append(failures, failure)
},
Expand Down Expand Up @@ -151,7 +151,7 @@ func (w *lintNames) check(id *ast.Ident, thing string) {
return
}

should := lint.Name(id.Name, w.whitelist, w.blacklist)
should := lint.Name(id.Name, w.allowlist, w.blocklist)
if id.Name == should {
return
}
Expand All @@ -177,8 +177,8 @@ type lintNames struct {
file *lint.File
fileAst *ast.File
onFailure func(lint.Failure)
whitelist []string
blacklist []string
allowlist []string
blocklist []string
upperCaseConst bool
}

Expand Down
34 changes: 34 additions & 0 deletions test/import-blocklist_test.go
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,
})
}
}
8 changes: 8 additions & 0 deletions testdata/imports-blocklist-original.go
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"
)

19 changes: 19 additions & 0 deletions testdata/imports-blocklist.go
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"
)
Loading