Skip to content

Commit f970547

Browse files
committed
prepareSkips: to resolve regexps in excludes
1 parent d76ffb9 commit f970547

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ require (
1717
)
1818

1919
require (
20+
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
2021
github.com/mattn/go-colorable v0.1.13 // indirect
2122
github.com/mattn/go-isatty v0.0.20 // indirect
2223
github.com/mattn/go-runewidth v0.0.16 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
22
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3+
github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I=
4+
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
35
github.com/chavacava/garif v0.1.0 h1:2JHa3hbYf5D9dsgseMKAmc/MZ109otzgNFk5s87H9Pc=
46
github.com/chavacava/garif v0.1.0/go.mod h1:XMyYCkEL58DF0oyW4qDjjnPWONs2HBqYKI+UIPD+Gww=
57
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

revivelib/core.go

100644100755
Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"path/filepath"
89
"strings"
910

11+
"github.com/bmatcuk/doublestar/v4"
1012
"github.com/mgechev/dots"
1113
"github.com/mgechev/revive/config"
1214
"github.com/mgechev/revive/lint"
@@ -169,14 +171,42 @@ func getPackages(includePatterns []string, excludePatterns ArrayFlags) ([][]stri
169171
globs = append(globs, ".")
170172
}
171173

172-
packages, err := dots.ResolvePackages(globs, normalizeSplit(excludePatterns))
174+
globs, skips, err := prepareSkips(globs, normalizeSplit(excludePatterns))
175+
if err != nil {
176+
return nil, fmt.Errorf("prepare skips - resolving excludes before dots: %w", err)
177+
}
178+
179+
packages, err := dots.ResolvePackages(globs, skips)
173180
if err != nil {
174181
return nil, fmt.Errorf("getting packages - resolving packages in dots: %w", err)
175182
}
176183

177184
return packages, nil
178185
}
179186

187+
func prepareSkips(globs, excludes []string) ([]string, []string, error) {
188+
var skips []string
189+
for _, path := range globs {
190+
var basepath string
191+
basepath, _ = doublestar.SplitPattern(path)
192+
fsys := os.DirFS(basepath)
193+
for _, skip := range excludes {
194+
matches, err := doublestar.Glob(fsys, skip)
195+
if err != nil {
196+
return nil, nil, fmt.Errorf("Skips Error: %v", err)
197+
}
198+
for _, match := range matches {
199+
path = basepath+"/"+match
200+
// create skip only for .go files
201+
if filepath.Ext(path) == ".go" {
202+
skips = append(skips, path)
203+
}
204+
}
205+
}
206+
}
207+
return globs, skips, nil
208+
}
209+
180210
func normalizeSplit(strs []string) []string {
181211
res := []string{}
182212

revivelib/core_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,43 @@ func TestReviveLint(t *testing.T) {
3636
}
3737
}
3838

39+
func TestReviveLintExcludeWithRegexp(t *testing.T) {
40+
// ARRANGE
41+
revive := getMockRevive(t)
42+
43+
// ACT
44+
files := []string{"../testdata/if-return.go"}
45+
excludePatterns := []string{"*return*"}
46+
packages := []*revivelib.LintPattern{}
47+
for _, file := range files {
48+
packages = append(packages, revivelib.Include(file))
49+
}
50+
51+
for _, file := range excludePatterns {
52+
packages = append(packages, revivelib.Exclude(file))
53+
}
54+
55+
failures, err := revive.Lint(packages...)
56+
57+
if err != nil {
58+
t.Fatal(err)
59+
}
60+
61+
// ASSERT
62+
failureList := []lint.Failure{}
63+
64+
for failure := range failures {
65+
failureList = append(failureList, failure)
66+
}
67+
68+
const expected = 0
69+
70+
got := len(failureList)
71+
if got != expected {
72+
t.Fatalf("Expected failures to have %d failures, but it has %d.", expected, got)
73+
}
74+
}
75+
3976
func TestReviveFormat(t *testing.T) {
4077
// ARRANGE
4178
revive := getMockRevive(t)

0 commit comments

Comments
 (0)