Skip to content

Commit

Permalink
fix(linter): can't disable no-nested-ternary rule anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Jan 31, 2025
1 parent c15af02 commit 89fc613
Showing 1 changed file with 57 additions and 33 deletions.
90 changes: 57 additions & 33 deletions crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,28 +110,43 @@ impl OxlintRules {
}
}
_ => {
// For overlapping rule names, use the "error" one
// "no-loss-of-precision": "off",
// "@typescript-eslint/no-loss-of-precision": "error"
if let Some(rule_config) =
rule_configs.iter().find(|r| r.severity.is_warn_deny())
{
let config = rule_config.config.clone().unwrap_or_default();

if let Some(rule) = rules_for_override.iter().find(|r| r.name() == *name) {
rules_to_replace
.push(RuleWithSeverity::new(rule.read_json(config), rule.severity));
}
// If the given rule is not found in the rule list (for example, if all rules are disabled),
// then look it up in the entire rules list and add it.
else if let Some(rule) = all_rules.iter().find(|r| r.name() == *name) {
rules_to_replace.push(RuleWithSeverity::new(
rule.read_json(config),
rule_config.severity,
));
}
} else if rule_configs.iter().all(|r| r.severity.is_allow()) {
if let Some(rule) = rules_for_override.iter().find(|r| r.name() == *name) {
let rules = rules_for_override
.iter()
.filter_map(|r| {
if r.name() == *name {
Some((r.plugin_name(), r))
} else {
None
}
})
.collect::<FxHashMap<_, _>>();

for rule_config in rule_configs {
let (rule_name, plugin_name) = transform_rule_and_plugin_name(
&rule_config.rule_name,
&rule_config.plugin_name,
);

if rule_config.severity.is_warn_deny() {
let config = rule_config.config.clone().unwrap_or_default();
if let Some(rule) = rules.get(&plugin_name) {
rules_to_replace.push(RuleWithSeverity::new(
rule.read_json(config),
rule.severity,
));
}
// If the given rule is not found in the rule list (for example, if all rules are disabled),
// then look it up in the entire rules list and add it.
else if let Some(rule) = all_rules
.iter()
.find(|r| r.name() == rule_name && r.plugin_name() == plugin_name)
{
rules_to_replace.push(RuleWithSeverity::new(
rule.read_json(config),
rule_config.severity,
));
}
} else if let Some(&rule) = rules.get(&plugin_name) {
rules_to_remove.push(rule.clone());
}
}
Expand All @@ -152,17 +167,12 @@ fn transform_rule_and_plugin_name<'a>(
rule_name: &'a str,
plugin_name: &'a str,
) -> (&'a str, &'a str) {
if plugin_name == "vitest" && is_jest_rule_adapted_to_vitest(rule_name) {
return (rule_name, "jest");
}

if plugin_name == "typescript" && is_eslint_rule_adapted_to_typescript(rule_name) {
return (rule_name, "eslint");
}

if plugin_name == "unicorn" && rule_name == "no-negated-condition" {
return ("no-negated-condition", "eslint");
}
let plugin_name = match plugin_name {
"vitest" if is_jest_rule_adapted_to_vitest(rule_name) => "jest",
"unicorn" if rule_name == "no-negated-condition" => "eslint",
"typescript" if is_eslint_rule_adapted_to_typescript(rule_name) => "eslint",
_ => plugin_name,
};

(rule_name, plugin_name)
}
Expand Down Expand Up @@ -494,4 +504,18 @@ mod test {
assert_eq!(rule.severity, AllowWarnDeny::Warn, "{config:?}");
}
}

#[test]
fn test_two_rules_with_same_name_and_different_plugin_names() {
// Issue: <https://github.com/oxc-project/oxc/issues/8485>
let configs =
[json!({ "eslint/no-nested-ternary": "off", "unicorn/no-nested-ternary": "off" })];

for config in &configs {
let mut rules = RuleSet::default();
r#override(&mut rules, config);

assert_eq!(rules.len(), 0, "{config:?}");
}
}
}

0 comments on commit 89fc613

Please sign in to comment.