noUselessLoneBlockStatements
¥Summary
-
规则生效日期:
v1.3.3¥Rule available since:
v1.3.3 -
诊断类别:
lint/complexity/noUselessLoneBlockStatements¥Diagnostic Category:
lint/complexity/noUselessLoneBlockStatements -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-lone-blocks相同¥Same as
no-lone-blocks
-
¥How to configure
{ "linter": { "rules": { "complexity": { "noUselessLoneBlockStatements": "error" } } }}¥Description
禁止不必要的嵌套块语句。
¥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.
if (foo) { bar(); { baz(); }}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
while (foo) { bar();}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号