mirror of
https://github.com/mgechev/revive.git
synced 2025-06-10 23:27:48 +02:00
* 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
131 lines
3.3 KiB
Go
131 lines
3.3 KiB
Go
package lint
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// FileFilter - file filter to exclude some files for rule
|
|
// supports whole
|
|
// 1. file/dir names : pkg/mypkg/my.go,
|
|
// 2. globs: **/*.pb.go,
|
|
// 3. regexes (~ prefix) ~-tmp\.\d+\.go
|
|
// 4. special test marker `TEST` - treats as `~_test\.go`
|
|
type FileFilter struct {
|
|
// raw definition of filter inside config
|
|
raw string
|
|
// don't care what was at start, will use regexes inside
|
|
rx *regexp.Regexp
|
|
// marks filter as matching everything
|
|
matchesAll bool
|
|
// marks filter as matching nothing
|
|
matchesNothing bool
|
|
}
|
|
|
|
// ParseFileFilter - creates [FileFilter] for given raw filter
|
|
// if empty string, it matches nothing
|
|
// if `*`, or `~`, it matches everything
|
|
// while regexp could be invalid, it could return it's compilation error
|
|
func ParseFileFilter(rawFilter string) (*FileFilter, error) {
|
|
rawFilter = strings.TrimSpace(rawFilter)
|
|
result := new(FileFilter)
|
|
result.raw = rawFilter
|
|
result.matchesNothing = len(result.raw) == 0
|
|
result.matchesAll = result.raw == "*" || result.raw == "~"
|
|
if !result.matchesAll && !result.matchesNothing {
|
|
if err := result.prepareRegexp(); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (ff *FileFilter) String() string { return ff.raw }
|
|
|
|
// MatchFileName - checks if file name matches filter
|
|
func (ff *FileFilter) MatchFileName(name string) bool {
|
|
if ff.matchesAll {
|
|
return true
|
|
}
|
|
if ff.matchesNothing {
|
|
return false
|
|
}
|
|
name = strings.ReplaceAll(name, "\\", "/")
|
|
return ff.rx.MatchString(name)
|
|
}
|
|
|
|
var (
|
|
fileFilterInvalidGlobRegexp = regexp.MustCompile(`[^/]\*\*[^/]`)
|
|
escapeRegexSymbols = ".+{}()[]^$"
|
|
)
|
|
|
|
func (ff *FileFilter) prepareRegexp() error {
|
|
var err error
|
|
src := ff.raw
|
|
if src == "TEST" {
|
|
src = "~_test\\.go"
|
|
}
|
|
if strings.HasPrefix(src, "~") {
|
|
ff.rx, err = regexp.Compile(src[1:])
|
|
if err != nil {
|
|
return fmt.Errorf("invalid file filter [%s], regexp compile error: [%w]", ff.raw, err)
|
|
}
|
|
return nil
|
|
}
|
|
/* globs */
|
|
if strings.Contains(src, "*") {
|
|
if fileFilterInvalidGlobRegexp.MatchString(src) {
|
|
return fmt.Errorf("invalid file filter [%s], invalid glob pattern", ff.raw)
|
|
}
|
|
var rxBuild strings.Builder
|
|
rxBuild.WriteByte('^')
|
|
wasStar := false
|
|
justDirGlob := false
|
|
for _, c := range src {
|
|
if c == '*' {
|
|
if wasStar {
|
|
rxBuild.WriteString(`[\s\S]*`)
|
|
wasStar = false
|
|
justDirGlob = true
|
|
continue
|
|
}
|
|
wasStar = true
|
|
continue
|
|
}
|
|
if wasStar {
|
|
rxBuild.WriteString("[^/]*")
|
|
wasStar = false
|
|
}
|
|
if strings.ContainsRune(escapeRegexSymbols, c) {
|
|
rxBuild.WriteByte('\\')
|
|
}
|
|
rxBuild.WriteRune(c)
|
|
if c == '/' && justDirGlob {
|
|
rxBuild.WriteRune('?')
|
|
}
|
|
justDirGlob = false
|
|
}
|
|
if wasStar {
|
|
rxBuild.WriteString("[^/]*")
|
|
}
|
|
rxBuild.WriteByte('$')
|
|
ff.rx, err = regexp.Compile(rxBuild.String())
|
|
if err != nil {
|
|
return fmt.Errorf("invalid file filter [%s], regexp compile error after glob expand: [%w]", ff.raw, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// it's whole file mask, just escape dots and normilze separators
|
|
fillRx := src
|
|
fillRx = strings.ReplaceAll(fillRx, "\\", "/")
|
|
fillRx = strings.ReplaceAll(fillRx, ".", `\.`)
|
|
fillRx = "^" + fillRx + "$"
|
|
ff.rx, err = regexp.Compile(fillRx)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid file filter [%s], regexp compile full path: [%w]", ff.raw, err)
|
|
}
|
|
return nil
|
|
}
|