-
-
Notifications
You must be signed in to change notification settings - Fork 483
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
feat(lint/noEmptyBlockStatements): add rule #521
Changes from all commits
48cf196
deee494
8b0a170
a5523b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic}; | ||
use biome_console::markup; | ||
use biome_js_syntax::{ | ||
JsBlockStatement, JsFunctionBody, JsStaticInitializationBlockClassMember, JsSwitchStatement, | ||
}; | ||
use biome_rowan::{declare_node_union, AstNode, AstNodeList}; | ||
|
||
declare_rule! { | ||
/// Disallow empty block statements and static blocks. | ||
/// | ||
/// Empty static blocks and block statements, while not technically errors, usually occur due to refactoring that wasn’t completed. They can cause confusion when reading code. | ||
/// | ||
/// This rule disallows empty block statements and static blocks. | ||
/// This rule ignores block statements or static blocks which contain a comment (for example, in an empty catch or finally block of a try statement to indicate that execution should continue regardless of errors). | ||
/// | ||
/// Source: https://eslint.org/docs/latest/rules/no-empty-static-block/ | ||
/// Source: https://eslint.org/docs/latest/rules/no-empty/ | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// function emptyFunctionBody () {} | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// try { | ||
/// doSomething(); | ||
/// } catch(ex) { | ||
/// | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// class Foo { | ||
/// static {} | ||
/// } | ||
/// ``` | ||
/// | ||
/// ## Valid | ||
/// | ||
/// ```js | ||
/// function foo () { | ||
/// doSomething(); | ||
/// } | ||
/// ``` | ||
/// | ||
/// ```js | ||
/// try { | ||
/// doSomething(); | ||
/// } catch (ex) { | ||
/// // continue regardless of error | ||
/// } | ||
/// ``` | ||
/// | ||
pub(crate) NoEmptyBlockStatements { | ||
version: "next", | ||
name: "noEmptyBlockStatements", | ||
recommended: false, | ||
} | ||
} | ||
|
||
declare_node_union! { | ||
pub(crate) Query = JsBlockStatement | JsFunctionBody | JsStaticInitializationBlockClassMember | JsSwitchStatement | ||
} | ||
|
||
impl Rule for NoEmptyBlockStatements { | ||
type Query = Ast<Query>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let query = ctx.query(); | ||
let is_empty = is_empty(query); | ||
let has_comments = query.syntax().has_comments_descendants(); | ||
|
||
(is_empty && !has_comments).then_some(()) | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _: &Self::State) -> Option<RuleDiagnostic> { | ||
let query = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
query.range(), | ||
markup! { | ||
"Unexpected empty block." | ||
}, | ||
) | ||
.note(markup! { | ||
"Empty blocks are usually the result of an incomplete refactoring. Remove the empty block or add a comment inside it if it is intentional." | ||
}), | ||
) | ||
} | ||
} | ||
|
||
fn is_empty(query: &Query) -> bool { | ||
use Query::*; | ||
match query { | ||
JsFunctionBody(body) => body.directives().len() == 0 && body.statements().len() == 0, | ||
JsBlockStatement(block) => block.statements().len() == 0, | ||
JsStaticInitializationBlockClassMember(block) => block.statements().len() == 0, | ||
JsSwitchStatement(statement) => statement.cases().len() == 0, | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
function foo() {} | ||
|
||
const bar = () => {}; | ||
|
||
function fooWithNestedEmptyFnBlock() { | ||
let a = 1; | ||
|
||
function shouldFail(){} | ||
|
||
return a | ||
} | ||
|
||
|
||
const barWithNestedEmptyFnBlock = () => { | ||
let a = 1; | ||
|
||
const shouldFail = () => {} | ||
|
||
return a | ||
} | ||
|
||
let someVar; | ||
if (someVar) { | ||
} | ||
|
||
while (someVar) { | ||
} | ||
|
||
switch(someVar) { | ||
} | ||
|
||
try { | ||
doSomething(); | ||
} catch(ex) { | ||
|
||
} finally { | ||
|
||
} | ||
|
||
class Foo { | ||
static {} | ||
} | ||
|
||
for(let i; i>0; i++){} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Last thing before merging: do you think it makes sense adding an exception for all loop statements? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you think of a concrete and/or common example where a developer wants to execute a loop without doing anything in the iterations? My initial thought is that would be pretty uncommon, and they could just inline suppress the rule or comment in the block if they really wanted it there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just my two cents. I'm not opposed to adding an exception if you think it's appropriate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's ship the rule as is. We see later if users complain about the default behavior. |
||
|
||
const ob = {} | ||
for (key in ob) {} | ||
|
||
const ar = [] | ||
for (val of ar) {} | ||
|
||
function fooWithInternalEmptyBlocks(){ | ||
let someVar; | ||
if (someVar) {} | ||
|
||
while (someVar) { | ||
} | ||
|
||
switch(someVar) { | ||
} | ||
|
||
try { | ||
doSomething(); | ||
} catch(ex) { | ||
|
||
} finally { | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here: we should keep only a few examples.