Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check elseClause inside noUselessLoneBlockStatements #584

Merged
merged 11 commits into from
Oct 23, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make;
use biome_js_syntax::{
AnyJsStatement, JsBlockStatement, JsFileSource, JsLabeledStatement, JsStatementList,
JsVariableStatement,
AnyJsStatement, JsBlockStatement, JsElseClause, JsFileSource, JsLabeledStatement,
JsStatementList, JsVariableStatement,
};
use biome_rowan::{AstNode, AstNodeList, BatchMutationExt};

Expand Down Expand Up @@ -145,6 +145,11 @@ fn statement_has_block_level_declaration(statement: &AnyJsStatement, is_module:
}

fn in_control_structure(block: &JsBlockStatement) -> bool {
let syntax_kind = block.syntax().parent().unwrap().kind();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually we don't use unwrap in rule impl. You can use if let or:

Suggested change
let syntax_kind = block.syntax().parent().unwrap().kind();
let Some(parent) = block.syntax().parent() else { return false; };

let is_else = JsElseClause::can_cast(syntax_kind);
if is_else {
return true;
}
matches!(
block.parent(),
Some(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ function g() {

{
function foo() {}
}

if(x) {
x += 1;
} else {
x += 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ function g() {
{
function foo() {}
}

if(x) {
x += 1;
} else {
x += 2;
}
```