Skip to content

Commit 6539915

Browse files
authored
feat(lint): apply defaultIgnoreDirectories to nested directories (#1072)
1 parent 271254b commit 6539915

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

internal/core/util.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"bytes"
55
"fmt"
6+
"path/filepath"
67
"regexp"
78
"strings"
89
"unicode"
@@ -44,7 +45,12 @@ func WhitespaceToSpace(msg string) string {
4445
}
4546

4647
// ShouldIgnoreDirectory will check if directory should be ignored
47-
func ShouldIgnoreDirectory(directoryName string) bool {
48+
func ShouldIgnoreDirectory(directoryPath string) bool {
49+
directoryName := filepath.Base(directoryPath)
50+
// if a current dir is detected e.g with directoryPath being an empty string always process
51+
if directoryName == "." {
52+
return false
53+
}
4854
for _, directory := range defaultIgnoreDirectories {
4955
if directory == directoryName {
5056
return true

internal/core/util_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,79 @@ func TestNormalizePath(t *testing.T) {
8888
t.Errorf("expected = %v, got = %v", expectedOutput, result)
8989
}
9090
}
91+
92+
func TestShouldIgnoreDirectory(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
path string
96+
expected bool
97+
}{
98+
{
99+
name: "empty directory name",
100+
path: "",
101+
expected: false,
102+
},
103+
// Direct directory names
104+
{
105+
name: "direct node_modules",
106+
path: "node_modules",
107+
expected: true,
108+
},
109+
{
110+
name: "direct .git",
111+
path: ".git",
112+
expected: true,
113+
},
114+
// Nested paths with ignored directories
115+
{
116+
name: "nested node_modules",
117+
path: "plugins/foo/node_modules",
118+
expected: true,
119+
},
120+
{
121+
name: "nested .git in worktree",
122+
path: "worktree-a/.git",
123+
expected: true,
124+
},
125+
{
126+
name: "deeply nested node_modules",
127+
path: "project/src/components/node_modules",
128+
expected: true,
129+
},
130+
{
131+
name: "node_modules in path with backslashes",
132+
path: filepath.Join("project", "src", "node_modules"),
133+
expected: true,
134+
},
135+
// Non-ignored directories
136+
{
137+
name: "regular directory",
138+
path: "src",
139+
expected: false,
140+
},
141+
{
142+
name: "nested regular directory",
143+
path: "plugins/foo",
144+
expected: false,
145+
},
146+
{
147+
name: "directory containing node_modules in name",
148+
path: "my_node_modules_backup",
149+
expected: false,
150+
},
151+
{
152+
name: "directory containing .git in name",
153+
path: "my.github",
154+
expected: false,
155+
},
156+
}
157+
158+
for _, tt := range tests {
159+
t.Run(tt.name, func(t *testing.T) {
160+
result := ShouldIgnoreDirectory(tt.path)
161+
if result != tt.expected {
162+
t.Errorf("ShouldIgnoreDirectory(%q) = %v, expected %v", tt.path, result, tt.expected)
163+
}
164+
})
165+
}
166+
}

0 commit comments

Comments
 (0)