fix(linter/eslint/no-lonely-if): check final else blocks in else-if chains (#27759)

Oxlint skipped the final `else { if (b) {} }` in `if (a) {} else if (d) {} else { if (b) {} }`, which ESLint reports. Follow the chain through alternate branches before applying the existing guard for an enclosing `if` body, preserving dangling-`else` protection.

Addresses report 14 (`eslint/no-lonely-if`) in #27746.
This commit is contained in:
camc314 committed 2026-10-16 11:02:19 +00:00
1 parent fc039b55ae
commit 2947917784
2 files changed
+43 -3

No files matched your search

@@ -3,7 +3,7 @@ use oxc_ast::AstKind;
use oxc_ast::ast::{IfStatement, Statement};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_span::{GetSpan, Span};
fn no_lonely_if_diagnostic(lonely_if: &IfStatement) -> OxcDiagnostic {
let span = Span::sized(lonely_if.span.start, 2);
@@ -101,8 +101,15 @@ impl Rule for NoLonelyIf {
return;
};
if let AstKind::IfStatement(_) = ctx.nodes().parent_kind(node.id()) {
return;
let mut current_if = if_stmt;
for ancestor in ctx.nodes().ancestor_kinds(node.id()) {
let AstKind::IfStatement(parent_if) = ancestor else {
break;
};
if parent_if.consequent.span() == current_if.span {
return;
}
current_if = parent_if;
}
match only_stmt {
@@ -133,6 +140,7 @@ fn test() {
if (c) { }
if (d) { }
}",
"if (a) if (c) {} else if (d) {} else { if (b) {} } else {}",
];
let fail = vec![
@@ -178,6 +186,10 @@ fn test() {
if (b) bar();
}
`template literal`;",
"if (a) {} else { if (b) {} }",
"if (a) {} else if (d) {} else { if (b) {} }",
"if (a) {} else if (c) {} else if (d) {} else { if (b) {} }",
"if (a) {} else if (d) {} else { if (b) {} else {} }",
];
/* Pending
@@ -83,3 +83,31 @@ source: crates/oxc_linter/src/tester.rs
5 │ }
╰────
help: Consider using `else if` instead.
⚠ eslint(no-lonely-if): Unexpected `if` as the only statement in an `else` block
╭─[no_lonely_if.tsx:1:18]
1 │ if (a) {} else { if (b) {} }
· ──
╰────
help: Consider using `else if` instead.
⚠ eslint(no-lonely-if): Unexpected `if` as the only statement in an `else` block
╭─[no_lonely_if.tsx:1:33]
1 │ if (a) {} else if (d) {} else { if (b) {} }
· ──
╰────
help: Consider using `else if` instead.
⚠ eslint(no-lonely-if): Unexpected `if` as the only statement in an `else` block
╭─[no_lonely_if.tsx:1:48]
1 │ if (a) {} else if (c) {} else if (d) {} else { if (b) {} }
· ──
╰────
help: Consider using `else if` instead.
⚠ eslint(no-lonely-if): Unexpected `if` as the only statement in an `else` block
╭─[no_lonely_if.tsx:1:33]
1 │ if (a) {} else if (d) {} else { if (b) {} else {} }
· ──
╰────
help: Consider using `else if` instead.