useCollapsedIf
¥Summary
-
规则生效日期:
v1.9.4¥Rule available since:
v1.9.4 -
诊断类别:
lint/style/useCollapsedIf¥Diagnostic Category:
lint/style/useCollapsedIf -
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
unicorn/no-lonely-if相同¥Same as
unicorn/no-lonely-if -
与
collapsible_if相同¥Same as
collapsible_if
-
¥How to configure
{ "linter": { "rules": { "style": { "useCollapsedIf": "error" } } }}¥Description
强制使用单个 if 子句而不是嵌套的 if 子句。
¥Enforce using single if instead of nested if clauses.
如果 if (a) 代码块中只有一条 if (b) 语句,通常使用 if (a && b) 形式会更清晰。
¥If an if (b) statement is the only statement in an if (a) block, it is often clearer to use an if (a && b) form.
¥Examples
¥Invalid
if (condition) { if (anotherCondition) { // ... }}code-block.js:2:5 lint/style/useCollapsedIf FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This if statement can be collapsed into another if statement.
1 │ if (condition) {
> 2 │ if (anotherCondition) {
│ ^^^^^^^^^^^^^^^^^^^^^^^
> 3 │ // …
> 4 │ }
│ ^
5 │ }
6 │
ℹ Safe fix: Use collapsed if instead.
1 │ - if·(condition)·{
2 │ - ····if·(anotherCondition)·{
1 │ + if·(condition·&&·anotherCondition)·{
3 2 │ // …
4 │ - ····}
5 │ - }
3 │ + ····}
6 4 │
if (condition) { // Comment if (anotherCondition) { // ... }}code-block.js:3:5 lint/style/useCollapsedIf ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This if statement can be collapsed into another if statement.
1 │ if (condition) {
2 │ // Comment
> 3 │ if (anotherCondition) {
│ ^^^^^^^^^^^^^^^^^^^^^^^
> 4 │ // …
> 5 │ }
│ ^
6 │ }
7 │
¥Valid
if (condition && anotherCondition) { // ...}if (condition) { if (anotherCondition) { // ... } doSomething();}if (condition) { if (anotherCondition) { // ... } else { // ... }}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号