Skip to content

Commit

Permalink
feat(linter): allow lint rules with the same name (oxc-project#7496)
Browse files Browse the repository at this point in the history
Apologies @Boshen for the stupidly large diff.

I've done the following:

 - `RuleEnum`'s members are now prefixed with the plugin name. e.g. `NoDebugger` has become `EslintNoDebugger`
- updated tester.rs to accept the rule's NAME, CATEGORY to allow us to test rules with the same name (diff category)
- updates `declare_all_lint_rules` satisfy the first above change.
  • Loading branch information
camc314 committed Nov 27, 2024
1 parent bd2ce02 commit bd0693b
Show file tree
Hide file tree
Showing 953 changed files with 1,081 additions and 556 deletions.
5 changes: 4 additions & 1 deletion crates/oxc_linter/src/config/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ mod test {

#[allow(clippy::default_trait_access)]
fn no_explicit_any() -> RuleWithSeverity {
RuleWithSeverity::new(RuleEnum::NoExplicitAny(Default::default()), AllowWarnDeny::Warn)
RuleWithSeverity::new(
RuleEnum::TypescriptNoExplicitAny(Default::default()),
AllowWarnDeny::Warn,
)
}

/// an empty ruleset is a no-op
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/config/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ mod test {
fn test_override_allow() {
let mut rules = RuleSet::default();
rules.insert(RuleWithSeverity {
rule: RuleEnum::NoConsole(Default::default()),
rule: RuleEnum::EslintNoConsole(Default::default()),
severity: AllowWarnDeny::Deny,
});
r#override(&mut rules, &json!({ "eslint/no-console": "off" }));
Expand Down Expand Up @@ -461,7 +461,7 @@ mod test {
for config in &configs {
let mut rules = RuleSet::default();
rules.insert(RuleWithSeverity {
rule: RuleEnum::NoUnusedVars(Default::default()),
rule: RuleEnum::EslintNoUnusedVars(Default::default()),
severity: AllowWarnDeny::Warn,
});
r#override(&mut rules, config);
Expand Down
6 changes: 4 additions & 2 deletions crates/oxc_linter/src/disable_directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl<'a> DisableDirectivesBuilder<'a> {

#[test]
fn test() {
use crate::tester::Tester;
use crate::{rule::RuleMeta, rules::EslintNoDebugger, tester::Tester};

for prefix in ["eslint", "oxlint"] {
// [Disabling Rules](https://eslint.org/docs/latest/use/configure/rules#disabling-rules)
Expand Down Expand Up @@ -599,6 +599,8 @@ semi*/
),
];

Tester::new("no-debugger", pass, fail).intentionally_allow_no_fix_tests().test();
Tester::new(EslintNoDebugger::NAME, EslintNoDebugger::CATEGORY, pass, fail)
.intentionally_allow_no_fix_tests()
.test();
}
}
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ mod import {
pub mod default;
pub mod export;
pub mod first;
pub mod import_no_namespace;
pub mod max_dependencies;
pub mod named;
pub mod namespace;
Expand All @@ -24,6 +23,7 @@ mod import {
pub mod no_dynamic_require;
pub mod no_named_as_default;
pub mod no_named_as_default_member;
pub mod no_namespace;
pub mod no_self_import;
pub mod no_webpack_loader_syntax;
pub mod unambiguous;
Expand Down Expand Up @@ -639,7 +639,7 @@ oxc_macros::declare_all_lint_rules! {
import::default,
import::export,
import::first,
import::import_no_namespace,
import::no_namespace,
import::max_dependencies,
import::named,
import::namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,5 +580,6 @@ fn test() {
("foo?.filter((function() { return () => { console.log('hello') } })?.())", None),
];

Tester::new(ArrayCallbackReturn::NAME, pass, fail).test_and_snapshot();
Tester::new(ArrayCallbackReturn::NAME, ArrayCallbackReturn::CATEGORY, pass, fail)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/constructor_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ fn test() {
// ("class A extends 'test' { constructor() { super(); } }", None),
];

Tester::new(ConstructorSuper::NAME, pass, fail).test_and_snapshot();
Tester::new(ConstructorSuper::NAME, ConstructorSuper::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/default_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,5 @@ fn test() {
),
];

Tester::new(DefaultCase::NAME, pass, fail).test_and_snapshot();
Tester::new(DefaultCase::NAME, DefaultCase::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/default_case_last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,5 @@ fn test() {
r"switch (foo) { case 1: default: case 2: }",
];

Tester::new(DefaultCaseLast::NAME, pass, fail).test_and_snapshot();
Tester::new(DefaultCaseLast::NAME, DefaultCaseLast::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/default_param_last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ fn test() {
"const f = ([a, b] = [1, 2], c) => {}",
];

Tester::new(DefaultParamLast::NAME, pass, fail).test_and_snapshot();
Tester::new(DefaultParamLast::NAME, DefaultParamLast::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/eqeqeq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,5 @@ fn test() {
("a == b", "a == b", None),
];

Tester::new(Eqeqeq::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
Tester::new(Eqeqeq::NAME, Eqeqeq::CATEGORY, pass, fail).expect_fix(fix).test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/for_direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,5 +320,7 @@ fn test() {
("for(var ii = 10; ii > 0; ii+=1){}", "for(var ii = 10; ii > 0; ii-=1){}", None),
];

Tester::new(ForDirection::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
Tester::new(ForDirection::NAME, ForDirection::CATEGORY, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,5 +798,7 @@ fn test() {
),
];

Tester::new(FuncNames::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
Tester::new(FuncNames::NAME, FuncNames::CATEGORY, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ fn test() {
),
];

Tester::new(GetterReturn::NAME, pass, fail)
Tester::new(GetterReturn::NAME, GetterReturn::CATEGORY, pass, fail)
.change_rule_path_extension("js")
.test_and_snapshot();

Expand All @@ -537,5 +537,5 @@ fn test() {

let fail = vec![];

Tester::new(GetterReturn::NAME, pass, fail).test();
Tester::new(GetterReturn::NAME, GetterReturn::CATEGORY, pass, fail).test();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/guard_for_in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ fn test() {
"for (var x in o) foo();",
];

Tester::new(GuardForIn::NAME, pass, fail).test_and_snapshot();
Tester::new(GuardForIn::NAME, GuardForIn::CATEGORY, pass, fail).test_and_snapshot();
}
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/max_classes_per_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,6 @@ fn test() {
),
];

Tester::new(MaxClassesPerFile::NAME, pass, fail).test_and_snapshot();
Tester::new(MaxClassesPerFile::NAME, MaxClassesPerFile::CATEGORY, pass, fail)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/max_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,5 +416,5 @@ fn test() {
),
];

Tester::new(MaxLines::NAME, pass, fail).test_and_snapshot();
Tester::new(MaxLines::NAME, MaxLines::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/max_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,5 @@ fn test() {
),
];

Tester::new(MaxParams::NAME, pass, fail).test_and_snapshot();
Tester::new(MaxParams::NAME, MaxParams::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_alert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,5 @@ fn test() {
"(window?.alert)(foo)", // { "ecmaVersion": 2020 }
];

Tester::new(NoAlert::NAME, pass, fail).test_and_snapshot();
Tester::new(NoAlert::NAME, NoAlert::CATEGORY, pass, fail).test_and_snapshot();
}
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_array_constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,6 @@ fn test() {
("Array(0, 1, 2)", None),
];

Tester::new(NoArrayConstructor::NAME, pass, fail).test_and_snapshot();
Tester::new(NoArrayConstructor::NAME, NoArrayConstructor::CATEGORY, pass, fail)
.test_and_snapshot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,6 @@ fn test() {
("new Promise(((((async () => {})))))", None),
];

Tester::new(NoAsyncPromiseExecutor::NAME, pass, fail).test_and_snapshot();
Tester::new(NoAsyncPromiseExecutor::NAME, NoAsyncPromiseExecutor::CATEGORY, pass, fail)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_await_in_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,5 @@ fn test() {
"async function foo() { for await (var x of xs) { while (1) await f(x) } }",
];

Tester::new(NoAwaitInLoop::NAME, pass, fail).test_and_snapshot();
Tester::new(NoAwaitInLoop::NAME, NoAwaitInLoop::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_bitwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,5 @@ fn test() {
("a >>>= b", None),
];

Tester::new(NoBitwise::NAME, pass, fail).test_and_snapshot();
Tester::new(NoBitwise::NAME, NoBitwise::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_caller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ fn test() {

let fail = vec![("var x = arguments.callee", None), ("var x = arguments.caller", None)];

Tester::new(NoCaller::NAME, pass, fail).test_and_snapshot();
Tester::new(NoCaller::NAME, NoCaller::CATEGORY, pass, fail).test_and_snapshot();
}
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_case_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,6 @@ fn test() {
("switch (a) { default: class C {} break; }", None),
];

Tester::new(NoCaseDeclarations::NAME, pass, fail).test_and_snapshot();
Tester::new(NoCaseDeclarations::NAME, NoCaseDeclarations::CATEGORY, pass, fail)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_class_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,5 @@ fn test() {
("if (foo) { class A {} A = 1; }", None),
];

Tester::new(NoClassAssign::NAME, pass, fail).test_and_snapshot();
Tester::new(NoClassAssign::NAME, NoClassAssign::CATEGORY, pass, fail).test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_compare_neg_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,7 @@ fn test() {
("-0n <= x", "0n <= x", None),
];

Tester::new(NoCompareNegZero::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
Tester::new(NoCompareNegZero::NAME, NoCompareNegZero::CATEGORY, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_cond_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,5 @@ fn test() {
("(((3496.29)).bkufyydt = 2e308) ? foo : bar;", None),
];

Tester::new(NoCondAssign::NAME, pass, fail).test_and_snapshot();
Tester::new(NoCondAssign::NAME, NoCondAssign::CATEGORY, pass, fail).test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,7 @@ fn test() {
("const x = { foo: console.log(bar) }", "const x = { foo: undefined }", None),
];

Tester::new(NoConsole::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
Tester::new(NoConsole::NAME, NoConsole::CATEGORY, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_const_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ fn test() {
("const b = 0; ({a, ...b} = {a: 1, c: 2, d: 3})", None),
];

Tester::new(NoConstAssign::NAME, pass, fail).test_and_snapshot();
Tester::new(NoConstAssign::NAME, NoConstAssign::CATEGORY, pass, fail).test_and_snapshot();
}
Original file line number Diff line number Diff line change
Expand Up @@ -654,5 +654,6 @@ fn test() {
("window.abc ?? 'non-nullish' ?? anything", None),
];

Tester::new(NoConstantBinaryExpression::NAME, pass, fail).test_and_snapshot();
Tester::new(NoConstantBinaryExpression::NAME, NoConstantBinaryExpression::CATEGORY, pass, fail)
.test_and_snapshot();
}
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_constant_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,5 +405,6 @@ fn test() {
// ("function* foo() { for (let foo = 1 + 2 + 3 + (yield); true; baz) {}}", None),
];

Tester::new(NoConstantCondition::NAME, pass, fail).test_and_snapshot();
Tester::new(NoConstantCondition::NAME, NoConstantCondition::CATEGORY, pass, fail)
.test_and_snapshot();
}
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_constructor_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,6 @@ fn test() {
"class C { constructor(a) { if (!a) { return '' } else { a() } } }",
];

Tester::new(NoConstructorReturn::NAME, pass, fail).test_and_snapshot();
Tester::new(NoConstructorReturn::NAME, NoConstructorReturn::CATEGORY, pass, fail)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,5 @@ fn test() {
"var sum = 0, i = 0; myLabel: while(i < 10) { if(i <= 5) { i++; continue myLabel; } sum += i; i++; }",
];

Tester::new(NoContinue::NAME, pass, fail).test_and_snapshot();
Tester::new(NoContinue::NAME, NoContinue::CATEGORY, pass, fail).test_and_snapshot();
}
6 changes: 5 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_control_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ mod tests {
fn test_hex_literals() {
Tester::new(
NoControlRegex::NAME,
NoControlRegex::CATEGORY,
vec![
"x1f", // not a control sequence
r"new RegExp('\x20')", // control sequence in valid range
Expand All @@ -227,6 +228,7 @@ mod tests {
fn test_unicode_literals() {
Tester::new(
NoControlRegex::NAME,
NoControlRegex::CATEGORY,
vec![
r"u00", // not a control sequence
r"\u00ff", // in valid range
Expand Down Expand Up @@ -258,6 +260,7 @@ mod tests {
fn test_unicode_brackets() {
Tester::new(
NoControlRegex::NAME,
NoControlRegex::CATEGORY,
vec![
r"let r = /\u{0}/", // no unicode flag, this is valid
r"let r = /\u{ff}/u",
Expand Down Expand Up @@ -289,7 +292,7 @@ mod tests {
r"const r = /([a-z])\2/;",
r"const r = /([a-z])\0/;",
];
Tester::new(NoControlRegex::NAME, pass, fail)
Tester::new(NoControlRegex::NAME, NoControlRegex::CATEGORY, pass, fail)
.with_snapshot_suffix("capture-group-indexing")
.test_and_snapshot();
}
Expand All @@ -300,6 +303,7 @@ mod tests {
// https://github.com/eslint/eslint/blob/v9.9.1/tests/lib/rules/no-control-regex.js
Tester::new(
NoControlRegex::NAME,
NoControlRegex::CATEGORY,
vec![
"var regex = /x1f/;",
r"var regex = /\\x1f/",
Expand Down
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ fn test() {
("if (foo) { debugger }", "if (foo) { }", None),
];

Tester::new(NoDebugger::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
Tester::new(NoDebugger::NAME, NoDebugger::CATEGORY, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_delete_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ fn test() {

let fail = vec![("delete x", None)];

Tester::new(NoDeleteVar::NAME, pass, fail).test_and_snapshot();
Tester::new(NoDeleteVar::NAME, NoDeleteVar::CATEGORY, pass, fail).test_and_snapshot();
}
4 changes: 3 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_div_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ fn test() {
None,
)];

Tester::new(NoDivRegex::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
Tester::new(NoDivRegex::NAME, NoDivRegex::CATEGORY, pass, fail)
.expect_fix(fix)
.test_and_snapshot();
}
3 changes: 2 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_dupe_class_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,6 @@ fn test() {
"class A { foo; foo() {}}",
];

Tester::new(NoDupeClassMembers::NAME, pass, fail).test_and_snapshot();
Tester::new(NoDupeClassMembers::NAME, NoDupeClassMembers::CATEGORY, pass, fail)
.test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_dupe_else_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,5 +258,5 @@ fn test() {
("if (a && a) {} else if (a) {}", None),
];

Tester::new(NoDupeElseIf::NAME, pass, fail).test_and_snapshot();
Tester::new(NoDupeElseIf::NAME, NoDupeElseIf::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_dupe_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,5 @@ fn test() {
("var x = ({ '/(?<zero>0)/': 1, [/(?<zero>0)/]: 2 })", None),
];

Tester::new(NoDupeKeys::NAME, pass, fail).test_and_snapshot();
Tester::new(NoDupeKeys::NAME, NoDupeKeys::CATEGORY, pass, fail).test_and_snapshot();
}
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_duplicate_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ fn test() {
"var a = 1, f = function(s) { return { p1: s } }; switch (a) {case f(\na + 1 // comment\n).p1: break; case f(a+1)\n.p1: break; default: break;}",
];

Tester::new(NoDuplicateCase::NAME, pass, fail).test_and_snapshot();
Tester::new(NoDuplicateCase::NAME, NoDuplicateCase::CATEGORY, pass, fail).test_and_snapshot();
}
Loading

0 comments on commit bd0693b

Please sign in to comment.