Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(linter/unicorn): improve diagnostic message for no-null #5172

Merged
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
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