diff --git a/rule/imports-blacklist.go b/rule/imports-blacklist.go index 9eae8658c..bb8262cae 100644 --- a/rule/imports-blacklist.go +++ b/rule/imports-blacklist.go @@ -1,25 +1,73 @@ package rule import ( + "fmt" "regexp" "sync" "github.com/mgechev/revive/lint" ) -// Deprecated: use ImportsBlocklistRule instead +// ImportsBlacklistRule lints given else constructs. type ImportsBlacklistRule struct { - blocklist []*regexp.Regexp + 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/imports-blocklist.go b/rule/imports-blocklist.go index fc4a25d9c..431066403 100644 --- a/rule/imports-blocklist.go +++ b/rule/imports-blocklist.go @@ -14,7 +14,7 @@ type ImportsBlocklistRule struct { sync.Mutex } -var replaceRegexp = regexp.MustCompile(`/?\*\*/?`) +var replaceImportRegexp = regexp.MustCompile(`/?\*\*/?`) func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) { r.Lock() @@ -28,7 +28,7 @@ func (r *ImportsBlocklistRule) configure(arguments lint.Arguments) { 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"$`, replaceRegexp.ReplaceAllString(argStr, `(\W|\w)*`))) + 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)) } diff --git a/rule/var-naming.go b/rule/var-naming.go index 804316a2b..e91c22dc2 100644 --- a/rule/var-naming.go +++ b/rule/var-naming.go @@ -18,10 +18,11 @@ var upperCaseConstRE = regexp.MustCompile(`^_?[A-Z][A-Z\d]*(_[A-Z\d]+)*$`) // VarNamingRule lints given else constructs. type VarNamingRule struct { - configured bool - allowlist []string - blocklist []string - upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants + configured bool + allowlist []string + blocklist []string + upperCaseConst bool // if true - allows to use UPPER_SOME_NAMES for constants + skipPackageNameChecks bool sync.Mutex }