switch-colon-spacing
在 switch 语句的冒号周围强制使用空格
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
冒号两边的空格可以提高 case/default 条款的可读性。
🌐 Spacing around colons improves readability of case/default clauses.
规则详情
🌐 Rule Details
此规则控制 switch 语句中 case 和 default 子句的冒号周围的间距。
仅当连续的标记位于同一行时,此规则才进行检查。
🌐 This rule controls spacing around colons of case and default clauses in switch statements.
This rule does the check only if the consecutive tokens exist on the same line.
此规则有 2 个布尔值选项。
🌐 This rule has 2 options that are boolean value.
{
"switch-colon-spacing": ["error", {"after": true, "before": false}]
}
"after": true(默认)在冒号后需要一个或多个空格。"after": false不允许冒号后有空格。"before": true在冒号前需要一个或多个空格。"before": false(默认)不允许出现在冒号前。
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint switch-colon-spacing: "error"*/
switch (a) {
case 0 :break;
default :foo();
}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint switch-colon-spacing: "error"*/
switch (a) {
case 0: foo(); break;
case 1:
bar();
break;
default:
baz();
break;
}
使用 {"after": false, "before": true} 选项违反此规则的 错误 代码示例:
🌐 Examples of incorrect code for this rule with {"after": false, "before": true} option:
/*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/
switch (a) {
case 0: break;
default: foo();
}
使用 {"after": false, "before": true} 选项的此规则的 正确 代码示例:
🌐 Examples of correct code for this rule with {"after": false, "before": true} option:
/*eslint switch-colon-spacing: ["error", {"after": false, "before": true}]*/
switch (a) {
case 0 :foo(); break;
case 1 :
bar();
break;
default :
baz();
break;
}
何时不使用
🌐 When Not To Use It
如果你不想在 switch 语句的冒号周围通知空格,那么禁用此规则是安全的。
🌐 If you don’t want to notify spacing around colons of switch statements, then it’s safe to disable this rule.
版本
此规则是在 ESLint v4.0.0-beta.0 中引入。