comma-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.
逗号周围的空格可以提高列表项目的可读性。虽然大多数语言的风格指南规定在逗号后加空格而在逗号前不加空格,但这取决于项目的偏好。
🌐 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:
- 两个逗号之间
- 在开括号
[和逗号之间,以避免与array-bracket-spacing规则冲突 - 在逗号和闭括号
]之间,以避免与array-bracket-spacing规则冲突 - 在逗号和闭括号
}之间,以避免与object-curly-spacing规则冲突 - 在逗号和闭括号
)之间,以避免与space-in-parens规则冲突
选项
🌐 Options
此规则有一个对象选项:
🌐 This rule has an object option:
"before": false(默认)不允许逗号前有空格"before": true在逗号前需要一个或多个空格"after": true(默认)要求逗号后有一个或多个空格"after": false不允许在逗号后有空格
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 中引入。