fix: remove redundant character escape '\/' (#3278)

This commit is contained in:
Ludovic Fernandez 2022-10-06 00:09:31 +02:00 committed by GitHub
parent bac47a7e52
commit d03294f25f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 46 additions and 58 deletions

8
pkg/fsutils/path_unix.go Normal file
View File

@ -0,0 +1,8 @@
//go:build !windows
package fsutils
// NormalizePathInRegex it's a noop function on Unix.
func NormalizePathInRegex(path string) string {
return path
}

View File

@ -0,0 +1,28 @@
//go:build windows
package fsutils
import (
"path/filepath"
"regexp"
"strings"
)
var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
// NormalizePathInRegex normalizes path in regular expressions.
// noop on Unix.
// This replacing should be safe because "/" are disallowed in Windows
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
func NormalizePathInRegex(path string) string {
// remove redundant character escape "\/" https://github.com/golangci/golangci-lint/issues/3277
clean := regexp.MustCompile(`\\+/`).
ReplaceAllStringFunc(path, func(s string) string {
if strings.Count(s, "\\")%2 == 0 {
return s
}
return s[1:]
})
return strings.ReplaceAll(clean, "/", separatorToReplace)
}

View File

@ -2,8 +2,6 @@ package golinters
import (
"fmt"
"path/filepath"
"regexp"
"strings"
"sync"
@ -12,6 +10,7 @@ import (
"golang.org/x/tools/go/loader" //nolint:staticcheck // require changes in github.com/OpenPeeDeeP/depguard
"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
@ -106,16 +105,6 @@ func (d depGuard) run(pass *analysis.Pass) ([]goanalysis.Issue, error) {
return resIssues, nil
}
var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
// normalizePathInRegex normalizes path in regular expressions.
// noop on Unix.
// This replacing should be safe because "/" are disallowed in Windows
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
func normalizePathInRegex(path string) string {
return strings.ReplaceAll(path, "/", separatorToReplace)
}
type guardian struct {
*depguard.Depguard
pkgsWithErrorMessage map[string]string
@ -124,7 +113,7 @@ type guardian struct {
func newGuardian(settings *config.DepGuardSettings) (*guardian, error) {
var ignoreFileRules []string
for _, rule := range settings.IgnoreFileRules {
ignoreFileRules = append(ignoreFileRules, normalizePathInRegex(rule))
ignoreFileRules = append(ignoreFileRules, fsutils.NormalizePathInRegex(rule))
}
dg := &depguard.Depguard{

View File

@ -44,7 +44,7 @@ func createRules(rules []ExcludeRule, prefix string) []excludeRule {
parsedRule.source = regexp.MustCompile(prefix + rule.Source)
}
if rule.Path != "" {
path := normalizePathInRegex(rule.Path)
path := fsutils.NormalizePathInRegex(rule.Path)
parsedRule.path = regexp.MustCompile(path)
}
parsedRules = append(parsedRules, parsedRule)

View File

@ -1,8 +0,0 @@
//go:build !windows
package processors
// normalizePathInRegex it's a noop function on Unix.
func normalizePathInRegex(path string) string {
return path
}

View File

@ -1,19 +0,0 @@
//go:build windows
package processors
import (
"path/filepath"
"regexp"
"strings"
)
var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
// normalizePathInRegex normalizes path in regular expressions.
// noop on Unix.
// This replacing should be safe because "/" are disallowed in Windows
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
func normalizePathInRegex(path string) string {
return strings.ReplaceAll(path, "/", separatorToReplace)
}

View File

@ -49,7 +49,7 @@ func createSeverityRules(rules []SeverityRule, prefix string) []severityRule {
parsedRule.source = regexp.MustCompile(prefix + rule.Source)
}
if rule.Path != "" {
path := normalizePathInRegex(rule.Path)
path := fsutils.NormalizePathInRegex(rule.Path)
parsedRule.path = regexp.MustCompile(path)
}
parsedRules = append(parsedRules, parsedRule)

View File

@ -7,6 +7,7 @@ import (
"github.com/pkg/errors"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
"github.com/golangci/golangci-lint/pkg/result"
)
@ -31,7 +32,7 @@ const goFileSuffix = ".go"
func NewSkipDirs(patterns []string, log logutils.Log, runArgs []string) (*SkipDirs, error) {
var patternsRe []*regexp.Regexp
for _, p := range patterns {
p = normalizePathInRegex(p)
p = fsutils.NormalizePathInRegex(p)
patternRe, err := regexp.Compile(p)
if err != nil {
return nil, errors.Wrapf(err, "can't compile regexp %q", p)

View File

@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/result"
)
@ -16,7 +17,7 @@ var _ Processor = (*SkipFiles)(nil)
func NewSkipFiles(patterns []string) (*SkipFiles, error) {
var patternsRe []*regexp.Regexp
for _, p := range patterns {
p = normalizePathInRegex(p)
p = fsutils.NormalizePathInRegex(p)
patternRe, err := regexp.Compile(p)
if err != nil {
return nil, fmt.Errorf("can't compile regexp %q: %s", p, err)

View File

@ -14,6 +14,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/golangci/golangci-lint/pkg/exitcodes"
"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
)
@ -304,7 +305,7 @@ func (r *RunnerResult) ExpectExitCode(possibleCodes ...int) *RunnerResult {
func (r *RunnerResult) ExpectOutputRegexp(s string) *RunnerResult {
r.tb.Helper()
assert.Regexp(r.tb, normalizePathInRegex(s), r.output, "exit code is %d", r.exitCode)
assert.Regexp(r.tb, fsutils.NormalizePathInRegex(s), r.output, "exit code is %d", r.exitCode)
return r
}

View File

@ -29,8 +29,3 @@ func defaultBinaryName() string {
func normalizeFilePath(in string) string {
return in
}
// normalizePathInRegex it's a noop function on Unix.
func normalizePathInRegex(path string) string {
return path
}

View File

@ -41,11 +41,3 @@ func normalizeFilePath(in string) string {
return strings.ReplaceAll(s, "/", "\\")
})
}
// normalizePathInRegex normalizes path in regular expressions.
// Replace all `/` with `\\`.
// This replacing should be safe because "/" are disallowed in Windows
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
func normalizePathInRegex(path string) string {
return strings.ReplaceAll(path, "/", regexp.QuoteMeta(string(filepath.Separator)))
}