使用 ESLint 或 Prettier 对 JS switch-case 强制使用大括号

bzy*_*zyr 7 javascript code-formatting eslint prettier

我想在 JS 中为 switch-case 强制使用大括号,可能使用 ESLint 或 Prettier。您是否知道任何相关的配置,或者如果不知道,那么还有其他的 linting 或格式化工具吗?

我尝试将 ESLint 规则curly设置为"all",但它并没有抱怨我的无卷曲开关盒。

有一个switch-case ESLint 插件,但我在它的文档中没有找到这样的规则,在它的源代码中也没有。

例子

错误的:

switch (foo) {
  case "bar":
    return 1;
  case "baz":
    return 2;
  default:
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正确的:

switch (foo) {
  case "bar": {
    return 1;
  }
  case "baz": {
    return 2;
  }
  default: {
    return 0;
  }
}
Run Code Online (Sandbox Code Playgroud)

Adi*_*rma 4

switch-braces以下 eslint 自定义规则存储库中的规则似乎是您正在寻找的规则: https ://github.com/justinanastos/eslint-plugin-justinanastos/blob/master/docs/rules/switch-braces.md

你可以尝试一下。