default-case

switch 语句中需要 default 个案例

一些代码约定要求所有 switch 语句都有 default 大小写,即使默认大小写为空,例如:

switch (foo) {
    case 1:
        doSomething();
        break;

    case 2:
        doSomething();
        break;

    default:
    // do nothing
}

想法是最好总是明确说明默认行为应该是什么,这样开发人员是否忘记了错误地包含默认行为就很清楚了。

其他代码约定允许您跳过 default 案例,只要有注释表明省略是故意的,例如:

switch (foo) {
    case 1:
        doSomething();
        break;

    case 2:
        doSomething();
        break;

    // no default
}

再一次,这里的目的是表明开发人员打算不存在默认行为。

规则详情

PfEmOSCyP7IEP4dY/nhkSdl+P7arUMD5zxZwYjMTcVxW6HR3ygIhQJIgoIujRkU6DdQATod5XGB/y9dBeL9LvGAwIn3Koxd8/EciXNEdtEDURr4CaOPzrWVDgGwxJaoPKhQ+LddUaAVlMnSLxAQR8pdGP0XrtHVTyAN1RJuowuQyJQtrW6Ccw0Fx2JPiZzT2pG6x9KOsRhHZdbKChOPUgRRgscjJJb6cloVj3KDsfiGb7gGy6h6mhoaoqgyshD0r/JAfioLsS+CZ2GQYhzaURRZKqJXng8GAn+Ikmj7sKvx2n9vVT6bdPxWYRBz1iZkxrJ4RbHLKhsiXCsJWCMBXQXj2sd4uzMKkl2BDn1UEdtf0kindTofRTZdj1lpelMIGIe28vJdl6Hl32jOatQ1XjQ==

k7HkpyhTRoT9T3bsrVJ1W9IUT56r3GvYicL0qS8XjwbDvDXG9eGk/CkswpMjh9k+

/*eslint default-case: "error"*/

switch (a) {
    case 1:
        /* code */
        break;
}

f/g6SKVZdhoBiPVtnee0m+uPEv1T4kMgIaFUpxdtFwzAZHGBqJ5dJxRHxHHP/cP8

/*eslint default-case: "error"*/

switch (a) {
    case 1:
        /* code */
        break;

    default:
        /* code */
        break;
}

switch (a) {
    case 1:
        /* code */
        break;

    // no default
}

switch (a) {
    case 1:
        /* code */
        break;

    // No Default
}

选项

s8U6fkMHLlw9xwLE9wEu7PScyGhNfB626MrUJUAFZrP6KHUUr2kWhPM8RiL6id6e

    TFrRmf6QXFSBS1hEz+MPOZh7WBioDyi2KqmeCRgV2s3q7Wmzlt1kcouRRbP2pgA+kXCY0PWpyAeS1xT/hSnE6Dc1R7qMCBjM1OTBcXi/JNyRnJYwdpBRKZsjsaOic4enT2J9lGMNQbhjup7B4MZiX1mkbgRW26zxMF5JnttxBn5ty1j2/n3h0lf5TKDy7FIPhraYWQ+D+2NtozhTS2CiGQ==

commentPattern

Nlp1jtDUP/vBFvw75eqFZuFX2AoQJKzJFDUcj/JZg2dozBWLyuCyRFnnssuLx4ut6Lvm7E0bRBf10qD2iBQqqgu920K96FZEdC3KnqDtOXq5xfVqkXLm3yPXlzy/oh21

/*eslint default-case: ["error", { "commentPattern": "^skip\\sdefault" }]*/

switch(a) {
    case 1:
        /* code */
        break;

    // skip default
}

switch(a) {
    case 1:
        /* code */
        break;

    // skip default case
}

何时不使用

ZD0Npzubtkr/rxB7vO8F0USf/9plMQjDRmcpTlpce3Bq/7KiD40JGkcHhJsM13MPNTsjzH3g94uIuNLO9ISDrkS2hUlcDSxyqhX8vKSREV0kW20pEAqkf87lK7vFjUxry0V3CIFaiqop4rMd5Gd8SXGFR47X0QrDM+9+YO4zBvI=