From e0da12a8686074c73f8b4e10fa125d2974962085 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Thu, 11 Jan 2024 11:43:02 +0800 Subject: [PATCH] fix(linter): allow eslintrc to add rule when overriding (#1984) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix a bug introduced in https://github.com/oxc-project/oxc/pull/1966. Ideally, the rules in eslintrc should be merged into final rules as described: > The rules will start with the categories we apply, and then merge all the configurations stated in the rules field. > > For example, if we begin with -D correctness with 80 rules, then > > "no-empty-file": "off" will remove the rule, yielding 79 rules > "no-empty": "error" (restriction) will add the rule, yield 81 rules > ""no-empty": ["error", { "allowEmptyCatch": true }]` add the rule's configuration However, the implementation did not include the newly added rules in the eslintrc. As a test case and example, I added a new fixture to `crates/oxc_cli/fixtures/no_undef`. No warn or deny will be found without this PR. This is my first Rust PR ever. Any nitpicking suggestions are welcome. Thanks! 😊 --- .../oxc_cli/fixtures/no_undef/eslintrc.json | 5 ++++ crates/oxc_cli/fixtures/no_undef/test.js | 1 + crates/oxc_cli/src/lint/mod.rs | 9 ++++++++ crates/oxc_linter/src/config/mod.rs | 23 ++++++++++++++----- crates/oxc_linter/src/options.rs | 2 +- 5 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 crates/oxc_cli/fixtures/no_undef/eslintrc.json create mode 100644 crates/oxc_cli/fixtures/no_undef/test.js diff --git a/crates/oxc_cli/fixtures/no_undef/eslintrc.json b/crates/oxc_cli/fixtures/no_undef/eslintrc.json new file mode 100644 index 000000000..f646b2282 --- /dev/null +++ b/crates/oxc_cli/fixtures/no_undef/eslintrc.json @@ -0,0 +1,5 @@ +{ + "rules": { + "no-undef": "warn" + } +} diff --git a/crates/oxc_cli/fixtures/no_undef/test.js b/crates/oxc_cli/fixtures/no_undef/test.js new file mode 100644 index 000000000..0f4e35a4a --- /dev/null +++ b/crates/oxc_cli/fixtures/no_undef/test.js @@ -0,0 +1 @@ +console.log() diff --git a/crates/oxc_cli/src/lint/mod.rs b/crates/oxc_cli/src/lint/mod.rs index fa85fe16e..fd5a2acb1 100644 --- a/crates/oxc_cli/src/lint/mod.rs +++ b/crates/oxc_cli/src/lint/mod.rs @@ -324,6 +324,15 @@ mod test { assert_eq!(result.number_of_errors, 0); } + #[test] + fn eslintrc_no_undef() { + let args = &["-c", "fixtures/no_undef/eslintrc.json", "fixtures/no_undef/test.js"]; + let result = test(args); + assert_eq!(result.number_of_files, 1); + assert_eq!(result.number_of_warnings, 1); + assert_eq!(result.number_of_errors, 0); + } + #[test] fn no_empty_allow_empty_catch() { let args = &[ diff --git a/crates/oxc_linter/src/config/mod.rs b/crates/oxc_linter/src/config/mod.rs index 306fcce1f..e347d183f 100644 --- a/crates/oxc_linter/src/config/mod.rs +++ b/crates/oxc_linter/src/config/mod.rs @@ -68,14 +68,20 @@ impl ESLintConfig { }) } - pub fn override_rules(&self, rules_to_override: &mut FxHashSet) { + pub fn override_rules( + &self, + rules_to_override: &mut FxHashSet, + all_rules: &[RuleEnum], + ) { let mut rules_to_replace = vec![]; let mut rules_to_remove = vec![]; - for rule in rules_to_override.iter() { - let plugin_name = rule.plugin_name(); - let rule_name = rule.name(); - if let Some(rule_to_configure) = - self.rules.iter().find(|r| r.plugin_name == plugin_name && r.rule_name == rule_name) + + for rule_to_configure in &self.rules { + let (plugin_name, rule_name) = + (&rule_to_configure.plugin_name, &rule_to_configure.rule_name); + if let Some(rule) = rules_to_override + .iter() + .find(|r| r.plugin_name() == plugin_name && r.name() == rule_name) { match rule_to_configure.severity { AllowWarnDeny::Warn | AllowWarnDeny::Deny => { @@ -85,8 +91,13 @@ impl ESLintConfig { rules_to_remove.push(rule.clone()); } } + } else if let Some(rule) = + all_rules.iter().find(|r| r.plugin_name() == plugin_name && r.name() == rule_name) + { + rules_to_replace.push(rule.read_json(rule_to_configure.config.clone())); } } + for rule in rules_to_remove { rules_to_override.remove(&rule); } diff --git a/crates/oxc_linter/src/options.rs b/crates/oxc_linter/src/options.rs index 312de4c9b..53465459c 100644 --- a/crates/oxc_linter/src/options.rs +++ b/crates/oxc_linter/src/options.rs @@ -199,7 +199,7 @@ impl LintOptions { } if let Some(config) = &config { - config.override_rules(&mut rules); + config.override_rules(&mut rules, &all_rules); } let mut rules = rules.into_iter().collect::>();