Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_duplicate_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl Rule for NoDuplicateCase {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::SwitchStatement)
}
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl Rule for NoEmptyCharacterClass {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::RegExpLiteral)
}
}

struct EmptyClassFinder {
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_empty_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ impl Rule for NoEmptyPattern {
};
ctx.diagnostic(no_empty_pattern_diagnostic(pattern_type, span));
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic()
.nodes()
.contains_any(&[oxc_ast::AstType::ObjectPattern, oxc_ast::AstType::ArrayPattern])
}
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_empty_static_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ impl Rule for NoEmptyStaticBlock {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::StaticBlock)
}
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_ex_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl Rule for NoExAssign {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic()
.nodes()
.contains_any(&[oxc_ast::AstType::TryStatement, oxc_ast::AstType::CatchClause])
}
}

#[test]
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_extra_boolean_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ impl Rule for NoExtraBooleanCast {
_ => {}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic()
.nodes()
.contains_any(&[oxc_ast::AstType::CallExpression, oxc_ast::AstType::UnaryExpression])
}
}

// Checks whether the node is a context that should report an error
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_func_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ impl Rule for NoFuncAssign {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::Function)
}
}

#[test]
Expand Down
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_global_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ impl Rule for NoGlobalAssign {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains_any(&[
oxc_ast::AstType::AssignmentExpression,
oxc_ast::AstType::UpdateExpression,
])
}
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_import_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ impl Rule for NoImportAssign {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::ImportDeclaration)
}
}

/// Check if a given node is at the first argument of a well-known mutation function.
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ impl Rule for NoInvalidRegexp {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic()
.nodes()
.contains_any(&[oxc_ast::AstType::NewExpression, oxc_ast::AstType::CallExpression])
}
}

fn parse_arguments_to_check<'a>(
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_loss_of_precision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ impl Rule for NoLossOfPrecision {
_ => {}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::NumericLiteral)
}
}

pub struct RawNum<'a> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ impl Rule for NoNewNativeNonconstructor {
));
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::NewExpression)
}
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ impl Rule for NoNonoctalDecimalEscape {
check_string(ctx, literal.span.source_text(ctx.source_text()));
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::StringLiteral)
}
}
trait StickyRegex {
fn sticky_captures<'h>(&self, haystack: &'h str, start: usize)
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_obj_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ impl Rule for NoObjCalls {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic()
.nodes()
.contains_any(&[oxc_ast::AstType::NewExpression, oxc_ast::AstType::CallExpression])
}
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_self_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ impl Rule for NoSelfAssign {
self.each_self_assignment(&assignment.left, &assignment.right, ctx);
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::AssignmentExpression)
}
}

impl NoSelfAssign {
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_setter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ impl Rule for NoSetterReturn {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::ReturnStatement)
&& ctx.semantic().nodes().contains_any(&[
oxc_ast::AstType::ObjectProperty,
oxc_ast::AstType::MethodDefinition,
])
}
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_sparse_arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl Rule for NoSparseArrays {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::ArrayExpression)
}
}

#[test]
Expand Down
8 changes: 8 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_this_before_super.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ impl Rule for NoThisBeforeSuper {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::Class)
&& ctx
.semantic()
.nodes()
.contains_any(&[oxc_ast::AstType::Super, oxc_ast::AstType::ThisExpression])
}
}

impl NoThisBeforeSuper {
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unsafe_finally.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ impl Rule for NoUnsafeFinally {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic().nodes().contains(oxc_ast::AstType::TryStatement)
}
}

#[derive(Debug, Clone, Copy)]
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/eslint/no_unsafe_negation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ impl Rule for NoUnsafeNegation {
}
}
}

fn should_run(&self, ctx: &crate::context::ContextHost) -> bool {
ctx.semantic()
.nodes()
.contains_all(&[oxc_ast::AstType::BinaryExpression, oxc_ast::AstType::UnaryExpression])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The contains_all check here is too restrictive - it requires both BinaryExpression AND UnaryExpression to be present in the AST for the rule to run. However, the rule is designed to detect unsafe negation patterns in binary expressions, which may or may not contain unary expressions.

Consider changing this to contains(oxc_ast::AstType::BinaryExpression) instead, as this would correctly run the rule whenever binary expressions exist in the code, regardless of whether unary expressions are also present.

Suggested change
.contains_all(&[oxc_ast::AstType::BinaryExpression, oxc_ast::AstType::UnaryExpression])
.contains(oxc_ast::AstType::BinaryExpression)

Spotted by Diamond

Is this helpful? React 👍 or 👎 to let us know.

}
}

#[test]
Expand Down
Loading