useBlockStatements (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v1.0.0 - Diagnostic Category:
lint/style/useBlockStatements - This rule isnβt recommended, so you need to enable it.
- This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
- Same as
curly
- Same as
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "style": { "useBlockStatements": "error" } } }}Description
Section titled βDescriptionβRequires following curly brace conventions.
JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to never omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity.
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβ if (x) x;code-block.js:1:2 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
> 1 β if (x) x;
β ^^^^^^^^^
2 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
1 β Β·ifΒ·(x)Β·{Β·x;Β·}
β ++ ++
if (x) { x; } else y;code-block.js:3:4 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
1 β if (x) {
2 β x;
> 3 β } else y;
β ^^^^^^^
4 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
3 β Β·}Β·elseΒ·{Β·y;Β·}
β ++ ++
if (x) { x;} else if (y) y;code-block.js:3:8 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
1 β if (x) {
2 β x;
> 3 β } else if (y) y;
β ^^^^^^^^^
4 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
3 β }Β·elseΒ·ifΒ·(y)Β·{Β·y;Β·}
β ++ ++
for (;;);code-block.js:1:4 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
> 1 β for (;;);
β ^^^^^^^^^
2 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
1 β - Β·Β·Β·forΒ·(;;);
1 β + Β·Β·Β·forΒ·(;;)Β·{}
2 2 β
for (p in obj);code-block.js:1:4 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
> 1 β for (p in obj);
β ^^^^^^^^^^^^^^^
2 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
1 β - Β·Β·Β·forΒ·(pΒ·inΒ·obj);
1 β + Β·Β·Β·forΒ·(pΒ·inΒ·obj)Β·{}
2 2 β
for (x of xs);code-block.js:1:3 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
> 1 β for (x of xs);
β ^^^^^^^^^^^^^^
2 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
1 β - Β·Β·forΒ·(xΒ·ofΒ·xs);
1 β + Β·Β·forΒ·(xΒ·ofΒ·xs)Β·{}
2 2 β
do; while (x);code-block.js:1:3 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
> 1 β do;
β ^^^
> 2 β while (x);
β ^^^^^^^^^^
3 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
1 β - Β·Β·do;
1 β + Β·Β·doΒ·{}
2 2 β while (x);
3 3 β
while (x);code-block.js:1:4 lint/style/useBlockStatements FIXABLE ββββββββββββββββββββββββββββββββββββββββββ
β Block statements are preferred in this position.
> 1 β while (x);
β ^^^^^^^^^^
2 β
βΉ Without braces, it is easy to add a second statement that looks like part of the block but is not. This can cause bugs.
βΉ Unsafe fix: Wrap the statement with a `JsBlockStatement`
1 β - Β·Β·Β·whileΒ·(x);
1 β + Β·Β·Β·whileΒ·(x)Β·{}
2 2 β
Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.