Skip to content
← Back to rules

unicorn/switch-case-break-position Style

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

作用

强制在 case 子句中统一 break/return/continue/throw 的位置。

为什么这不好?

要求终止语句(breakreturncontinuethrow)出现在 case 子句的块语句内部,而不是放在其后。 这通常会在重构时发生——例如,移除了 if 包裹层,但把 break 留在了大括号外面。

示例

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

js
switch (foo) {
  case 1:
    {
      doStuff();
    }
    break;
}

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

js
switch (foo) {
  case 1: {
    doStuff();
    break;
  }
}

如何使用

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

json
{
  "rules": {
    "unicorn/switch-case-break-position": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/switch-case-break-position": "error",
  },
});
bash
oxlint --deny unicorn/switch-case-break-position

版本

此规则新增于 v1.59.0。

参考资料