admin管理员组文章数量:1287497
I'd like to enforce curly braces for switch-cases in JS, possibly with ESLint or Prettier. Do you know of any related config, or if not that, then any other linting or formatting tool?
I tried the ESLint rule curly
set to "all"
, but it didn't pain about my curly-less switch-cases.
There is a switch-case ESLint plugin, but I haven't found such a rule in its doc, neither in its source code.
Example
Wrong:
switch (foo) {
case "bar":
return 1;
case "baz":
return 2;
default:
return 0;
}
Correct:
switch (foo) {
case "bar": {
return 1;
}
case "baz": {
return 2;
}
default: {
return 0;
}
}
I'd like to enforce curly braces for switch-cases in JS, possibly with ESLint or Prettier. Do you know of any related config, or if not that, then any other linting or formatting tool?
I tried the ESLint rule curly
set to "all"
, but it didn't pain about my curly-less switch-cases.
There is a switch-case ESLint plugin, but I haven't found such a rule in its doc, neither in its source code.
Example
Wrong:
switch (foo) {
case "bar":
return 1;
case "baz":
return 2;
default:
return 0;
}
Correct:
switch (foo) {
case "bar": {
return 1;
}
case "baz": {
return 2;
}
default: {
return 0;
}
}
Share
Improve this question
edited Aug 21, 2021 at 12:15
lejlun
4,4192 gold badges18 silver badges31 bronze badges
asked Aug 21, 2021 at 12:00
bzyrbzyr
4674 silver badges14 bronze badges
4 Answers
Reset to default 4The switch-braces
rule in the following eslint custom rules repo seems to be the one you are looking for:
https://github./justinanastos/eslint-plugin-justinanastos/blob/master/docs/rules/switch-braces.md
You can give it a try.
You can try 'no-restricted-syntax' rule with such selector.
With verbose output (each non-block child of a case is reported as error):
'no-restricted-syntax': ['error',
{
'selector': 'SwitchCase > *.consequent[type!="BlockStatement"]',
'message': 'Switch cases without blocks are disallowed.'
},
],
If :has
is supported by your parser, you can try this one, but it can have false-positive reports: since child (relative) selectors currently seem to be disallowed in :has
, descendant non-block .consequent
nodes may throw.
'no-restricted-syntax': ['error',
{
'selector': 'SwitchCase:has(*.consequent[type!="BlockStatement"])',
'message': 'Switch cases without blocks are disallowed.'
},
],
*This applies to eslint without plugins.
The curly
rule does not apply to blocks in switch cases. Unfortunately there is no good rule that does what you need. You can suggest the rule (or edit to curly rule) here https://github./eslint/eslint/issues.
If you're using unicorn (which I personally can remend), there is a rule for that:
ESLint Plugin Unicorn - Rule: "Switch case braces": https://github./sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/switch-case-braces.md
It's also in the "remended" preset since v44: https://github./sindresorhus/eslint-plugin-unicorn/releases/tag/v44.0.0
So, it will automatically be applied, if you have in your .eslintrc.json something like:
"plugins": ["@typescript-eslint", ... "unicorn"],
"extends": [ ... "plugin:unicorn/remended"],
本文标签: javascriptEnforce curly braces for JS switchcases with ESLint or PrettierStack Overflow
版权声明:本文标题:javascript - Enforce curly braces for JS switch-cases with ESLint or Prettier - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741294889a2370759.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论