mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(linter/unicorn): breaking fixer in case statements for no-null (#5176)
This commit is contained in:
parent
7b86ed61d1
commit
fd1031abeb
1 changed files with 31 additions and 8 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
use oxc_ast::{
|
use oxc_ast::{
|
||||||
ast::{
|
ast::{
|
||||||
Argument, BinaryExpression, CallExpression, Expression, NullLiteral, VariableDeclarator,
|
Argument, BinaryExpression, CallExpression, Expression, NullLiteral, SwitchStatement,
|
||||||
|
VariableDeclarator,
|
||||||
},
|
},
|
||||||
AstKind,
|
AstKind,
|
||||||
};
|
};
|
||||||
|
|
@ -201,6 +202,11 @@ impl Rule for NoNull {
|
||||||
fixer.delete(null_literal)
|
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| {
|
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
|
||||||
fix_null(fixer, null_literal)
|
fix_null(fixer, null_literal)
|
||||||
|
|
@ -214,6 +220,23 @@ fn fix_null<'a>(fixer: RuleFixer<'_, 'a>, null: &NullLiteral) -> RuleFix<'a> {
|
||||||
fixer.replace(null.span, "undefined")
|
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]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
use crate::tester::Tester;
|
use crate::tester::Tester;
|
||||||
|
|
@ -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) {}", None),
|
("if (foo != null) {}", "if (foo != undefined) {}", None),
|
||||||
("if (foo == null) {}", "if (foo == undefined) {}", Some(check_strict_equality(true))),
|
("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;
|
let isNullish;
|
||||||
|
|
@ -335,7 +352,7 @@ fn test() {
|
||||||
"
|
"
|
||||||
let isNullish;
|
let isNullish;
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case undefined:
|
case null:
|
||||||
case undefined:
|
case undefined:
|
||||||
isNullish = true;
|
isNullish = true;
|
||||||
break;
|
break;
|
||||||
|
|
@ -346,6 +363,12 @@ fn test() {
|
||||||
",
|
",
|
||||||
None,
|
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();
|
Tester::new(NoNull::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue