-
Notifications
You must be signed in to change notification settings - Fork 281
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
Showing
8 changed files
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"regexp" | ||
"sync" | ||
"unicode" | ||
|
||
"github.com/mgechev/revive/lint" | ||
) | ||
|
||
// FilenameFormatRule lints source filenames according to a set of regular expressions given as arguments | ||
type FilenameFormatRule struct { | ||
format *regexp.Regexp | ||
sync.Mutex | ||
} | ||
|
||
// Apply applies the rule to the given file. | ||
func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { | ||
r.configure(arguments) | ||
|
||
filename := filepath.Base(file.Name) | ||
if r.format.MatchString(filename) { | ||
return nil | ||
} | ||
|
||
failureMsg := fmt.Sprintf("Filename %s is not of the format %s.%s", filename, r.format.String(), r.getMsgForNonAsciiChars(filename)) | ||
return []lint.Failure{{ | ||
Confidence: 1, | ||
Failure: failureMsg, | ||
RuleName: r.Name(), | ||
Node: file.AST.Name, | ||
}} | ||
} | ||
|
||
func (r *FilenameFormatRule) getMsgForNonAsciiChars(str string) string { | ||
result := "" | ||
for _, c := range str { | ||
if c <= unicode.MaxASCII { | ||
continue | ||
} | ||
|
||
result += fmt.Sprintf(" Non ASCII character %c (%U) found.", c, c) | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Name returns the rule name. | ||
func (*FilenameFormatRule) Name() string { | ||
return "filename-format" | ||
} | ||
|
||
var defaultFormat = regexp.MustCompile("^[_A-Za-z0-9][_A-Za-z0-9-]*.go$") | ||
|
||
func (r *FilenameFormatRule) configure(arguments lint.Arguments) { | ||
r.Lock() | ||
defer r.Unlock() | ||
|
||
if r.format != nil { | ||
return | ||
} | ||
|
||
argsCount := len(arguments) | ||
if argsCount == 0 { | ||
r.format = defaultFormat | ||
return | ||
} | ||
|
||
if argsCount > 1 { | ||
panic(fmt.Sprintf("rule %q expects only one argument, got %d %v", r.Name(), argsCount, arguments)) | ||
} | ||
|
||
arg := arguments[0] | ||
str, ok := arg.(string) | ||
if !ok { | ||
panic(fmt.Sprintf("rule %q expects a string argument, got %v of type %T", r.Name(), arg, arg)) | ||
} | ||
|
||
format, err := regexp.Compile(str) | ||
if err != nil { | ||
panic(fmt.Sprintf("rule %q expects a valid regexp argument, got %v for %s", r.Name(), err, arg)) | ||
} | ||
|
||
r.format = format | ||
} |
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,16 @@ | ||
package test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mgechev/revive/lint" | ||
"github.com/mgechev/revive/rule" | ||
) | ||
|
||
func TestLintFilenameFormat(t *testing.T) { | ||
testRule(t, "filename-ok-default", &rule.FilenameFormatRule{}, &lint.RuleConfig{}) | ||
|
||
testRule(t, "filenamе-with-non-ascii-char", &rule.FilenameFormatRule{}, &lint.RuleConfig{}) | ||
|
||
testRule(t, "filename_with_underscores", &rule.FilenameFormatRule{}, &lint.RuleConfig{Arguments: []any{"^[A-Za-z][A-Za-z0-9]*.go$"}}) | ||
} |
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 @@ | ||
package main |
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,3 @@ | ||
package main | ||
|
||
// MATCH:1 /Filename filename_with_underscores.go is not of the format ^[A-Za-z][A-Za-z0-9]*.go$./ |
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,3 @@ | ||
package main | ||
|
||
// MATCH:1 /Filename filenamе-with-non-ascii-char.go is not of the format ^[_A-Za-z0-9][_A-Za-z0-9-]*.go$. Non ASCII character е (U+0435) found./ |