Skip to content

Commit

Permalink
fix: remove redundant character escape '\/' (#3278)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Oct 5, 2022
1 parent bac47a7 commit d03294f
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 58 deletions.
8 changes: 8 additions & 0 deletions pkg/fsutils/path_unix.go
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)))
}

0 comments on commit d03294f

Please sign in to comment.