Skip to content

Commit fd1031a

Browse files
committedAug 24, 2024·
fix(linter/unicorn): breaking fixer in case statements for no-null (#5176)
1 parent 7b86ed6 commit fd1031a

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed
 

‎crates/oxc_linter/src/rules/unicorn/no_null.rs

+31-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use oxc_ast::{
22
ast::{
3-
Argument, BinaryExpression, CallExpression, Expression, NullLiteral, VariableDeclarator,
3+
Argument, BinaryExpression, CallExpression, Expression, NullLiteral, SwitchStatement,
4+
VariableDeclarator,
45
},
56
AstKind,
67
};
@@ -201,6 +202,11 @@ impl Rule for NoNull {
201202
fixer.delete(null_literal)
202203
});
203204
}
205+
(AstKind::SwitchCase(_), Some(AstKind::SwitchStatement(switch))) => {
206+
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
207+
try_fix_case(fixer, null_literal, switch)
208+
});
209+
}
204210
_ => {
205211
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
206212
fix_null(fixer, null_literal)
@@ -214,6 +220,23 @@ fn fix_null<'a>(fixer: RuleFixer<'_, 'a>, null: &NullLiteral) -> RuleFix<'a> {
214220
fixer.replace(null.span, "undefined")
215221
}
216222

223+
fn try_fix_case<'a>(
224+
fixer: RuleFixer<'_, 'a>,
225+
null: &NullLiteral,
226+
switch: &SwitchStatement<'a>,
227+
) -> RuleFix<'a> {
228+
let also_has_undefined = switch
229+
.cases
230+
.iter()
231+
.filter_map(|case| case.test.as_ref())
232+
.any(|test| test.get_inner_expression().is_undefined());
233+
if also_has_undefined {
234+
fixer.noop()
235+
} else {
236+
fixer.replace(null.span, "undefined")
237+
}
238+
}
239+
217240
#[test]
218241
fn test() {
219242
use crate::tester::Tester;
@@ -313,12 +336,6 @@ fn test() {
313336
("if (foo == null) {}", "if (foo == undefined) {}", None),
314337
("if (foo != null) {}", "if (foo != undefined) {}", None),
315338
("if (foo == null) {}", "if (foo == undefined) {}", Some(check_strict_equality(true))),
316-
// FIXME
317-
(
318-
"if (foo === null || foo === undefined) {}",
319-
"if (foo === undefined || foo === undefined) {}",
320-
Some(check_strict_equality(true)),
321-
),
322339
(
323340
"
324341
let isNullish;
@@ -335,7 +352,7 @@ fn test() {
335352
"
336353
let isNullish;
337354
switch (foo) {
338-
case undefined:
355+
case null:
339356
case undefined:
340357
isNullish = true;
341358
break;
@@ -346,6 +363,12 @@ fn test() {
346363
",
347364
None,
348365
),
366+
// FIXME
367+
(
368+
"if (foo === null || foo === undefined) {}",
369+
"if (foo === undefined || foo === undefined) {}",
370+
Some(check_strict_equality(true)),
371+
),
349372
];
350373
Tester::new(NoNull::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
351374
}

0 commit comments

Comments
 (0)
Please sign in to comment.