Skip to content

Commit

Permalink
ignore 'vendor' and 'testdata' directories and directories with names…
Browse files Browse the repository at this point in the history
… that begin with '.' or '_'

Fixes #42
  • Loading branch information
fzipp committed Mar 22, 2022
1 parent 9daf4d9 commit 3f1109b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Ignore `vendor` and `testdata` directories and directories with names
that begin with `.` or `_`

## [0.4.0] - 2021-12-19
### Added
- Support method receivers with type parameters introduced in Go 1.18
Expand Down
14 changes: 14 additions & 0 deletions analyze.go
Expand Up @@ -41,6 +41,9 @@ func Analyze(paths []string, ignore *regexp.Regexp) Stats {

func analyzeDir(dirname string, ignore *regexp.Regexp, stats Stats) Stats {
filepath.WalkDir(dirname, func(path string, entry fs.DirEntry, err error) error {
if isSkipDir(entry) {
return filepath.SkipDir
}
if err == nil && isGoFile(entry) {
stats = analyzeFile(path, ignore, stats)
}
Expand All @@ -49,6 +52,17 @@ func analyzeDir(dirname string, ignore *regexp.Regexp, stats Stats) Stats {
return stats
}

var skipDirs = map[string]bool{
"testdata": true,
"vendor": true,
}

func isSkipDir(entry fs.DirEntry) bool {
return entry.IsDir() && (skipDirs[entry.Name()] ||
strings.HasPrefix(entry.Name(), ".") ||
strings.HasPrefix(entry.Name(), "_"))
}

func isGoFile(entry fs.DirEntry) bool {
return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".go")
}
Expand Down
4 changes: 4 additions & 0 deletions analyze_test.go
Expand Up @@ -75,6 +75,10 @@ func TestAnalyze(t *testing.T) {
`1 directory b testdata/directory/file2.go:3:1
1 directory a testdata/directory/file1.go:3:1`,
},
{
[]string{"testdata/ignore"},
"",
},
}

for _, tt := range tests {
Expand Down
3 changes: 3 additions & 0 deletions testdata/ignore/.dotname/file.go
@@ -0,0 +1,3 @@
package dotname

func a() {}
3 changes: 3 additions & 0 deletions testdata/ignore/_underscore/file.go
@@ -0,0 +1,3 @@
package underscore

func a() {}
3 changes: 3 additions & 0 deletions testdata/ignore/testdata/file.go
@@ -0,0 +1,3 @@
package testdata

func a() {}
3 changes: 3 additions & 0 deletions testdata/ignore/vendor/file.go
@@ -0,0 +1,3 @@
package vendor

func a() {}

0 comments on commit 3f1109b

Please sign in to comment.