Skip to content
← Back to rules

eslint/no-duplicate-case 正确性

This rule is turned on by default.

作用

禁止重复的 case 标签。

为什么不好?

如果 switch 语句在 case 子句中有重复的测试表达式, 很可能是程序员复制了 case 子句但忘记更改测试表达式。

示例

此规则 错误 代码示例:

js
var a = 1,
  one = 1;
switch (a) {
  case 1:
    break;
  case 2:
    break;
  case 1: // 重复的测试表达式
    break;
  default:
    break;
}

switch (a) {
  case one:
    break;
  case 2:
    break;
  case one: // 重复的测试表达式
    break;
  default:
    break;
}

此规则 正确 代码示例:

js
var a = 1,
  one = 1;
switch (a) {
  case 1:
    break;
  case 2:
    break;
  default:
    break;
}

switch (a) {
  case "1":
    break;
  case "2":
    break;
  default:
    break;
}

如何使用

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

json
{
  "rules": {
    "no-duplicate-case": "error"
  }
}
ts
import { defineConfig } from "oxlint";

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

版本

此规则在 v0.0.3 中新增。

参考资料