switch-colon-spacing
在 switch 语句的冒号周围强制使用空格
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
冒号周围的间距提高了 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": true
(Default) requires one or more spaces after colons. -
"after": false
冒号后不允许有空格。¥
"after": false
disallows spaces after colons. -
"before": true
冒号前需要一个或多个空格。¥
"before": true
requires one or more spaces before colons. -
"before": false
(默认)不允许在冒号之前。¥
"before": false
(Default) disallows before colons.
此规则的错误代码示例:
¥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 中引入。