diff --git a/README.md b/README.md index 89e4350e3..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) | whitelist & blacklist 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 | @@ -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 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] diff --git a/RULES_DESCRIPTIONS.md b/RULES_DESCRIPTIONS.md index e1b63b641..73b0ee31b 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 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)) 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..e91c22dc2 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], "allowlist") } 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" -)