diff --git a/crates/oxc_linter/src/rules/eslint/no_unsafe_optional_chaining.rs b/crates/oxc_linter/src/rules/eslint/no_unsafe_optional_chaining.rs index f77f7428a7ed8..48d1af2a6dcb3 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unsafe_optional_chaining.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unsafe_optional_chaining.rs @@ -145,6 +145,10 @@ impl Rule for NoUnsafeOptionalChaining { _ => {} } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::ChainExpression) + } } #[derive(Clone, Copy)] diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_labels.rs b/crates/oxc_linter/src/rules/eslint/no_unused_labels.rs index a015c35e6a454..575d0fdae35ac 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_labels.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_labels.rs @@ -65,7 +65,8 @@ impl Rule for NoUnusedLabels { } fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { - ctx.file_path().extension().is_some_and(|ext| ext != "svelte") + ctx.semantic().nodes().contains(oxc_ast::AstType::LabeledStatement) + && ctx.file_path().extension().is_some_and(|ext| ext != "svelte") } } diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs b/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs index 857345e2b9d6e..ff8bb6d00d786 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs @@ -110,6 +110,12 @@ impl Rule for NoUnusedPrivateClassMembers { } }); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic() + .nodes() + .contains_all(&[oxc_ast::AstType::Class, oxc_ast::AstType::PrivateIdentifier]) + } } fn is_read(current_node_id: NodeId, nodes: &AstNodes) -> bool { diff --git a/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs b/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs index 60fb64f27723d..01e1bb6722ae7 100644 --- a/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs +++ b/crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs @@ -87,6 +87,14 @@ impl Rule for NoUselessBackreference { } }); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains_any(&[ + oxc_ast::AstType::RegExpLiteral, + oxc_ast::AstType::NewExpression, + oxc_ast::AstType::CallExpression, + ]) + } } enum Problem { diff --git a/crates/oxc_linter/src/rules/eslint/no_useless_catch.rs b/crates/oxc_linter/src/rules/eslint/no_useless_catch.rs index 69597ad45f7ef..c9d5fd74732ea 100644 --- a/crates/oxc_linter/src/rules/eslint/no_useless_catch.rs +++ b/crates/oxc_linter/src/rules/eslint/no_useless_catch.rs @@ -83,6 +83,12 @@ impl Rule for NoUselessCatch { } } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic() + .nodes() + .contains_all(&[oxc_ast::AstType::TryStatement, oxc_ast::AstType::CatchClause]) + } } #[test] diff --git a/crates/oxc_linter/src/rules/eslint/no_useless_escape.rs b/crates/oxc_linter/src/rules/eslint/no_useless_escape.rs index 5e0c38634809d..9e95404747a85 100644 --- a/crates/oxc_linter/src/rules/eslint/no_useless_escape.rs +++ b/crates/oxc_linter/src/rules/eslint/no_useless_escape.rs @@ -150,6 +150,14 @@ impl Rule for NoUselessEscape { Self(Box::new(NoUselessEscapeConfig { allow_regex_characters })) } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains_any(&[ + oxc_ast::AstType::RegExpLiteral, + oxc_ast::AstType::StringLiteral, + oxc_ast::AstType::TemplateLiteral, + ]) + } } fn is_within_jsx_attribute(id: NodeId, ctx: &LintContext) -> bool { diff --git a/crates/oxc_linter/src/rules/eslint/no_useless_rename.rs b/crates/oxc_linter/src/rules/eslint/no_useless_rename.rs index 26240cdb592f4..ceac51b650447 100644 --- a/crates/oxc_linter/src/rules/eslint/no_useless_rename.rs +++ b/crates/oxc_linter/src/rules/eslint/no_useless_rename.rs @@ -164,6 +164,15 @@ impl Rule for NoUselessRename { _ => {} } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains_any(&[ + oxc_ast::AstType::ObjectPattern, + oxc_ast::AstType::AssignmentTarget, + oxc_ast::AstType::ImportSpecifier, + oxc_ast::AstType::ExportNamedDeclaration, + ]) + } } #[test] diff --git a/crates/oxc_linter/src/rules/eslint/no_with.rs b/crates/oxc_linter/src/rules/eslint/no_with.rs index 90d8d548f2e35..3ddb3be7f6e46 100644 --- a/crates/oxc_linter/src/rules/eslint/no_with.rs +++ b/crates/oxc_linter/src/rules/eslint/no_with.rs @@ -42,6 +42,10 @@ impl Rule for NoWith { ctx.diagnostic(no_with_diagnostic(Span::sized(with_statement.span.start, 4))); } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::WithStatement) + } } #[test] diff --git a/crates/oxc_linter/src/rules/eslint/require_yield.rs b/crates/oxc_linter/src/rules/eslint/require_yield.rs index c8ce83f0fc32b..db3e258e5d0af 100644 --- a/crates/oxc_linter/src/rules/eslint/require_yield.rs +++ b/crates/oxc_linter/src/rules/eslint/require_yield.rs @@ -46,6 +46,10 @@ impl Rule for RequireYield { } } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::Function) + } } #[test] diff --git a/crates/oxc_linter/src/rules/eslint/use_isnan.rs b/crates/oxc_linter/src/rules/eslint/use_isnan.rs index 187538a5d311d..df9ac5b4b4d9e 100644 --- a/crates/oxc_linter/src/rules/eslint/use_isnan.rs +++ b/crates/oxc_linter/src/rules/eslint/use_isnan.rs @@ -152,6 +152,15 @@ impl Rule for UseIsnan { Self { enforce_for_switch_case, enforce_for_index_of } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains_any(&[ + oxc_ast::AstType::BinaryExpression, + oxc_ast::AstType::SwitchCase, + oxc_ast::AstType::SwitchStatement, + oxc_ast::AstType::CallExpression, + ]) + } } fn is_nan_identifier<'a>(expr: &'a Expression<'a>) -> bool { diff --git a/crates/oxc_linter/src/rules/eslint/valid_typeof.rs b/crates/oxc_linter/src/rules/eslint/valid_typeof.rs index 44ceaa8d4f718..0d00635fd04a8 100644 --- a/crates/oxc_linter/src/rules/eslint/valid_typeof.rs +++ b/crates/oxc_linter/src/rules/eslint/valid_typeof.rs @@ -171,6 +171,12 @@ impl Rule for ValidTypeof { Self { require_string_literals } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic() + .nodes() + .contains_all(&[oxc_ast::AstType::UnaryExpression, oxc_ast::AstType::BinaryExpression]) + } } const VALID_TYPES: [&str; 8] = diff --git a/crates/oxc_linter/src/rules/import/default.rs b/crates/oxc_linter/src/rules/import/default.rs index 8dd7dcd7a10ca..fc4616e0e21c9 100644 --- a/crates/oxc_linter/src/rules/import/default.rs +++ b/crates/oxc_linter/src/rules/import/default.rs @@ -82,6 +82,13 @@ impl Rule for Default { } } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains_any(&[ + oxc_ast::AstType::ImportDefaultSpecifier, + oxc_ast::AstType::ExportDefaultDeclaration, + ]) + } } #[test] diff --git a/crates/oxc_linter/src/rules/import/namespace.rs b/crates/oxc_linter/src/rules/import/namespace.rs index 5df3c44ddb20c..eaac566c3f0b6 100644 --- a/crates/oxc_linter/src/rules/import/namespace.rs +++ b/crates/oxc_linter/src/rules/import/namespace.rs @@ -218,6 +218,10 @@ impl Rule for Namespace { }); } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::ImportDeclaration) + } } /// If the name is a namespace object in imported module, return the module request name. diff --git a/crates/oxc_linter/src/rules/jest/expect_expect.rs b/crates/oxc_linter/src/rules/jest/expect_expect.rs index 361f76e431a7c..9c2f45b1ecd9e 100644 --- a/crates/oxc_linter/src/rules/jest/expect_expect.rs +++ b/crates/oxc_linter/src/rules/jest/expect_expect.rs @@ -133,6 +133,10 @@ impl Rule for ExpectExpect { ) { run(self, jest_node, ctx); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } fn run<'a>( diff --git a/crates/oxc_linter/src/rules/jest/no_conditional_expect.rs b/crates/oxc_linter/src/rules/jest/no_conditional_expect.rs index 868624b049e50..5dcb86babded5 100644 --- a/crates/oxc_linter/src/rules/jest/no_conditional_expect.rs +++ b/crates/oxc_linter/src/rules/jest/no_conditional_expect.rs @@ -126,6 +126,10 @@ impl Rule for NoConditionalExpect { } } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } fn check_parents<'a>( diff --git a/crates/oxc_linter/src/rules/jest/no_disabled_tests.rs b/crates/oxc_linter/src/rules/jest/no_disabled_tests.rs index af9c692a7643f..4626e3f563411 100644 --- a/crates/oxc_linter/src/rules/jest/no_disabled_tests.rs +++ b/crates/oxc_linter/src/rules/jest/no_disabled_tests.rs @@ -99,6 +99,10 @@ impl Rule for NoDisabledTests { ) { run(jest_node, ctx); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>) { diff --git a/crates/oxc_linter/src/rules/jest/no_export.rs b/crates/oxc_linter/src/rules/jest/no_export.rs index e3c56c3a2c9ca..4ee2882ca8f15 100644 --- a/crates/oxc_linter/src/rules/jest/no_export.rs +++ b/crates/oxc_linter/src/rules/jest/no_export.rs @@ -53,6 +53,15 @@ impl Rule for NoExport { ctx.diagnostic(no_export_diagnostic(span)); } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains_any(&[ + oxc_ast::AstType::ExportNamedDeclaration, + oxc_ast::AstType::ExportDefaultDeclaration, + oxc_ast::AstType::ExportAllDeclaration, + oxc_ast::AstType::ExportSpecifier, + ]) + } } #[test] diff --git a/crates/oxc_linter/src/rules/jest/no_focused_tests.rs b/crates/oxc_linter/src/rules/jest/no_focused_tests.rs index ebdcaad550d4a..e14ed6e59579e 100644 --- a/crates/oxc_linter/src/rules/jest/no_focused_tests.rs +++ b/crates/oxc_linter/src/rules/jest/no_focused_tests.rs @@ -76,6 +76,10 @@ impl Rule for NoFocusedTests { ) { run(jest_node, ctx); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>) { diff --git a/crates/oxc_linter/src/rules/jest/no_standalone_expect/mod.rs b/crates/oxc_linter/src/rules/jest/no_standalone_expect/mod.rs index a61de4a70c126..01993f752831a 100644 --- a/crates/oxc_linter/src/rules/jest/no_standalone_expect/mod.rs +++ b/crates/oxc_linter/src/rules/jest/no_standalone_expect/mod.rs @@ -86,6 +86,10 @@ impl Rule for NoStandaloneExpect { self.run(possible_jest_node, &id_nodes_mapping, ctx); } } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } impl NoStandaloneExpect { diff --git a/crates/oxc_linter/src/rules/jest/require_to_throw_message.rs b/crates/oxc_linter/src/rules/jest/require_to_throw_message.rs index b5ea6b52729e0..0564d01fec703 100644 --- a/crates/oxc_linter/src/rules/jest/require_to_throw_message.rs +++ b/crates/oxc_linter/src/rules/jest/require_to_throw_message.rs @@ -57,6 +57,10 @@ impl Rule for RequireToThrowMessage { ) { Self::run(jest_node, ctx); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } impl RequireToThrowMessage { diff --git a/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs b/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs index 8e02ba96c6c79..6cff9560ffc17 100644 --- a/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs +++ b/crates/oxc_linter/src/rules/jest/valid_describe_callback.rs @@ -82,6 +82,10 @@ impl Rule for ValidDescribeCallback { ) { run(jest_node, ctx); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>) { diff --git a/crates/oxc_linter/src/rules/jest/valid_expect.rs b/crates/oxc_linter/src/rules/jest/valid_expect.rs index f49d1d6598fed..22ac2bc59e2c3 100644 --- a/crates/oxc_linter/src/rules/jest/valid_expect.rs +++ b/crates/oxc_linter/src/rules/jest/valid_expect.rs @@ -131,6 +131,10 @@ impl Rule for ValidExpect { ) { self.run(jest_node, ctx); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } impl ValidExpect { diff --git a/crates/oxc_linter/src/rules/jest/valid_title.rs b/crates/oxc_linter/src/rules/jest/valid_title.rs index 5588ca55ae863..580976326ac0b 100644 --- a/crates/oxc_linter/src/rules/jest/valid_title.rs +++ b/crates/oxc_linter/src/rules/jest/valid_title.rs @@ -139,6 +139,10 @@ impl Rule for ValidTitle { ) { self.run(jest_node, ctx); } + + fn should_run(&self, ctx: &crate::context::ContextHost) -> bool { + ctx.semantic().nodes().contains(oxc_ast::AstType::CallExpression) + } } impl ValidTitle {