-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
13 changed files
with
135 additions
and
59 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
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
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"go/ast" | ||
"strings" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// BannedCharsRule checks if a file contains banned characters. | ||
type BannedCharsRule struct{} | ||
|
||
const bannedCharsRuleName = "banned-characters" | ||
|
||
// Apply applied the rule to the given file. | ||
func (r *BannedCharsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { | ||
var failures []lint.Failure | ||
|
||
checkNumberOfArguments(1, arguments, bannedCharsRuleName) | ||
bannedCharList := r.getBannedCharsList(arguments) | ||
|
||
onFailure := func(failure lint.Failure) { | ||
failures = append(failures, failure) | ||
} | ||
|
||
w := lintBannedCharsRule{ | ||
bannedChars: bannedCharList, | ||
onFailure: onFailure, | ||
} | ||
ast.Walk(w, file.AST) | ||
return failures | ||
} | ||
|
||
// Name returns the rule name | ||
func (r *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 | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
// Test banned characters in a const, var and func names. | ||
// One banned character is in the comment and should not be checked. | ||
// One banned character from the list is not present in the fixture file. | ||
func TestBannedCharacters(t *testing.T) { | ||
testRule(t, "banned-characters", &rule.BannedCharsRule{}, &lint.RuleConfig{ | ||
Arguments: []interface{}{"Ω", "Σ", "σ", "1"}, | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package fixtures | ||
|
||
const Ω = "Omega" // MATCH:3 /banned character found: Ω/ | ||
|
||
// func contains banned characters Ω // authorized banned chars in comment | ||
func funcΣ() error { // MATCH:6 /banned character found: Σ/ | ||
var charσhid string // MATCH:7 /banned character found: σ/ | ||
return nil | ||
} |
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