semi-spacing
在分号前后强制执行一致的间距
此规则报告的一些问题可通过 --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.
JavaScript 允许你在分号之前或之后放置不必要的空格。
🌐 JavaScript allows you to place unnecessary spaces before or after a semicolon.
禁止或强制使用分号周围的空格可以提高程序的可读性。
🌐 Disallowing or enforcing space around a semicolon can improve the readability of your program.
var a = "b" ;
var c = "d";var e = "f";
规则详情
🌐 Rule Details
此规则旨在强制分号周围的空格。此规则防止在表达式中的分号前使用空格。
🌐 This rule aims to enforce spacing around a semicolon. This rule prevents the use of spaces before a semicolon in expressions.
在以下情况下,此规则不检查间距:
🌐 This rule doesn’t check spacing in the following cases:
- 分号后的间距(如果它是行中的第一个标记)。
- 如果分号在开括号之后(
(或{),则检查其前的间距;如果分号在闭括号之前()或}),则检查其后的间距。该间距由space-in-parens或block-spacing检查。 - 在空条件的 for 循环中分号周围的间距(
for(;;))。
选项
🌐 Options
该规则接受一个选项,一个对象,该对象有两个键 before 和 after,它们的布尔值为 true 或 false。
如果 before 是 true,则在分号前强制添加空格;如果是 false,则不允许在分号前添加空格。
如果 after 是 true,则在分号后强制添加空格;如果是 false,则不允许在分号后添加空格。
只有当分号不在行尾时,after 选项才会被应用。
🌐 The rule takes one option, an object, which has two keys before and after having boolean values true or false.
If before is true, space is enforced before semicolons and if it’s false, space is disallowed before semicolons.
If after is true, space is enforced after semicolons and if it’s false, space is disallowed after semicolons.
The after option will be only applied if a semicolon is not at the end of line.
默认值是 {"before": false, "after": true}。
🌐 The default is {"before": false, "after": true}.
"semi-spacing": ["error", {"before": false, "after": true}]
{"before": false, "after": true}
这是默认选项。它在分号后要求空格,并且不允许在分号前加空格。
🌐 This is the default option. It enforces spacing after semicolons and disallows spacing before semicolons.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint semi-spacing: "error"*/
var foo ;
var foo;var bar;
throw new Error("error") ;
while (a) { break ; }
for (i = 0 ; i < 10 ; i++) {}
for (i = 0;i < 10;i++) {}
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint semi-spacing: "error"*/
var foo;
var foo; var bar;
throw new Error("error");
while (a) { break; }
for (i = 0; i < 10; i++) {}
for (;;) {}
if (true) {;}
;foo();
{"before": true, "after": false}
此选项强制分号前有空格,而分号后不允许有空格。
🌐 This option enforces spacing before semicolons and disallows spacing after semicolons.
使用 {"before": true, "after": false} 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the {"before": true, "after": false} option:
/*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
var foo;
var foo ; var bar;
throw new Error("error");
while (a) { break; }
for (i = 0;i < 10;i++) {}
for (i = 0; i < 10; i++) {}
使用 {"before": true, "after": false} 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the {"before": true, "after": false} option:
/*eslint semi-spacing: ["error", { "before": true, "after": false }]*/
var foo ;
var foo ;var bar ;
throw new Error("error") ;
while (a) {break ;}
for (i = 0 ;i < 10 ;i++) {}
何时不使用
🌐 When Not To Use It
如果你不关心分号前后间距的一致性,你可以关闭此规则。
🌐 You can turn this rule off if you are not concerned with the consistency of spacing before or after semicolons.
相关规则
版本
此规则是在 ESLint v0.16.0 中引入。