no-duplicate-case

不允许重复的案例标签

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

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

规则详情

此规则不允许在 switch 语句的 case 子句中出现重复的测试表达式。

此规则的错误代码示例:

/*eslint no-duplicate-case: "error"*/

var a = 1,
    one = 1;

switch (a) {
    case 1:
        break;
    case 2:
        break;
    case 1:         // duplicate test expression
        break;
    default:
        break;
}

switch (a) {
    case one:
        break;
    case 2:
        break;
    case one:         // duplicate test expression
        break;
    default:
        break;
}

switch (a) {
    case "1":
        break;
    case "2":
        break;
    case "1":         // duplicate test expression
        break;
    default:
        break;
}

DzO9VpSFHJiTjGCgt1KSooNhAIn1o64th84QTOygwWSy7fBgj4TGxtI1vaW0J9Ta

/*eslint no-duplicate-case: "error"*/

var a = 1,
    one = 1;

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

switch (a) {
    case one:
        break;
    case 2:
        break;
    case 3:
        break;
    default:
        break;
}

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

何时不使用

sP9z8JkGYptH3Hkrh5UgPS9z8LxAD8DStt/kUn6T4EmfkI8IwYmZ+mwUdoAvbNl//M/+AkJ6jYoKHksRI1KNAq+SYQ//u41hvl9nMXCMWplQlHEqxMliuQrvTFJPenei2gsCYBKOJhM4RniUdGRAKcPj6B9R/UP5M3GmUI+GUP7zLBfYr5jCWMCUaHsrfrDIbitHpiHmghq1m08OE1z3J2Tbwe+X+zNOTJgAV3cPP+8e30Smzur9ElzTk9+udUp9

switch (a) {
    case i++:
        foo();
        break;
    case i++: // eslint-disable-line no-duplicate-case
        bar();
        break;
}