Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/fsutils/path_unix.go
@@ -0,0 +1,8 @@
//go:build !windows

package fsutils

// NormalizePathInRegex it's a noop function on Unix.
func NormalizePathInRegex(path string) string {
return path
}
28 changes: 28 additions & 0 deletions pkg/fsutils/path_windows.go
@@ -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)
}
15 changes: 2 additions & 13 deletions pkg/golinters/depguard.go
Expand Up @@ -2,8 +2,6 @@ package golinters

import (
"fmt"
"path/filepath"
"regexp"
"strings"
"sync"

Expand All @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/result/processors/exclude_rules.go
Expand Up @@ -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)
Expand Down
8 changes: 0 additions & 8 deletions pkg/result/processors/path_unix.go

This file was deleted.

19 changes: 0 additions & 19 deletions pkg/result/processors/path_windows.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/result/processors/severity_rules.go
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/result/processors/skip_dirs.go
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/result/processors/skip_files.go
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"regexp"

"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/result"
)

Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion test/testshared/runner.go
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

Expand Down
5 changes: 0 additions & 5 deletions test/testshared/runner_unix.go
Expand Up @@ -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
}
8 changes: 0 additions & 8 deletions test/testshared/runner_windows.go
Expand Up @@ -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)))
}