Skip to content

Commit d03294f

Browse files
authored
fix: remove redundant character escape '\/' (#3278)
1 parent bac47a7 commit d03294f

File tree

12 files changed

+46
-58
lines changed

12 files changed

+46
-58
lines changed

pkg/fsutils/path_unix.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//go:build !windows
2+
3+
package fsutils
4+
5+
// NormalizePathInRegex it's a noop function on Unix.
6+
func NormalizePathInRegex(path string) string {
7+
return path
8+
}

pkg/fsutils/path_windows.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//go:build windows
2+
3+
package fsutils
4+
5+
import (
6+
"path/filepath"
7+
"regexp"
8+
"strings"
9+
)
10+
11+
var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
12+
13+
// NormalizePathInRegex normalizes path in regular expressions.
14+
// noop on Unix.
15+
// This replacing should be safe because "/" are disallowed in Windows
16+
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
17+
func NormalizePathInRegex(path string) string {
18+
// remove redundant character escape "\/" https://github.com/golangci/golangci-lint/issues/3277
19+
clean := regexp.MustCompile(`\\+/`).
20+
ReplaceAllStringFunc(path, func(s string) string {
21+
if strings.Count(s, "\\")%2 == 0 {
22+
return s
23+
}
24+
return s[1:]
25+
})
26+
27+
return strings.ReplaceAll(clean, "/", separatorToReplace)
28+
}

pkg/golinters/depguard.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package golinters
22

33
import (
44
"fmt"
5-
"path/filepath"
6-
"regexp"
75
"strings"
86
"sync"
97

@@ -12,6 +10,7 @@ import (
1210
"golang.org/x/tools/go/loader" //nolint:staticcheck // require changes in github.com/OpenPeeDeeP/depguard
1311

1412
"github.com/golangci/golangci-lint/pkg/config"
13+
"github.com/golangci/golangci-lint/pkg/fsutils"
1514
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1615
"github.com/golangci/golangci-lint/pkg/lint/linter"
1716
"github.com/golangci/golangci-lint/pkg/result"
@@ -106,16 +105,6 @@ func (d depGuard) run(pass *analysis.Pass) ([]goanalysis.Issue, error) {
106105
return resIssues, nil
107106
}
108107

109-
var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
110-
111-
// normalizePathInRegex normalizes path in regular expressions.
112-
// noop on Unix.
113-
// This replacing should be safe because "/" are disallowed in Windows
114-
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
115-
func normalizePathInRegex(path string) string {
116-
return strings.ReplaceAll(path, "/", separatorToReplace)
117-
}
118-
119108
type guardian struct {
120109
*depguard.Depguard
121110
pkgsWithErrorMessage map[string]string
@@ -124,7 +113,7 @@ type guardian struct {
124113
func newGuardian(settings *config.DepGuardSettings) (*guardian, error) {
125114
var ignoreFileRules []string
126115
for _, rule := range settings.IgnoreFileRules {
127-
ignoreFileRules = append(ignoreFileRules, normalizePathInRegex(rule))
116+
ignoreFileRules = append(ignoreFileRules, fsutils.NormalizePathInRegex(rule))
128117
}
129118

130119
dg := &depguard.Depguard{

pkg/result/processors/exclude_rules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func createRules(rules []ExcludeRule, prefix string) []excludeRule {
4444
parsedRule.source = regexp.MustCompile(prefix + rule.Source)
4545
}
4646
if rule.Path != "" {
47-
path := normalizePathInRegex(rule.Path)
47+
path := fsutils.NormalizePathInRegex(rule.Path)
4848
parsedRule.path = regexp.MustCompile(path)
4949
}
5050
parsedRules = append(parsedRules, parsedRule)

pkg/result/processors/path_unix.go

-8
This file was deleted.

pkg/result/processors/path_windows.go

-19
This file was deleted.

pkg/result/processors/severity_rules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func createSeverityRules(rules []SeverityRule, prefix string) []severityRule {
4949
parsedRule.source = regexp.MustCompile(prefix + rule.Source)
5050
}
5151
if rule.Path != "" {
52-
path := normalizePathInRegex(rule.Path)
52+
path := fsutils.NormalizePathInRegex(rule.Path)
5353
parsedRule.path = regexp.MustCompile(path)
5454
}
5555
parsedRules = append(parsedRules, parsedRule)

pkg/result/processors/skip_dirs.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/pkg/errors"
99

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

pkg/result/processors/skip_files.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"regexp"
66

7+
"github.com/golangci/golangci-lint/pkg/fsutils"
78
"github.com/golangci/golangci-lint/pkg/result"
89
)
910

@@ -16,7 +17,7 @@ var _ Processor = (*SkipFiles)(nil)
1617
func NewSkipFiles(patterns []string) (*SkipFiles, error) {
1718
var patternsRe []*regexp.Regexp
1819
for _, p := range patterns {
19-
p = normalizePathInRegex(p)
20+
p = fsutils.NormalizePathInRegex(p)
2021
patternRe, err := regexp.Compile(p)
2122
if err != nil {
2223
return nil, fmt.Errorf("can't compile regexp %q: %s", p, err)

test/testshared/runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/stretchr/testify/require"
1515

1616
"github.com/golangci/golangci-lint/pkg/exitcodes"
17+
"github.com/golangci/golangci-lint/pkg/fsutils"
1718
"github.com/golangci/golangci-lint/pkg/logutils"
1819
)
1920

@@ -304,7 +305,7 @@ func (r *RunnerResult) ExpectExitCode(possibleCodes ...int) *RunnerResult {
304305
func (r *RunnerResult) ExpectOutputRegexp(s string) *RunnerResult {
305306
r.tb.Helper()
306307

307-
assert.Regexp(r.tb, normalizePathInRegex(s), r.output, "exit code is %d", r.exitCode)
308+
assert.Regexp(r.tb, fsutils.NormalizePathInRegex(s), r.output, "exit code is %d", r.exitCode)
308309
return r
309310
}
310311

test/testshared/runner_unix.go

-5
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,3 @@ func defaultBinaryName() string {
2929
func normalizeFilePath(in string) string {
3030
return in
3131
}
32-
33-
// normalizePathInRegex it's a noop function on Unix.
34-
func normalizePathInRegex(path string) string {
35-
return path
36-
}

test/testshared/runner_windows.go

-8
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,3 @@ func normalizeFilePath(in string) string {
4141
return strings.ReplaceAll(s, "/", "\\")
4242
})
4343
}
44-
45-
// normalizePathInRegex normalizes path in regular expressions.
46-
// Replace all `/` with `\\`.
47-
// This replacing should be safe because "/" are disallowed in Windows
48-
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
49-
func normalizePathInRegex(path string) string {
50-
return strings.ReplaceAll(path, "/", regexp.QuoteMeta(string(filepath.Separator)))
51-
}

0 commit comments

Comments
 (0)