1
0
mirror of https://github.com/mgechev/revive.git synced 2025-06-06 23:16:16 +02:00
revive/rule/filename_format.go

82 lines
1.9 KiB
Go
Raw Normal View History

2024-11-02 14:23:46 -03:00
package rule
import (
"fmt"
"path/filepath"
"regexp"
"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
}
// Apply applies the rule to the given file.
func (r *FilenameFormatRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
2024-11-02 14:23:46 -03:00
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))
2024-11-02 14:23:46 -03:00
return []lint.Failure{{
Confidence: 1,
Failure: failureMsg,
RuleName: r.Name(),
Node: file.AST.Name,
}}
}
refactor: fix linting issues (#1188) * refactor: fix thelper issues test/utils_test.go:19:6 thelper test helper function should start from t.Helper() test/utils_test.go:42:6 thelper test helper function should start from t.Helper() test/utils_test.go:63:6 thelper test helper function should start from t.Helper() test/utils_test.go:146:6 thelper test helper function should start from t.Helper() * refactor: fix govet issues rule/error_strings.go:50:21 govet printf: non-constant format string in call to fmt.Errorf * refactor: fix gosimple issue rule/bare_return.go:52:9 gosimple S1016: should convert w (type lintBareReturnRule) to bareReturnFinder instead of using struct literal * refactor: fix errorlint issues lint/filefilter.go:70:86 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/filefilter.go:113:104 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/filefilter.go:125:89 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/linter.go:166:72 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/linter.go:171:73 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors config/config.go:174:57 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors config/config.go:179:64 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors * refactor: fix revive issue about comment spacing cli/main.go:31:2 revive comment-spacings: no space between comment delimiter and comment text * refactor: fix revive issue about unused-receiver revivelib/core_test.go:77:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ revivelib/core_test.go:81:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/context_as_argument.go:76:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/var_naming.go:73:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/modifies_value_receiver.go:59:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/filename_format.go:43:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ * refactor: fix revive issues about unused-parameter revivelib/core_test.go:81:24 revive unused-parameter: parameter 'file' seems to be unused, consider removing or renaming it as _ revivelib/core_test.go:81:41 revive unused-parameter: parameter 'arguments' seems to be unused, consider removing or renaming it as _ * refactor: fix gocritic issues about commentedOutCode test/utils_test.go:119:5 gocritic commentedOutCode: may want to remove commented-out code rule/unreachable_code.go:65:3 gocritic commentedOutCode: may want to remove commented-out code
2024-12-12 08:42:41 +01:00
func (*FilenameFormatRule) getMsgForNonASCIIChars(str string) string {
2024-11-02 14:23:46 -03:00
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$`)
2024-11-02 14:23:46 -03:00
// Configure validates the rule configuration, and configures the rule accordingly.
//
// Configuration implements the [lint.ConfigurableRule] interface.
func (r *FilenameFormatRule) Configure(arguments lint.Arguments) error {
2024-11-02 14:23:46 -03:00
argsCount := len(arguments)
if argsCount == 0 {
r.format = defaultFormat
return nil
2024-11-02 14:23:46 -03:00
}
if argsCount > 1 {
return fmt.Errorf("rule %q expects only one argument, got %d %v", r.Name(), argsCount, arguments)
2024-11-02 14:23:46 -03:00
}
arg := arguments[0]
str, ok := arg.(string)
if !ok {
return fmt.Errorf("rule %q expects a string argument, got %v of type %T", r.Name(), arg, arg)
2024-11-02 14:23:46 -03:00
}
format, err := regexp.Compile(str)
if err != nil {
return fmt.Errorf("rule %q expects a valid regexp argument, got error for %s: %w", r.Name(), str, err)
2024-11-02 14:23:46 -03:00
}
r.format = format
return nil
2024-11-02 14:23:46 -03:00
}