mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(linter): improve eslint/no-lone-blocks (#8588)
The code `let mut lone_blocks = Vec::new();` is unnecessary because it will contain at most one element throughout its usage. Additionally, special test cases has been added for when `parent_node.kind()` is `AstKind::FunctionBody(_)`.
This commit is contained in:
parent
01ac773d0c
commit
40f51654ec
2 changed files with 43 additions and 48 deletions
|
|
@ -61,58 +61,55 @@ impl Rule for NoLoneBlocks {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let body = &stmt.body;
|
if stmt.body.is_empty() {
|
||||||
if body.is_empty() {
|
|
||||||
if !matches!(parent_node.kind(), AstKind::TryStatement(_) | AstKind::CatchClause(_)) {
|
if !matches!(parent_node.kind(), AstKind::TryStatement(_) | AstKind::CatchClause(_)) {
|
||||||
report(ctx, node, parent_node);
|
report(ctx, node, parent_node);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if body.len() == 1
|
let mut is_lone_blocks = is_lone_block(node, parent_node);
|
||||||
&& matches!(parent_node.kind(), AstKind::FunctionBody(parent) if parent.statements.len() == 1)
|
|
||||||
{
|
|
||||||
report(ctx, node, parent_node);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut lone_blocks = Vec::new();
|
if is_lone_blocks {
|
||||||
if is_lone_block(node, parent_node) {
|
for child in &stmt.body {
|
||||||
lone_blocks.push(node);
|
match child.as_declaration() {
|
||||||
}
|
Some(Declaration::VariableDeclaration(decl))
|
||||||
|
if decl.kind != VariableDeclarationKind::Var =>
|
||||||
for child in &stmt.body {
|
{
|
||||||
match child.as_declaration() {
|
is_lone_blocks = false;
|
||||||
Some(Declaration::VariableDeclaration(decl))
|
}
|
||||||
if decl.kind != VariableDeclarationKind::Var =>
|
Some(
|
||||||
{
|
Declaration::ClassDeclaration(_) | Declaration::FunctionDeclaration(_),
|
||||||
mark_lone_block(node, &mut lone_blocks);
|
) => {
|
||||||
|
is_lone_blocks = false;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
Some(Declaration::ClassDeclaration(_) | Declaration::FunctionDeclaration(_)) => {
|
|
||||||
mark_lone_block(node, &mut lone_blocks);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(last) = lone_blocks.last() {
|
if is_lone_blocks {
|
||||||
if last.id() == node.id() {
|
|
||||||
lone_blocks.pop();
|
|
||||||
report(ctx, node, parent_node);
|
report(ctx, node, parent_node);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
match parent_node.kind() {
|
|
||||||
AstKind::BlockStatement(parent_statement) => {
|
match parent_node.kind() {
|
||||||
if parent_statement.body.len() == 1 {
|
AstKind::FunctionBody(parent) => {
|
||||||
report(ctx, node, parent_node);
|
if parent.statements.len() == 1 && stmt.body.len() == 1 {
|
||||||
}
|
report(ctx, node, parent_node);
|
||||||
}
|
}
|
||||||
AstKind::StaticBlock(parent_statement) => {
|
|
||||||
if parent_statement.body.len() == 1 {
|
|
||||||
report(ctx, node, parent_node);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
AstKind::BlockStatement(parent_statement) => {
|
||||||
|
if parent_statement.body.len() == 1 {
|
||||||
|
report(ctx, node, parent_node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AstKind::StaticBlock(parent_statement) => {
|
||||||
|
if parent_statement.body.len() == 1 {
|
||||||
|
report(ctx, node, parent_node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -142,16 +139,6 @@ fn is_lone_block(node: &AstNode, parent_node: &AstNode) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mark_lone_block(parent_node: &AstNode, lone_blocks: &mut Vec<&AstNode>) {
|
|
||||||
if lone_blocks.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if lone_blocks.last().is_some_and(|last| last.id() == parent_node.id()) {
|
|
||||||
lone_blocks.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test() {
|
fn test() {
|
||||||
use crate::tester::Tester;
|
use crate::tester::Tester;
|
||||||
|
|
@ -160,6 +147,7 @@ fn test() {
|
||||||
"if (foo) { if (bar) { baz(); } }",
|
"if (foo) { if (bar) { baz(); } }",
|
||||||
"do { bar(); } while (foo)",
|
"do { bar(); } while (foo)",
|
||||||
"function foo() { while (bar) { baz() } }",
|
"function foo() { while (bar) { baz() } }",
|
||||||
|
"function test() { { console.log(6); console.log(6) } }",
|
||||||
"{ let x = 1; }", // { "ecmaVersion": 6 },
|
"{ let x = 1; }", // { "ecmaVersion": 6 },
|
||||||
"{ const x = 1; }", // { "ecmaVersion": 6 },
|
"{ const x = 1; }", // { "ecmaVersion": 6 },
|
||||||
"'use strict'; { function bar() {} }", // { "ecmaVersion": 6 },
|
"'use strict'; { function bar() {} }", // { "ecmaVersion": 6 },
|
||||||
|
|
@ -216,6 +204,7 @@ fn test() {
|
||||||
"{}",
|
"{}",
|
||||||
"{var x = 1;}",
|
"{var x = 1;}",
|
||||||
"foo(); {} bar();",
|
"foo(); {} bar();",
|
||||||
|
"function test() { { console.log(6); } }",
|
||||||
"if (foo) { bar(); {} baz(); }",
|
"if (foo) { bar(); {} baz(); }",
|
||||||
"{
|
"{
|
||||||
{ } }",
|
{ } }",
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,12 @@ source: crates/oxc_linter/src/tester.rs
|
||||||
· ──
|
· ──
|
||||||
╰────
|
╰────
|
||||||
|
|
||||||
|
⚠ eslint(no-lone-blocks): Block is unnecessary.
|
||||||
|
╭─[no_lone_blocks.tsx:1:19]
|
||||||
|
1 │ function test() { { console.log(6); } }
|
||||||
|
· ───────────────────
|
||||||
|
╰────
|
||||||
|
|
||||||
⚠ eslint(no-lone-blocks): Nested block is redundant.
|
⚠ eslint(no-lone-blocks): Nested block is redundant.
|
||||||
╭─[no_lone_blocks.tsx:1:19]
|
╭─[no_lone_blocks.tsx:1:19]
|
||||||
1 │ if (foo) { bar(); {} baz(); }
|
1 │ if (foo) { bar(); {} baz(); }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue