semi-spacing
在分号前后强制执行一致的间距
此规则报告的一些问题可通过 --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
.
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:
-
分号后的间距(如果它是行中的第一个标记)。
¥The spacing after the semicolon if it is the first token in the line.
-
如果分号在左括号(
(
或{
)之后,则为分号之前的空格,如果在右括号()
或}
)之前,则为分号之后的空格。该间距由space-in-parens
或block-spacing
检查。¥The spacing before the semicolon if it is after an opening parenthesis (
(
or{
), or the spacing after the semicolon if it is before a closing parenthesis ()
or}
). That spacing is checked byspace-in-parens
orblock-spacing
. -
具有空条件 (
for(;;)
) 的 for 循环中分号周围的间距。¥The spacing around the semicolon in a for loop with an empty condition (
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 中引入。