useBlockStatements
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/style/useBlockStatements¥Diagnostic Category:
lint/style/useBlockStatements -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "style": { "useBlockStatements": "error" } } }}¥Description
要求遵循大括号约定。
¥Requires following curly brace conventions.
当一个块只包含一个语句时,JavaScript 允许省略大括号。但是,许多人认为最佳做法是永远不要省略块周围的大括号,即使它们是可选的,因为它会导致错误并降低代码清晰度。
¥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
¥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 │
ℹ 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 │
ℹ 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 │
ℹ 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 │
ℹ 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 │
ℹ 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 │
ℹ 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 │
ℹ 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 │
ℹ Unsafe fix: Wrap the statement with a `JsBlockStatement`
1 │ - ···while·(x);
1 │ + ···while·(x)·{}
2 2 │
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号