Skip to content

Commit 509cbb7

Browse files
authored
feat: add skip files to linter (#236)
1 parent a11fa6b commit 509cbb7

File tree

6 files changed

+18
-5
lines changed

6 files changed

+18
-5
lines changed

cmd/rslint/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,11 @@ func (h *IPCHandler) HandleLint(req api.LintRequest) (*api.LintResponse, error)
134134
}
135135

136136
// Run linter
137-
lintedFilesCount, err := linter.RunLinter(
137+
lintedFilesCount, err := linter.RunLinter(
138138
programs,
139139
false, // Don't use single-threaded mode for IPC
140140
allowedFiles,
141+
utils.ExcludePaths,
141142
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
142143
return utils.Map(rulesWithOptions, func(r RuleWithOption) linter.ConfiguredRule {
143144

cmd/rslint/cmd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ func runCMD() int {
481481
programs,
482482
singleThreaded,
483483
nil,
484+
utils.ExcludePaths,
484485
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
485486
activeRules := rslintconfig.GlobalRuleRegistry.GetEnabledRules(rslintConfig, sourceFile.FileName())
486487
return activeRules

cmd/rslint/lsp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ func runLintWithPrograms(uri lsproto.DocumentUri, programs []*compiler.Program,
505505
programs,
506506
false, // Don't use single-threaded mode for LSP
507507
[]string{filename},
508+
utils.ExcludePaths,
508509
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
509510
activeRules := config.GlobalRuleRegistry.GetEnabledRules(rslintConfig, sourceFile.FileName())
510511
return activeRules

internal/linter/linter.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type ConfiguredRule struct {
2121

2222
// when allowedFiles is passed as nil which means all files are allowed
2323
// when allowedFiles is passed as slice, only files in the slice are allowed
24-
func RunLinter(programs []*compiler.Program, singleThreaded bool, allowFiles []string, getRulesForFile func(sourceFile *ast.SourceFile) []ConfiguredRule, onDiagnostic func(diagnostic rule.RuleDiagnostic)) (int32, error) {
24+
func RunLinter(programs []*compiler.Program, singleThreaded bool, allowFiles []string, skipFiles []string, getRulesForFile func(sourceFile *ast.SourceFile) []ConfiguredRule, onDiagnostic func(diagnostic rule.RuleDiagnostic)) (int32, error) {
2525

2626
wg := core.NewWorkGroup(singleThreaded)
2727

@@ -34,9 +34,15 @@ func RunLinter(programs []*compiler.Program, singleThreaded bool, allowFiles []s
3434
wg.Queue(func() {
3535
for _, file := range program.GetSourceFiles() {
3636
p := string(file.Path())
37-
// skip lint node_modules and bundled files
38-
// FIXME: we may have better api to tell whether a file is a bundled file or not
39-
if strings.Contains(p, "/node_modules/") || strings.Contains(p, "bundled:") {
37+
// skip files based on skipFiles parameter
38+
skipFile := false
39+
for _, skipPattern := range skipFiles {
40+
if strings.Contains(p, skipPattern) {
41+
skipFile = true
42+
break
43+
}
44+
}
45+
if skipFile {
4046
continue
4147
}
4248
// only lint allowedFiles if allowedFiles is not empty

internal/rule_tester/rule_tester.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func RunRuleTester(rootDir string, tsconfigPath string, t *testing.T, r *rule.Ru
8080
[]*compiler.Program{program},
8181
true,
8282
allowedFiles,
83+
[]string{}, // No files to skip in test environment
8384
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
8485
return []linter.ConfiguredRule{
8586
{

internal/utils/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,6 @@ func IsStrWhiteSpace(r rune) bool {
183183
// WhiteSpace
184184
return unicode.Is(unicode.Zs, r)
185185
}
186+
187+
// ExcludePaths contains paths that should be excluded from linting
188+
var ExcludePaths = []string{"/node_modules/", "bundled:"}

0 commit comments

Comments
 (0)