1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-28 08:49:11 +02:00
revive/rule/banned-characters.go
chavacava 2a1838f501
adds default config to enable all rules work out of the box (#830)
* adds default config to enable all rules out of the box
2023-05-20 14:44:34 +02:00

91 lines
2.0 KiB
Go

package rule
import (
"fmt"
"go/ast"
"strings"
"sync"
"github.com/mgechev/revive/lint"
)
// BannedCharsRule checks if a file contains banned characters.
type BannedCharsRule struct {
bannedCharList []string
sync.Mutex
}
const bannedCharsRuleName = "banned-characters"
func (r *BannedCharsRule) configure(arguments lint.Arguments) {
r.Lock()
defer r.Unlock()
if r.bannedCharList == nil && len(arguments) > 0 {
checkNumberOfArguments(1, arguments, bannedCharsRuleName)
r.bannedCharList = r.getBannedCharsList(arguments)
}
}
// Apply applied the rule to the given file.
func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
r.configure(arguments)
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintBannedCharsRule{
bannedChars: r.bannedCharList,
onFailure: onFailure,
}
ast.Walk(w, file.AST)
return failures
}
// Name returns the rule name
func (*BannedCharsRule) Name() string {
return bannedCharsRuleName
}
// getBannedCharsList converts arguments into the banned characters list
func (r *BannedCharsRule) getBannedCharsList(args lint.Arguments) []string {
var bannedChars []string
for _, char := range args {
charStr, ok := char.(string)
if !ok {
panic(fmt.Sprintf("Invalid argument for the %s rule: expecting a string, got %T", r.Name(), char))
}
bannedChars = append(bannedChars, charStr)
}
return bannedChars
}
type lintBannedCharsRule struct {
bannedChars []string
onFailure func(lint.Failure)
}
// Visit checks for each node if an identifier contains banned characters
func (w lintBannedCharsRule) Visit(node ast.Node) ast.Visitor {
n, ok := node.(*ast.Ident)
if !ok {
return w
}
for _, c := range w.bannedChars {
ok := strings.Contains(n.Name, c)
if ok {
w.onFailure(lint.Failure{
Confidence: 1,
Failure: fmt.Sprintf("banned character found: %s", c),
RuleName: bannedCharsRuleName,
Node: n,
})
}
}
return w
}