noUselessLoneBlockStatements
诊断类别:lint/complexity/noUselessLoneBlockStatements
¥Diagnostic Category: lint/complexity/noUselessLoneBlockStatements
自从:v1.3.3
¥Since: v1.3.3
来源:
¥Sources:
-
与以下相同:
no-lone-blocks
¥Same as:
no-lone-blocks
禁止不必要的嵌套块语句。
¥Disallow unnecessary nested block statements.
在 ES6 之前的 JavaScript 中,用大括号分隔的独立代码块不会创建新的作用域,也没有用处。在 ES6 中,如果存在块级绑定(let 和 const)、类声明或函数声明(在严格模式下),则代码块可能会创建新的范围。在这些情况下,块不被视为多余的。
¥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
¥Invalid
code-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 │
¥Valid
¥Related links