Skip to content

Commit

Permalink
fix(linter/unicorn): breaking fixer in case statements for no-null (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Aug 24, 2024
1 parent 7b86ed6 commit fd1031a
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions crates/oxc_linter/src/rules/unicorn/no_null.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use oxc_ast::{
ast::{
Argument, BinaryExpression, CallExpression, Expression, NullLiteral, VariableDeclarator,
Argument, BinaryExpression, CallExpression, Expression, NullLiteral, SwitchStatement,
VariableDeclarator,
},
AstKind,
};
Expand Down Expand Up @@ -201,6 +202,11 @@ impl Rule for NoNull {
fixer.delete(null_literal)
});
}
(AstKind::SwitchCase(_), Some(AstKind::SwitchStatement(switch))) => {
ctx.diagnostic_with_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| {
fix_null(fixer, null_literal)
Expand All @@ -214,6 +220,23 @@ fn fix_null<'a>(fixer: RuleFixer<'_, 'a>, null: &NullLiteral) -> RuleFix<'a> {
fixer.replace(null.span, "undefined")
}

fn try_fix_case<'a>(
fixer: RuleFixer<'_, 'a>,
null: &NullLiteral,
switch: &SwitchStatement<'a>,
) -> RuleFix<'a> {
let also_has_undefined = switch
.cases
.iter()
.filter_map(|case| case.test.as_ref())
.any(|test| test.get_inner_expression().is_undefined());
if also_has_undefined {
fixer.noop()
} else {
fixer.replace(null.span, "undefined")
}
}

#[test]
fn test() {
use crate::tester::Tester;
Expand Down Expand Up @@ -313,12 +336,6 @@ fn test() {
("if (foo == null) {}", "if (foo == undefined) {}", None),
("if (foo != null) {}", "if (foo != undefined) {}", None),
("if (foo == null) {}", "if (foo == undefined) {}", Some(check_strict_equality(true))),
// FIXME
(
"if (foo === null || foo === undefined) {}",
"if (foo === undefined || foo === undefined) {}",
Some(check_strict_equality(true)),
),
(
"
let isNullish;
Expand All @@ -335,7 +352,7 @@ fn test() {
"
let isNullish;
switch (foo) {
case undefined:
case null:
case undefined:
isNullish = true;
break;
Expand All @@ -346,6 +363,12 @@ fn test() {
",
None,
),
// FIXME
(
"if (foo === null || foo === undefined) {}",
"if (foo === undefined || foo === undefined) {}",
Some(check_strict_equality(true)),
),
];
Tester::new(NoNull::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}

0 comments on commit fd1031a

Please sign in to comment.