Skip to content
Merged
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
25 changes: 13 additions & 12 deletions crates/oxc_linter/src/rules/unicorn/no_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ declare_oxc_lint!(
NoNull,
unicorn,
style,
conditional_fix,
conditional_dangerous_fix,
config = NoNull
);

Expand All @@ -84,21 +84,22 @@ impl NoNull {
match binary_expr.operator {
// `if (foo != null) {}`
BinaryOperator::Equality | BinaryOperator::Inequality => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}

// `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_dangerous_fix(
no_null_diagnostic(null_literal.span),
|fixer| fix_null(fixer, null_literal),
);
}
}
_ => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
Expand All @@ -116,15 +117,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(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_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(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
Expand Down Expand Up @@ -183,7 +184,7 @@ impl Rule for NoNull {

let mut parents = iter_outer_expressions(ctx.nodes(), node.id());
let Some(parent_kind) = parents.next() else {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
return;
Expand All @@ -203,7 +204,7 @@ impl Rule for NoNull {
diagnose_variable_declarator(ctx, null_literal, decl, grandparent_kind);
}
(AstKind::ReturnStatement(_), _) => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_fix(no_null_diagnostic(null_literal.span), |fixer| {
let mut null_span = null_literal.span;
// Find the last parent that is a TSAsExpression (`null as any`) or TSNonNullExpression (`null!`)
for parent in ctx.nodes().ancestors(node.id()) {
Expand All @@ -223,12 +224,12 @@ impl Rule for NoNull {
});
}
(AstKind::SwitchCase(_), Some(AstKind::SwitchStatement(switch))) => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_fix(no_null_diagnostic(null_literal.span), |fixer| {
try_fix_case(fixer, null_literal, switch)
});
}
_ => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
ctx.diagnostic_with_dangerous_fix(no_null_diagnostic(null_literal.span), |fixer| {
fix_null(fixer, null_literal)
});
}
Expand Down
Loading