Skip to content

Commit

Permalink
fix(linter/unicorn): improve diagnostic message for no-null (#5172)
Browse files Browse the repository at this point in the history
This stack was made because of shortcomings I noticed while running `oxlint-eccosystem-ci` tests locally
  • Loading branch information
DonIsaac committed Aug 24, 2024
1 parent dc9e1e2 commit b629e16
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 89 deletions.
26 changes: 9 additions & 17 deletions crates/oxc_linter/src/rules/unicorn/no_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@ use crate::{
AstNode,
};

fn replace_null_diagnostic(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow the use of the `null` literal")
.with_help("Replace the `null` literal with `undefined`.")
.with_label(span0)
}

fn remove_null_diagnostic(span0: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow the use of the `null` literal")
.with_help("Remove the `null` literal.")
.with_label(span0)
fn no_null_diagnostic(null: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not use `null` literals").with_label(null)
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -56,7 +48,7 @@ declare_oxc_lint!(
/// ```
NoNull,
style,
fix
conditional_fix
);

fn match_null_arg(call_expr: &CallExpression, index: usize, span: Span) -> bool {
Expand Down Expand Up @@ -87,15 +79,15 @@ fn diagnose_binary_expression(

// `if (foo != null) {}`
if matches!(binary_expr.operator, BinaryOperator::Equality | BinaryOperator::Inequality) {
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});

return;
}

// checkStrictEquality=true && `if (foo !== null) {}`
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
Expand All @@ -110,15 +102,15 @@ fn diagnose_variable_declarator(
if matches!(&variable_declarator.init, Some(Expression::NullLiteral(expr)) if expr.span == null_literal.span)
&& matches!(parent_kind, Some(AstKind::VariableDeclaration(var_declaration)) if !var_declaration.kind.is_const() )
{
ctx.diagnostic_with_fix(remove_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fixer.delete_range(Span::new(variable_declarator.id.span().end, null_literal.span.end))
});

return;
}

// `const foo = null`
ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
Expand Down Expand Up @@ -207,15 +199,15 @@ impl Rule for NoNull {

// `function foo() { return null; }`,
if matches!(parent_node.kind(), AstKind::ReturnStatement(_)) {
ctx.diagnostic_with_fix(remove_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fixer.delete_range(null_literal.span)
});

return;
}
}

ctx.diagnostic_with_fix(replace_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
Expand Down
Loading

0 comments on commit b629e16

Please sign in to comment.