Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
mfederowicz committed Nov 30, 2023
1 parent 0188281 commit 97795eb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
52 changes: 50 additions & 2 deletions rule/imports-blacklist.go
Original file line number Diff line number Diff line change
@@ -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"
}
4 changes: 2 additions & 2 deletions rule/imports-blocklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
}
Expand Down
9 changes: 5 additions & 4 deletions rule/var-naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 97795eb

Please sign in to comment.