Skip to content

Commit

Permalink
fix(linter/no-null): incorrect fixer for NullLiteral within `Return…
Browse files Browse the repository at this point in the history
…Statement` (#5247)

close: #5194

It’s a long time no contributed to Linter, I'd like to try it
  • Loading branch information
Dunqing committed Aug 27, 2024
1 parent a6e9769 commit 9953fa5
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/oxc_linter/src/rules/unicorn/no_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,17 @@ impl Rule for NoNull {
}
(AstKind::ReturnStatement(_), _) => {
ctx.diagnostic_with_fix(no_null_diagnostic(null_literal.span), |fixer| {
fixer.delete(null_literal)
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().iter_parents(node.id()).skip(1) {
if matches!(
parent.kind(),
AstKind::TSAsExpression(_) | AstKind::TSNonNullExpression(_)
) {
null_span = parent.kind().span();
}
}
fixer.delete(&null_span)
});
}
(AstKind::SwitchCase(_), Some(AstKind::SwitchStatement(switch))) => {
Expand Down Expand Up @@ -369,6 +379,8 @@ fn test() {
"if (foo === undefined || foo === undefined) {}",
Some(check_strict_equality(true)),
),
("() => { return null! }", "() => { return }", None),
("() => { return null as any as typeof Array }", "() => { return }", None),
];
Tester::new(NoNull::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}

0 comments on commit 9953fa5

Please sign in to comment.