Skip to content
← Back to rules

eslint/no-lone-blocks Style

作用

禁止不必要的独立代码块语句。

为什么这很糟糕?

独立代码块可能会让人困惑,因为在不必要地使用时,它们并没有任何实际用途。 它们可能会引入额外的嵌套,降低代码可读性,并可能误导读者对作用域或意图的理解。

示例

以下是此规则的错误代码示例:

js
{
  var x = 1;
}

以下是此规则的正确代码示例:

js
if (condition) {
  var x = 1;
}

{
  let x = 1; // 用于创建一个有效的块级作用域。
}

如何使用

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

json
{
  "rules": {
    "no-lone-blocks": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "no-lone-blocks": "error",
  },
});
bash
oxlint --deny no-lone-blocks

版本

此规则已在 v0.15.6 中添加。

参考资料