noUselessLoneBlockStatements
このコンテンツはまだ日本語訳がありません。
Diagnostic Category: lint/complexity/noUselessLoneBlockStatements
Since: v1.3.3
Sources:
- Same as:
no-lone-blocks
Disallow unnecessary nested block statements.
In JavaScript, prior to ES6, standalone code blocks delimited by curly braces do not create a new scope and have no use. In ES6, code blocks may create a new scope if a block-level binding (let and const), a class declaration or a function declaration (in strict mode) are present. A block is not considered redundant in these cases.
Examples
Section titled ExamplesInvalid
Section titled Invalidcode-block.js:1:1 lint/complexity/noUselessLoneBlockStatements ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This block statement doesn’t serve any purpose and can be safely removed.
> 1 │ {}
│ ^^
2 │
ℹ Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
code-block.js:3:3 lint/complexity/noUselessLoneBlockStatements FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This block statement doesn’t serve any purpose and can be safely removed.
1 │ if (foo) {
2 │ bar();
> 3 │ {
│ ^
> 4 │ baz();
> 5 │ }
│ ^
6 │ }
7 │
ℹ Standalone block statements without any block-level declarations are redundant in JavaScript and can be removed to simplify the code.
ℹ Safe fix: Remove redundant block.
1 1 │ if (foo) {
2 2 │ bar();
3 │ - ··{
4 3 │ baz();
5 │ - ··}
6 4 │ }
7 5 │