-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0188281
commit 97795eb
Showing
3 changed files
with
57 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters