Skip to content

Commit a553fce

Browse files
committed
fix: Support files configuration option
1 parent 617936f commit a553fce

File tree

2 files changed

+79
-79
lines changed

2 files changed

+79
-79
lines changed

internal/config/config.go

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import (
6565
"github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/only_throw_error"
6666
"github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_as_const"
6767
"github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_promise_reject_errors"
68+
6869
// "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_readonly_parameter_types" // Temporarily disabled - incomplete implementation
6970
"github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_reduce_type_parameter"
7071
"github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_return_this_type"
@@ -306,47 +307,46 @@ func (config RslintConfig) GetRulesForFile(filePath string) map[string]*RuleConf
306307

307308
for _, entry := range config {
308309
// First check if the file should be ignored
309-
if isFileIgnored(filePath, entry.Ignores) {
310+
if isFileMatched(filePath, entry.Ignores) {
310311
continue // Skip this config entry for ignored files
311312
}
312313

313-
// Check if the file matches the files pattern
314-
matches := true
315-
316-
if matches {
314+
// Skip this config entry if the file doesn't matches the files pattern
315+
if len(entry.Files) > 0 && !isFileMatched(filePath, entry.Files) {
316+
continue
317+
}
317318

318-
/// Merge rules from plugin
319-
for _, plugin := range entry.Plugins {
319+
/// Merge rules from plugin
320+
for _, plugin := range entry.Plugins {
320321

321-
for _, rule := range GetAllRulesForPlugin(plugin) {
322-
enabledRules[rule.Name] = &RuleConfig{Level: "error"} // Default level for plugin rules
323-
}
322+
for _, rule := range GetAllRulesForPlugin(plugin) {
323+
enabledRules[rule.Name] = &RuleConfig{Level: "error"} // Default level for plugin rules
324324
}
325-
// Merge rules from this entry
326-
for ruleName, ruleValue := range entry.Rules {
327-
328-
switch v := ruleValue.(type) {
329-
case string:
330-
// Handle simple string values like "error", "warn", "off"
331-
enabledRules[ruleName] = &RuleConfig{Level: v}
332-
case map[string]interface{}:
333-
// Handle object configuration
334-
ruleConfig := &RuleConfig{}
335-
if level, ok := v["level"].(string); ok {
336-
ruleConfig.Level = level
337-
}
338-
if options, ok := v["options"].(map[string]interface{}); ok {
339-
ruleConfig.Options = options
340-
}
341-
if ruleConfig.IsEnabled() {
342-
enabledRules[ruleName] = ruleConfig
343-
}
344-
case []interface{}:
345-
// Handle array format like ["error", {...options}] or ["warn"] or ["off"]
346-
ruleConfig := parseArrayRuleConfig(v)
347-
if ruleConfig != nil && ruleConfig.IsEnabled() {
348-
enabledRules[ruleName] = ruleConfig
349-
}
325+
}
326+
// Merge rules from this entry
327+
for ruleName, ruleValue := range entry.Rules {
328+
329+
switch v := ruleValue.(type) {
330+
case string:
331+
// Handle simple string values like "error", "warn", "off"
332+
enabledRules[ruleName] = &RuleConfig{Level: v}
333+
case map[string]interface{}:
334+
// Handle object configuration
335+
ruleConfig := &RuleConfig{}
336+
if level, ok := v["level"].(string); ok {
337+
ruleConfig.Level = level
338+
}
339+
if options, ok := v["options"].(map[string]interface{}); ok {
340+
ruleConfig.Options = options
341+
}
342+
if ruleConfig.IsEnabled() {
343+
enabledRules[ruleName] = ruleConfig
344+
}
345+
case []interface{}:
346+
// Handle array format like ["error", {...options}] or ["warn"] or ["off"]
347+
ruleConfig := parseArrayRuleConfig(v)
348+
if ruleConfig != nil && ruleConfig.IsEnabled() {
349+
enabledRules[ruleName] = ruleConfig
350350
}
351351
}
352352
}
@@ -471,13 +471,13 @@ func getAllTypeScriptEslintPluginRules() []rule.Rule {
471471
return rules
472472
}
473473

474-
// isFileIgnored checks if a file should be ignored based on ignore patterns
475-
func isFileIgnored(filePath string, ignorePatterns []string) bool {
474+
// isFileMatched checks if a file should be matched based on glob patterns
475+
func isFileMatched(filePath string, ignorePatterns []string) bool {
476476
// Get current working directory for relative path resolution
477477
cwd, err := os.Getwd()
478478
if err != nil {
479479
// If we can't get cwd, fall back to simple matching
480-
return isFileIgnoredSimple(filePath, ignorePatterns)
480+
return isFileMatchedSimple(filePath, ignorePatterns)
481481
}
482482

483483
// Normalize the file path relative to cwd
@@ -515,8 +515,8 @@ func normalizePath(filePath, cwd string) string {
515515
}))
516516
}
517517

518-
// isFileIgnoredSimple provides fallback matching when cwd is unavailable
519-
func isFileIgnoredSimple(filePath string, ignorePatterns []string) bool {
518+
// isFileMatchedSimple provides fallback matching when cwd is unavailable
519+
func isFileMatchedSimple(filePath string, ignorePatterns []string) bool {
520520
for _, pattern := range ignorePatterns {
521521
if matched, err := doublestar.Match(pattern, filePath); err == nil && matched {
522522
return true

internal/config/cwd_test.go

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,62 +17,62 @@ func TestCwdHandling(t *testing.T) {
1717
defer t.Chdir(originalCwd) // Restore after test completes
1818

1919
tests := []struct {
20-
name string
21-
filePath string
22-
patterns []string
23-
shouldIgnore bool
24-
description string
20+
name string
21+
filePath string
22+
patterns []string
23+
shouldMatch bool
24+
description string
2525
}{
2626
{
27-
name: "Relative path matching",
28-
filePath: "src/component.ts",
29-
patterns: []string{"src/**"},
30-
shouldIgnore: true,
31-
description: "Relative paths should match relative patterns",
27+
name: "Relative path matching",
28+
filePath: "src/component.ts",
29+
patterns: []string{"src/**"},
30+
shouldMatch: true,
31+
description: "Relative paths should match relative patterns",
3232
},
3333
{
34-
name: "Absolute path to relative path matching",
35-
filePath: filepath.Join(originalCwd, "src/component.ts"),
36-
patterns: []string{"src/**"},
37-
shouldIgnore: true,
38-
description: "Absolute paths should be converted to relative paths before matching",
34+
name: "Absolute path to relative path matching",
35+
filePath: filepath.Join(originalCwd, "src/component.ts"),
36+
patterns: []string{"src/**"},
37+
shouldMatch: true,
38+
description: "Absolute paths should be converted to relative paths before matching",
3939
},
4040
{
41-
name: "node_modules recursive matching",
42-
filePath: "node_modules/package/deep/file.js",
43-
patterns: []string{"node_modules/**"},
44-
shouldIgnore: true,
45-
description: "Recursive patterns should match deeply nested files",
41+
name: "node_modules recursive matching",
42+
filePath: "node_modules/package/deep/file.js",
43+
patterns: []string{"node_modules/**"},
44+
shouldMatch: true,
45+
description: "Recursive patterns should match deeply nested files",
4646
},
4747
{
48-
name: "Test file pattern matching",
49-
filePath: "src/utils/helper.test.ts",
50-
patterns: []string{"**/*.test.ts"},
51-
shouldIgnore: true,
52-
description: "Global recursive patterns should match test files in any location",
48+
name: "Test file pattern matching",
49+
filePath: "src/utils/helper.test.ts",
50+
patterns: []string{"**/*.test.ts"},
51+
shouldMatch: true,
52+
description: "Global recursive patterns should match test files in any location",
5353
},
5454
{
55-
name: "Non-matching file",
56-
filePath: "src/component.ts",
57-
patterns: []string{"dist/**", "*.log"},
58-
shouldIgnore: false,
59-
description: "Files not matching any pattern should not be ignored",
55+
name: "Non-matching file",
56+
filePath: "src/component.ts",
57+
patterns: []string{"dist/**", "*.log"},
58+
shouldMatch: false,
59+
description: "Files not matching any pattern should not be ignored",
6060
},
6161
{
62-
name: "Cross-platform path handling",
63-
filePath: "src\\windows\\style\\path.ts",
64-
patterns: []string{"src/**"},
65-
shouldIgnore: true,
66-
description: "Windows style paths should be handled correctly",
62+
name: "Cross-platform path handling",
63+
filePath: "src\\windows\\style\\path.ts",
64+
patterns: []string{"src/**"},
65+
shouldMatch: true,
66+
description: "Windows style paths should be handled correctly",
6767
},
6868
}
6969

7070
for _, tt := range tests {
7171
t.Run(tt.name, func(t *testing.T) {
72-
result := isFileIgnored(tt.filePath, tt.patterns)
73-
if result != tt.shouldIgnore {
74-
t.Errorf("%s: isFileIgnored(%q, %v) = %v, expected %v",
75-
tt.description, tt.filePath, tt.patterns, result, tt.shouldIgnore)
72+
result := isFileMatched(tt.filePath, tt.patterns)
73+
if result != tt.shouldMatch {
74+
t.Errorf("%s: isFileMatched(%q, %v) = %v, expected %v",
75+
tt.description, tt.filePath, tt.patterns, result, tt.shouldMatch)
7676
}
7777
})
7878
}

0 commit comments

Comments
 (0)