From 0d1a44cbf217cd736f556ce199e2e9ee001e91db Mon Sep 17 00:00:00 2001 From: chavacava Date: Sat, 3 Feb 2024 10:31:36 +0100 Subject: [PATCH 1/4] removes references to black and white lists --- README.md | 6 +-- RULES_DESCRIPTIONS.md | 12 ++--- config/config.go | 13 ++++- rule/imports-blacklist.go | 73 -------------------------- rule/var-naming.go | 4 +- test/import-blacklist_test.go | 34 ------------ testdata/imports-blacklist-original.go | 8 --- testdata/imports-blacklist.go | 19 ------- 8 files changed, 22 insertions(+), 147 deletions(-) delete mode 100644 rule/imports-blacklist.go delete mode 100644 test/import-blacklist_test.go delete mode 100644 testdata/imports-blacklist-original.go delete mode 100644 testdata/imports-blacklist.go diff --git a/README.md b/README.md index 89e4350e3..81fe79d42 100644 --- a/README.md +++ b/README.md @@ -477,7 +477,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a | [`exported`](./RULES_DESCRIPTIONS.md#exported) | []string | Naming and commenting conventions on exported symbols. | yes | no | | [`if-return`](./RULES_DESCRIPTIONS.md#if-return) | n/a | Redundant if when returning an error. | no | no | | [`increment-decrement`](./RULES_DESCRIPTIONS.md#increment-decrement) | n/a | Use `i++` and `i--` instead of `i += 1` and `i -= 1`. | yes | no | -| [`var-naming`](./RULES_DESCRIPTIONS.md#var-naming) | whitelist & blacklist of initialisms | Naming rules. | yes | no | +| [`var-naming`](./RULES_DESCRIPTIONS.md#var-naming) | allow-list & blocklist of initialisms | Naming rules. | yes | no | | [`package-comments`](./RULES_DESCRIPTIONS.md#package-comments) | n/a | Package commenting conventions. | yes | no | | [`range`](./RULES_DESCRIPTIONS.md#range) | n/a | Prevents redundant variables when iterating over a collection. | yes | no | | [`receiver-naming`](./RULES_DESCRIPTIONS.md#receiver-naming) | n/a | Conventions around the naming of receivers. | yes | no | @@ -504,7 +504,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a | [`bool-literal-in-expr`](./RULES_DESCRIPTIONS.md#bool-literal-in-expr)| n/a | Suggests removing Boolean literals from logic expressions | no | no | | [`redefines-builtin-id`](./RULES_DESCRIPTIONS.md#redefines-builtin-id)| n/a | Warns on redefinitions of builtin identifiers | no | no | | [`function-result-limit`](./RULES_DESCRIPTIONS.md#function-result-limit) | int (defaults to 3)| Specifies the maximum number of results a function can return | no | no | -| [`imports-blacklist`](./RULES_DESCRIPTIONS.md#imports-blacklist) | []string | Disallows importing the specified packages | no | no | +| [`imports-blocklist`](./RULES_DESCRIPTIONS.md#imports-blocklist) | []string | Disallows importing the specified packages | no | no | | [`range-val-in-closure`](./RULES_DESCRIPTIONS.md#range-val-in-closure)| n/a | Warns if range value is used in a closure dispatched as goroutine| no | no | | [`range-val-address`](./RULES_DESCRIPTIONS.md#range-val-address)| n/a | Warns if address of range value is used dangerously | no | yes | | [`waitgroup-by-value`](./RULES_DESCRIPTIONS.md#waitgroup-by-value) | n/a | Warns on functions taking sync.WaitGroup as a by-value parameter | no | no | @@ -547,7 +547,7 @@ Here you can find how you can configure some existing rules: ### `var-naming` -This rule accepts two slices of strings, a whitelist and a blacklist of initialisms. By default, the rule behaves exactly as the alternative in `golint` but optionally, you can relax it (see [golint/lint/issues/89](https://github.com/golang/lint/issues/89)) +This rule accepts two slices of strings, an allow-list and a blocklist of initialisms. By default, the rule behaves exactly as the alternative in `golint` but optionally, you can relax it (see [golint/lint/issues/89](https://github.com/golang/lint/issues/89)) ```toml [rule.var-naming] diff --git a/RULES_DESCRIPTIONS.md b/RULES_DESCRIPTIONS.md index e1b63b641..6f55e83ee 100644 --- a/RULES_DESCRIPTIONS.md +++ b/RULES_DESCRIPTIONS.md @@ -44,7 +44,7 @@ List of all available rules. - [if-return](#if-return) - [import-alias-naming](#import-alias-naming) - [import-shadowing](#import-shadowing) - - [imports-blacklist](#imports-blacklist) + - [imports-blocklist](#imports-blocklist) - [increment-decrement](#increment-decrement) - [indent-error-flow](#indent-error-flow) - [line-length-limit](#line-length-limit) @@ -570,16 +570,16 @@ name of an imported package. This rule spots identifiers that shadow an import. _Configuration_: N/A -## imports-blacklist +## imports-blocklist -_Description_: Warns when importing black-listed packages. +_Description_: Warns when importing block-listed packages. -_Configuration_: black-list of package names (or regular expression package names). +_Configuration_: block-list of package names (or regular expression package names). Example: ```toml -[imports-blacklist] +[imports-blocklist] arguments =["crypto/md5", "crypto/sha1", "crypto/**/pkix"] ``` @@ -928,7 +928,7 @@ _Description_: This rule warns when [initialism](https://github.com/golang/go/wi _Configuration_: This rule accepts two slices of strings and one optional slice with single map with named parameters. (it's due to TOML hasn't "slice of any" and we keep backward compatibility with previous config version) -First slice is a whitelist and second one is a blacklist of initialisms. +First slice is an allow-list and second one is a blocklist of initialisms. In map, you can add "upperCaseConst=true" parameter to allow `UPPER_CASE` for `const` By default, the rule behaves exactly as the alternative in `golint` but optionally, you can relax it (see [golint/lint/issues/89](https://github.com/golang/lint/issues/89)) diff --git a/config/config.go b/config/config.go index a0e0ff618..78ff637fa 100644 --- a/config/config.go +++ b/config/config.go @@ -55,7 +55,6 @@ var allRules = append([]lint.Rule{ &rule.ModifiesValRecRule{}, &rule.ConstantLogicalExprRule{}, &rule.BoolLiteralRule{}, - &rule.ImportsBlacklistRule{}, &rule.ImportsBlocklistRule{}, &rule.FunctionResultsLimitRule{}, &rule.MaxPublicStructsRule{}, @@ -132,7 +131,8 @@ func GetLintingRules(config *lint.Config, extraRules []lint.Rule) ([]lint.Rule, var lintingRules []lint.Rule for name, ruleConfig := range config.Rules { - r, ok := rulesMap[name] + actualName := actualRuleName(name) + r, ok := rulesMap[actualName] if !ok { return nil, fmt.Errorf("cannot find rule: %s", name) } @@ -147,6 +147,15 @@ func GetLintingRules(config *lint.Config, extraRules []lint.Rule) ([]lint.Rule, return lintingRules, nil } +func actualRuleName(name string) string { + switch name { + case "imports-blacklist": + return "imports-blocklist" + default: + return name + } +} + func parseConfig(path string, config *lint.Config) error { file, err := os.ReadFile(path) if err != nil { diff --git a/rule/imports-blacklist.go b/rule/imports-blacklist.go deleted file mode 100644 index bb8262cae..000000000 --- a/rule/imports-blacklist.go +++ /dev/null @@ -1,73 +0,0 @@ -package rule - -import ( - "fmt" - "regexp" - "sync" - - "github.com/mgechev/revive/lint" -) - -// ImportsBlacklistRule lints given else constructs. -type ImportsBlacklistRule struct { - blacklist []*regexp.Regexp - sync.Mutex -} - -var replaceRegexp = regexp.MustCompile(`/?\*\*/?`) - -func (r *ImportsBlacklistRule) configure(arguments lint.Arguments) { - r.Lock() - defer r.Unlock() - - if r.blacklist == nil { - r.blacklist = make([]*regexp.Regexp, 0) - - for _, arg := range arguments { - argStr, ok := arg.(string) - if !ok { - panic(fmt.Sprintf("Invalid argument to the imports-blacklist rule. Expecting a string, got %T", arg)) - } - regStr, err := regexp.Compile(fmt.Sprintf(`(?m)"%s"$`, replaceRegexp.ReplaceAllString(argStr, `(\W|\w)*`))) - if err != nil { - panic(fmt.Sprintf("Invalid argument to the imports-blacklist rule. Expecting %q to be a valid regular expression, got: %v", argStr, err)) - } - r.blacklist = append(r.blacklist, regStr) - } - } -} - -func (r *ImportsBlacklistRule) isBlacklisted(path string) bool { - for _, regex := range r.blacklist { - if regex.MatchString(path) { - return true - } - } - return false -} - -// Apply applies the rule to given file. -func (r *ImportsBlacklistRule) 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.isBlacklisted(path.Value) { - failures = append(failures, lint.Failure{ - Confidence: 1, - Failure: "should not use the following blacklisted import: " + path.Value, - Node: is, - Category: "imports", - }) - } - } - - return failures -} - -// Name returns the rule name. -func (*ImportsBlacklistRule) Name() string { - return "imports-blacklist" -} diff --git a/rule/var-naming.go b/rule/var-naming.go index c818e50da..2ae0138e2 100644 --- a/rule/var-naming.go +++ b/rule/var-naming.go @@ -35,11 +35,11 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) { r.configured = true if len(arguments) >= 1 { - r.allowlist = getList(arguments[0], "whitelist") + r.allowlist = getList(arguments[0], "allow-list") } if len(arguments) >= 2 { - r.blocklist = getList(arguments[1], "blacklist") + r.blocklist = getList(arguments[1], "blocklist") } if len(arguments) >= 3 { diff --git a/test/import-blacklist_test.go b/test/import-blacklist_test.go deleted file mode 100644 index cd2c9e772..000000000 --- a/test/import-blacklist_test.go +++ /dev/null @@ -1,34 +0,0 @@ -package test - -import ( - "testing" - - "github.com/mgechev/revive/lint" - "github.com/mgechev/revive/rule" -) - -func TestImportsBlacklistOriginal(t *testing.T) { - args := []any{"crypto/md5", "crypto/sha1"} - - testRule(t, "imports-blacklist-original", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{ - Arguments: args, - }) -} - -func TestImportsBlacklist(t *testing.T) { - args := []any{"github.com/full/match", "wildcard/**/between", "wildcard/backward/**", "**/wildcard/forward", "full"} - - testRule(t, "imports-blacklist", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{ - Arguments: args, - }) -} - -func BenchmarkImportsBlacklist(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-blacklist", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{ - Arguments: args, - }) - } -} diff --git a/testdata/imports-blacklist-original.go b/testdata/imports-blacklist-original.go deleted file mode 100644 index e8d6f7d9d..000000000 --- a/testdata/imports-blacklist-original.go +++ /dev/null @@ -1,8 +0,0 @@ -package fixtures - -import ( - "crypto/md5" // MATCH /should not use the following blacklisted import: "crypto/md5"/ - "crypto/sha1" // MATCH /should not use the following blacklisted import: "crypto/sha1"/ - "strings" -) - diff --git a/testdata/imports-blacklist.go b/testdata/imports-blacklist.go deleted file mode 100644 index aca95fc71..000000000 --- a/testdata/imports-blacklist.go +++ /dev/null @@ -1,19 +0,0 @@ -package fixtures - -import ( - "github.com/full/match" // MATCH /should not use the following blacklisted import: "github.com/full/match"/ - "bithub.com/full/match" - "github.com/full/matche" - "wildcard/between" // MATCH /should not use the following blacklisted import: "wildcard/between"/ - "wildcard/pkg1/between" // MATCH /should not use the following blacklisted import: "wildcard/pkg1/between"/ - "wildcard/pkg1/pkg2/between" // MATCH /should not use the following blacklisted import: "wildcard/pkg1/pkg2/between"/ - "wildcard/backward" // MATCH /should not use the following blacklisted import: "wildcard/backward"/ - "wildcard/backward/pkg" // MATCH /should not use the following blacklisted import: "wildcard/backward/pkg"/ - "wildcard/backward/pkg/pkg1" // MATCH /should not use the following blacklisted import: "wildcard/backward/pkg/pkg1"/ - "wildcard/forward" // MATCH /should not use the following blacklisted import: "wildcard/forward"/ - "pkg/wildcard/forward" // MATCH /should not use the following blacklisted import: "pkg/wildcard/forward"/ - "pkg/pkg1/wildcard/forward" // MATCH /should not use the following blacklisted import: "pkg/pkg1/wildcard/forward"/ - "full" // MATCH /should not use the following blacklisted import: "full"/ - "github.com/partical/match/fully" - "strings" -) From 37b47f7a05da5aa65decb7a7a7c61d44c4cf004f Mon Sep 17 00:00:00 2001 From: chavacava Date: Sat, 3 Feb 2024 18:19:35 +0100 Subject: [PATCH 2/4] replaces allow-list with allowlist --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81fe79d42..1aed0e490 100644 --- a/README.md +++ b/README.md @@ -477,7 +477,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a | [`exported`](./RULES_DESCRIPTIONS.md#exported) | []string | Naming and commenting conventions on exported symbols. | yes | no | | [`if-return`](./RULES_DESCRIPTIONS.md#if-return) | n/a | Redundant if when returning an error. | no | no | | [`increment-decrement`](./RULES_DESCRIPTIONS.md#increment-decrement) | n/a | Use `i++` and `i--` instead of `i += 1` and `i -= 1`. | yes | no | -| [`var-naming`](./RULES_DESCRIPTIONS.md#var-naming) | allow-list & blocklist of initialisms | Naming rules. | yes | no | +| [`var-naming`](./RULES_DESCRIPTIONS.md#var-naming) | allowlist & blocklist of initialisms | Naming rules. | yes | no | | [`package-comments`](./RULES_DESCRIPTIONS.md#package-comments) | n/a | Package commenting conventions. | yes | no | | [`range`](./RULES_DESCRIPTIONS.md#range) | n/a | Prevents redundant variables when iterating over a collection. | yes | no | | [`receiver-naming`](./RULES_DESCRIPTIONS.md#receiver-naming) | n/a | Conventions around the naming of receivers. | yes | no | @@ -547,7 +547,7 @@ Here you can find how you can configure some existing rules: ### `var-naming` -This rule accepts two slices of strings, an allow-list and a blocklist of initialisms. By default, the rule behaves exactly as the alternative in `golint` but optionally, you can relax it (see [golint/lint/issues/89](https://github.com/golang/lint/issues/89)) +This rule accepts two slices of strings, an allowlist and a blocklist of initialisms. By default, the rule behaves exactly as the alternative in `golint` but optionally, you can relax it (see [golint/lint/issues/89](https://github.com/golang/lint/issues/89)) ```toml [rule.var-naming] From 9c6b7662dbbdb9610f8449f3307950660dd7416b Mon Sep 17 00:00:00 2001 From: chavacava Date: Sat, 3 Feb 2024 18:21:05 +0100 Subject: [PATCH 3/4] replaces allow-list with allowlist --- RULES_DESCRIPTIONS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RULES_DESCRIPTIONS.md b/RULES_DESCRIPTIONS.md index 6f55e83ee..73b0ee31b 100644 --- a/RULES_DESCRIPTIONS.md +++ b/RULES_DESCRIPTIONS.md @@ -928,7 +928,7 @@ _Description_: This rule warns when [initialism](https://github.com/golang/go/wi _Configuration_: This rule accepts two slices of strings and one optional slice with single map with named parameters. (it's due to TOML hasn't "slice of any" and we keep backward compatibility with previous config version) -First slice is an allow-list and second one is a blocklist of initialisms. +First slice is an allowlist and second one is a blocklist of initialisms. In map, you can add "upperCaseConst=true" parameter to allow `UPPER_CASE` for `const` By default, the rule behaves exactly as the alternative in `golint` but optionally, you can relax it (see [golint/lint/issues/89](https://github.com/golang/lint/issues/89)) From a284314dad93900e003d667764f5bef85e07c5ea Mon Sep 17 00:00:00 2001 From: chavacava Date: Sat, 3 Feb 2024 18:22:32 +0100 Subject: [PATCH 4/4] replaces allow-list by allowlist --- rule/var-naming.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rule/var-naming.go b/rule/var-naming.go index 2ae0138e2..e91c22dc2 100644 --- a/rule/var-naming.go +++ b/rule/var-naming.go @@ -35,7 +35,7 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) { r.configured = true if len(arguments) >= 1 { - r.allowlist = getList(arguments[0], "allow-list") + r.allowlist = getList(arguments[0], "allowlist") } if len(arguments) >= 2 {