Skip to content

Commit 8ce21d1

Browse files
authoredJan 31, 2025··
fix(linter): can't disable no-nested-ternary rule anymore (#8600)
closes #8485 Since we currently support two rules with the same `rule_name` but different `plugin_names`, some of the original logic is no longer applicable. As a result, I have made some adjustments.
1 parent e929f26 commit 8ce21d1

File tree

5 files changed

+73
-33
lines changed

5 files changed

+73
-33
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"plugins": [
3+
"oxc",
4+
"unicorn"
5+
],
6+
"rules": {
7+
"eslint/no-nested-ternary": "off",
8+
"unicorn/no-nested-ternary": "off"
9+
}
10+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log(bar ? baz : qux === quxx ? bing : bam);

Diff for: ‎apps/oxlint/src/lint.rs

+7
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,13 @@ mod test {
824824
.test_and_snapshot_multiple(&[args_1, args_2]);
825825
}
826826

827+
#[test]
828+
fn test_two_rules_with_same_name_from_different_plugin_names() {
829+
// Issue: <https://github.com/oxc-project/oxc/issues/8485>
830+
let args = &["-c", ".oxlintrc.json", "test.js"];
831+
Tester::new().with_cwd("fixtures/two_rules_with_same_name".into()).test_and_snapshot(args);
832+
}
833+
827834
#[test]
828835
fn test_adjust_ignore_patterns() {
829836
let base = PathBuf::from("/project/root");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: apps/oxlint/src/tester.rs
3+
---
4+
##########
5+
arguments: -c .oxlintrc.json test.js
6+
working directory: fixtures/two_rules_with_same_name
7+
----------
8+
Found 0 warnings and 0 errors.
9+
Finished in <variable>ms on 1 file with 74 rules using 1 threads.
10+
----------
11+
CLI result: LintSucceeded
12+
----------

Diff for: ‎crates/oxc_linter/src/config/rules.rs

+43-33
Original file line numberDiff line numberDiff line change
@@ -110,28 +110,43 @@ impl OxlintRules {
110110
}
111111
}
112112
_ => {
113-
// For overlapping rule names, use the "error" one
114-
// "no-loss-of-precision": "off",
115-
// "@typescript-eslint/no-loss-of-precision": "error"
116-
if let Some(rule_config) =
117-
rule_configs.iter().find(|r| r.severity.is_warn_deny())
118-
{
119-
let config = rule_config.config.clone().unwrap_or_default();
120-
121-
if let Some(rule) = rules_for_override.iter().find(|r| r.name() == *name) {
122-
rules_to_replace
123-
.push(RuleWithSeverity::new(rule.read_json(config), rule.severity));
124-
}
125-
// If the given rule is not found in the rule list (for example, if all rules are disabled),
126-
// then look it up in the entire rules list and add it.
127-
else if let Some(rule) = all_rules.iter().find(|r| r.name() == *name) {
128-
rules_to_replace.push(RuleWithSeverity::new(
129-
rule.read_json(config),
130-
rule_config.severity,
131-
));
132-
}
133-
} else if rule_configs.iter().all(|r| r.severity.is_allow()) {
134-
if let Some(rule) = rules_for_override.iter().find(|r| r.name() == *name) {
113+
let rules = rules_for_override
114+
.iter()
115+
.filter_map(|r| {
116+
if r.name() == *name {
117+
Some((r.plugin_name(), r))
118+
} else {
119+
None
120+
}
121+
})
122+
.collect::<FxHashMap<_, _>>();
123+
124+
for rule_config in rule_configs {
125+
let (rule_name, plugin_name) = transform_rule_and_plugin_name(
126+
&rule_config.rule_name,
127+
&rule_config.plugin_name,
128+
);
129+
130+
if rule_config.severity.is_warn_deny() {
131+
let config = rule_config.config.clone().unwrap_or_default();
132+
if let Some(rule) = rules.get(&plugin_name) {
133+
rules_to_replace.push(RuleWithSeverity::new(
134+
rule.read_json(config),
135+
rule.severity,
136+
));
137+
}
138+
// If the given rule is not found in the rule list (for example, if all rules are disabled),
139+
// then look it up in the entire rules list and add it.
140+
else if let Some(rule) = all_rules
141+
.iter()
142+
.find(|r| r.name() == rule_name && r.plugin_name() == plugin_name)
143+
{
144+
rules_to_replace.push(RuleWithSeverity::new(
145+
rule.read_json(config),
146+
rule_config.severity,
147+
));
148+
}
149+
} else if let Some(&rule) = rules.get(&plugin_name) {
135150
rules_to_remove.push(rule.clone());
136151
}
137152
}
@@ -152,17 +167,12 @@ fn transform_rule_and_plugin_name<'a>(
152167
rule_name: &'a str,
153168
plugin_name: &'a str,
154169
) -> (&'a str, &'a str) {
155-
if plugin_name == "vitest" && is_jest_rule_adapted_to_vitest(rule_name) {
156-
return (rule_name, "jest");
157-
}
158-
159-
if plugin_name == "typescript" && is_eslint_rule_adapted_to_typescript(rule_name) {
160-
return (rule_name, "eslint");
161-
}
162-
163-
if plugin_name == "unicorn" && rule_name == "no-negated-condition" {
164-
return ("no-negated-condition", "eslint");
165-
}
170+
let plugin_name = match plugin_name {
171+
"vitest" if is_jest_rule_adapted_to_vitest(rule_name) => "jest",
172+
"unicorn" if rule_name == "no-negated-condition" => "eslint",
173+
"typescript" if is_eslint_rule_adapted_to_typescript(rule_name) => "eslint",
174+
_ => plugin_name,
175+
};
166176

167177
(rule_name, plugin_name)
168178
}

0 commit comments

Comments
 (0)
Please sign in to comment.