Skip to content

Commit

Permalink
refactor(linter/unicorn): clean up no-null (#5174)
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Aug 24, 2024
1 parent 7886618 commit 7ab6152
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions crates/oxc_linter/src/rules/unicorn/no_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::BinaryOperator;
use serde_json::Value;

use crate::{
ast_util::is_method_call,
Expand All @@ -23,7 +24,7 @@ fn no_null_diagnostic(null: Span) -> OxcDiagnostic {

#[derive(Debug, Default, Clone)]
pub struct NoNull {
check_strict_equality: Option<bool>,
check_strict_equality: bool,
}

declare_oxc_lint!(
Expand Down Expand Up @@ -61,35 +62,36 @@ fn match_null_arg(call_expr: &CallExpression, index: usize, span: Span) -> bool
})
}

fn diagnose_binary_expression(
no_null: &NoNull,
ctx: &LintContext,
null_literal: &NullLiteral,
binary_expr: &BinaryExpression,
) {
// checkStrictEquality=false && `if (foo !== null) {}`
if !no_null.check_strict_equality.is_some_and(|val| val)
&& matches!(
binary_expr.operator,
BinaryOperator::StrictEquality | BinaryOperator::StrictInequality
)
{
return;
}

// `if (foo != null) {}`
if matches!(binary_expr.operator, BinaryOperator::Equality | BinaryOperator::Inequality) {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
impl NoNull {
fn diagnose_binary_expression(
&self,
ctx: &LintContext,
null_literal: &NullLiteral,
binary_expr: &BinaryExpression,
) {
match binary_expr.operator {
// `if (foo != null) {}`
BinaryOperator::Equality | BinaryOperator::Inequality => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}

return;
// `if (foo !== null) {}`
BinaryOperator::StrictEquality | BinaryOperator::StrictInequality => {
if self.check_strict_equality {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
}
_ => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
}
}

// checkStrictEquality=true && `if (foo !== null) {}`
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}

fn diagnose_variable_declarator(
Expand Down Expand Up @@ -156,12 +158,13 @@ fn match_call_expression_pass_case(null_literal: &NullLiteral, call_expr: &CallE
}

impl Rule for NoNull {
fn from_configuration(value: serde_json::Value) -> Self {
fn from_configuration(value: Value) -> Self {
Self {
check_strict_equality: value
.get(0)
.and_then(|v| v.get("checkStrictEquality"))
.and_then(serde_json::Value::as_bool),
.and_then(Value::as_bool)
.unwrap_or_default(),
}
}

Expand All @@ -182,7 +185,7 @@ impl Rule for NoNull {
}

if let AstKind::BinaryExpression(binary_expr) = parent_node.kind() {
diagnose_binary_expression(self, ctx, null_literal, binary_expr);
self.diagnose_binary_expression(ctx, null_literal, binary_expr);
return;
}

Expand Down

0 comments on commit 7ab6152

Please sign in to comment.