-
Notifications
You must be signed in to change notification settings - Fork 14
fix: Support files configuration option #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lambda47
wants to merge
2
commits into
web-infra-dev:main
Choose a base branch
from
lambda47:fix/support_files_config
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,7 @@ import ( | |
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/bmatcuk/doublestar/v4" | ||
| "github.com/microsoft/typescript-go/shim/tspath" | ||
| importPlugin "github.com/web-infra-dev/rslint/internal/plugins/import" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/adjacent_overload_signatures" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/array_type" | ||
|
|
@@ -65,6 +62,7 @@ import ( | |
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/only_throw_error" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_as_const" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_promise_reject_errors" | ||
|
|
||
| // "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_readonly_parameter_types" // Temporarily disabled - incomplete implementation | ||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_reduce_type_parameter" | ||
| "github.com/web-infra-dev/rslint/internal/plugins/typescript/rules/prefer_return_this_type" | ||
|
|
@@ -108,6 +106,15 @@ type ConfigEntry struct { | |
| LanguageOptions *LanguageOptions `json:"languageOptions,omitempty"` | ||
| Rules Rules `json:"rules"` | ||
| Plugins []string `json:"plugins,omitempty"` // List of plugin names | ||
| matcher *fileMatcher // Cache for the file matcher | ||
| } | ||
|
|
||
| // GetFileMatcher returns the cached file matcher or creates a new one | ||
| func (c *ConfigEntry) GetFileMatcher(cwd string) *fileMatcher { | ||
| if c.matcher == nil { | ||
| c.matcher = newFileMatcher(c, cwd) | ||
| } | ||
| return c.matcher | ||
| } | ||
|
|
||
| // LanguageOptions contains language-specific configuration options | ||
|
|
@@ -304,49 +311,47 @@ func parseArrayRuleConfig(ruleArray []interface{}) *RuleConfig { | |
| func (config RslintConfig) GetRulesForFile(filePath string) map[string]*RuleConfig { | ||
| enabledRules := make(map[string]*RuleConfig) | ||
|
|
||
| for _, entry := range config { | ||
| // First check if the file should be ignored | ||
| if isFileIgnored(filePath, entry.Ignores) { | ||
| continue // Skip this config entry for ignored files | ||
| } | ||
| cwd, _ := os.Getwd() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this fails, cwd becomes empty and pattern normalization may behave unexpectedly. Consider referring to the previous logic in |
||
|
|
||
| // Check if the file matches the files pattern | ||
| matches := true | ||
| for i := range config { | ||
| entry := &config[i] | ||
|
|
||
| if matches { | ||
| fileMatcher := entry.GetFileMatcher(cwd) | ||
| if !fileMatcher.isFileMatched(normalizeAbsolutePath(filePath, cwd)) { | ||
| continue | ||
| } | ||
|
|
||
| /// Merge rules from plugin | ||
| for _, plugin := range entry.Plugins { | ||
| /// Merge rules from plugin | ||
| for _, plugin := range entry.Plugins { | ||
|
|
||
| for _, rule := range GetAllRulesForPlugin(plugin) { | ||
| enabledRules[rule.Name] = &RuleConfig{Level: "error"} // Default level for plugin rules | ||
| } | ||
| for _, rule := range GetAllRulesForPlugin(plugin) { | ||
| enabledRules[rule.Name] = &RuleConfig{Level: "error"} // Default level for plugin rules | ||
| } | ||
| // Merge rules from this entry | ||
| for ruleName, ruleValue := range entry.Rules { | ||
|
|
||
| switch v := ruleValue.(type) { | ||
| case string: | ||
| // Handle simple string values like "error", "warn", "off" | ||
| enabledRules[ruleName] = &RuleConfig{Level: v} | ||
| case map[string]interface{}: | ||
| // Handle object configuration | ||
| ruleConfig := &RuleConfig{} | ||
| if level, ok := v["level"].(string); ok { | ||
| ruleConfig.Level = level | ||
| } | ||
| if options, ok := v["options"].(map[string]interface{}); ok { | ||
| ruleConfig.Options = options | ||
| } | ||
| if ruleConfig.IsEnabled() { | ||
| enabledRules[ruleName] = ruleConfig | ||
| } | ||
| case []interface{}: | ||
| // Handle array format like ["error", {...options}] or ["warn"] or ["off"] | ||
| ruleConfig := parseArrayRuleConfig(v) | ||
| if ruleConfig != nil && ruleConfig.IsEnabled() { | ||
| enabledRules[ruleName] = ruleConfig | ||
| } | ||
| } | ||
| // Merge rules from this entry | ||
| for ruleName, ruleValue := range entry.Rules { | ||
|
|
||
| switch v := ruleValue.(type) { | ||
| case string: | ||
| // Handle simple string values like "error", "warn", "off" | ||
| enabledRules[ruleName] = &RuleConfig{Level: v} | ||
| case map[string]interface{}: | ||
| // Handle object configuration | ||
| ruleConfig := &RuleConfig{} | ||
| if level, ok := v["level"].(string); ok { | ||
| ruleConfig.Level = level | ||
| } | ||
| if options, ok := v["options"].(map[string]interface{}); ok { | ||
| ruleConfig.Options = options | ||
| } | ||
| if ruleConfig.IsEnabled() { | ||
| enabledRules[ruleName] = ruleConfig | ||
| } | ||
| case []interface{}: | ||
| // Handle array format like ["error", {...options}] or ["warn"] or ["off"] | ||
| ruleConfig := parseArrayRuleConfig(v) | ||
| if ruleConfig != nil && ruleConfig.IsEnabled() { | ||
| enabledRules[ruleName] = ruleConfig | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -471,60 +476,6 @@ func getAllTypeScriptEslintPluginRules() []rule.Rule { | |
| return rules | ||
| } | ||
|
|
||
| // isFileIgnored checks if a file should be ignored based on ignore patterns | ||
| func isFileIgnored(filePath string, ignorePatterns []string) bool { | ||
| // Get current working directory for relative path resolution | ||
| cwd, err := os.Getwd() | ||
| if err != nil { | ||
| // If we can't get cwd, fall back to simple matching | ||
| return isFileIgnoredSimple(filePath, ignorePatterns) | ||
| } | ||
|
|
||
| // Normalize the file path relative to cwd | ||
| normalizedPath := normalizePath(filePath, cwd) | ||
|
|
||
| for _, pattern := range ignorePatterns { | ||
| // Try matching against normalized path | ||
| if matched, err := doublestar.Match(pattern, normalizedPath); err == nil && matched { | ||
| return true | ||
| } | ||
|
|
||
| // Also try matching against original path for absolute patterns | ||
| if normalizedPath != filePath { | ||
| if matched, err := doublestar.Match(pattern, filePath); err == nil && matched { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| // Try Unix-style path for cross-platform compatibility | ||
| unixPath := strings.ReplaceAll(normalizedPath, "\\", "/") | ||
| if unixPath != normalizedPath { | ||
| if matched, err := doublestar.Match(pattern, unixPath); err == nil && matched { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // normalizePath converts file path to be relative to cwd for consistent matching | ||
| func normalizePath(filePath, cwd string) string { | ||
| return tspath.NormalizePath(tspath.ConvertToRelativePath(filePath, tspath.ComparePathsOptions{ | ||
| UseCaseSensitiveFileNames: true, | ||
| CurrentDirectory: cwd, | ||
| })) | ||
| } | ||
|
|
||
| // isFileIgnoredSimple provides fallback matching when cwd is unavailable | ||
| func isFileIgnoredSimple(filePath string, ignorePatterns []string) bool { | ||
| for _, pattern := range ignorePatterns { | ||
| if matched, err := doublestar.Match(pattern, filePath); err == nil && matched { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // initialize a default config in the directory | ||
| func InitDefaultConfig(directory string) error { | ||
| configPath := filepath.Join(directory, "rslint.jsonc") | ||
|
|
||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
matcherfield andGetFileMatchermethod on ConfigEntry should be removed. Suggest creating matcher inline inGetRulesForFileinstead.