comma-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
.
逗号周围的间距提高了项目列表的可读性。尽管大多数语言的风格指南都规定在逗号之后而不是之前添加空格,但这取决于项目的偏好。
¥Spacing around commas improves readability of a list of items. Although most of the style guidelines for languages prescribe adding a space after a comma and not before it, it is subjective to the preferences of a project.
var foo = 1, bar = 2;
var foo = 1 ,bar = 2;
规则详情
¥Rule Details
该规则在变量声明、数组字面量、对象字面量、函数参数和序列中强制逗号前后的间距保持一致。
¥This rule enforces consistent spacing before and after commas in variable declarations, array literals, object literals, function parameters, and sequences.
本规则不适用于下列任何一种情况:
¥This rule does not apply in either of the following cases:
-
两个逗号之间
¥between two commas
-
在左括号
[
和逗号之间,以避免与array-bracket-spacing
规则冲突¥between opening bracket
[
and comma, to avoid conflicts with thearray-bracket-spacing
rule -
在逗号和右括号
]
之间,避免与array-bracket-spacing
规则冲突¥between comma and closing bracket
]
, to avoid conflicts with thearray-bracket-spacing
rule -
在逗号和右大括号
}
之间,避免与object-curly-spacing
规则冲突¥between comma and closing brace
}
, to avoid conflicts with theobject-curly-spacing
rule -
在逗号和右括号
)
之间,避免与space-in-parens
规则冲突¥between comma and closing parentheses
)
, to avoid conflicts with thespace-in-parens
rule
选项
¥Options
此规则有一个对象选项:
¥This rule has an object option:
-
"before": false
(默认)不允许逗号前有空格¥
"before": false
(default) disallows spaces before commas -
"before": true
逗号前需要一个或多个空格¥
"before": true
requires one or more spaces before commas -
"after": true
(默认)逗号后需要一个或多个空格¥
"after": true
(default) requires one or more spaces after commas -
"after": false
不允许逗号后有空格¥
"after": false
disallows spaces after commas
after
使用默认 { "before": false, "after": true }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the default { "before": false, "after": true }
options:
/*eslint comma-spacing: ["error", { "before": false, "after": true }]*/
var foo = 1 ,bar = 2;
var arr = [1 , 2];
var obj = {"foo": "bar" ,"baz": "qur"};
foo(a ,b);
new Foo(a ,b);
function baz(a ,b){}
a ,b
使用默认 { "before": false, "after": true }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the default { "before": false, "after": true }
options:
/*eslint comma-spacing: ["error", { "before": false, "after": true }]*/
var foo = 1, bar = 2
, baz = 3;
var arr = [1, 2];
var arr = [1,, 3]
var obj = {"foo": "bar", "baz": "qur"};
foo(a, b);
new Foo(a, b);
function qur(a, b){}
a, b
使用默认 { "before": false, "after": true }
选项的此规则的正确代码的其他示例:
¥Additional examples of correct code for this rule with the default { "before": false, "after": true }
options:
/*eslint comma-spacing: ["error", { "before": false, "after": true }]*/
// this rule does not enforce spacing between two commas
var arr = [
,,
, ,
];
// this rule does not enforce spacing after `[` and before `]`
var arr = [,];
var arr = [ , ];
var arr = [a, b,];
[,] = arr;
[ , ] = arr;
[a, b,] = arr;
// this rule does not enforce spacing before `}`
var obj = {x, y,};
var {z, q,} = obj;
import {foo, bar,} from "mod";
// this rule does not enforce spacing before `)`
foo(a, b,)
before
使用 { "before": true, "after": false }
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the { "before": true, "after": false }
options:
/*eslint comma-spacing: ["error", { "before": true, "after": false }]*/
var foo = 1, bar = 2;
var arr = [1 , 2];
var obj = {"foo": "bar", "baz": "qur"};
new Foo(a,b);
function baz(a,b){}
a, b
使用 { "before": true, "after": false }
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the { "before": true, "after": false }
options:
/*eslint comma-spacing: ["error", { "before": true, "after": false }]*/
var foo = 1 ,bar = 2 ,
baz = true;
var arr = [1 ,2];
var arr = [1 ,,3]
var obj = {"foo": "bar" ,"baz": "qur"};
foo(a ,b);
new Foo(a ,b);
function qur(a ,b){}
a ,b
何时不使用
¥When Not To Use It
如果你的项目不会遵循一致的逗号间距模式,请关闭此规则。
¥If your project will not be following a consistent comma-spacing pattern, turn this rule off.
相关规则
版本
此规则是在 ESLint v0.9.0 中引入。