Skip to content
← Back to rules

unicorn/no-lonely-if Pedantic

🚧 An auto-fix is planned for this rule, but not implemented at this time.

它的作用

禁止在没有 elseif 块中,将 if 语句作为唯一语句。

为什么这不好?

在一个 if 块中,只有一个没有 else 子句的 if 语句会让人感到困惑。

示例

此规则的错误代码示例:

javascript
if (foo) {
  if (bar) {
  }
}
if (foo) if (bar) baz();

此规则的正确代码示例:

javascript
if (foo && bar) {
}
if (foo && bar) baz();

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "rules": {
    "unicorn/no-lonely-if": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-lonely-if": "error",
  },
});
bash
oxlint --deny unicorn/no-lonely-if

版本

此规则于 v0.0.18 中加入。

参考资料