Versions

comma-spacing

Enforce consistent spacing before and after commas

🔧 Fixable

Some problems reported by this rule are automatically fixable by the --fix command line option

逗号周围的间距可以提高项目列表的可读性。尽管大多数语言的风格指南都规定在逗号后而不是在逗号前添加空格,但这是由项目的偏好所决定的。

var foo = 1, bar = 2;
var foo = 1 ,bar = 2;

规则细节

这条规则使变量声明、数组字面、对象字面、函数参数和序列中逗号前后的间距一致。

此规则不适用于以下情况:

选项

此规则选项为对象:

  • "before": false(默认值)不允许在逗号前有空格
  • "before": true 要求在逗号前有一个或多个空格
  • "after": true(默认值)要求在逗号后有一个或多个空格
  • "after": false不允许在逗号后有空格

after

使用此规则与默认的 { "before": false, "after": true } 选项的错误示例:

Open in Playground
/*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 foo(a ,b){}
a ,b

使用此规则与默认的 { "before": false, "after": true } 选项的正确示例:

Open in Playground
/*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 foo(a, b){}
a, b

Additional 使用此规则与默认的 { "before": false, "after": true } 选项的正确示例:

Open in Playground
/*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 } 选项的错误示例:

Open in Playground
/*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 foo(a,b){}
a, b

使用此规则与 { "before": true, "after": false } 选项的正确示例:

Open in Playground
/*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 foo(a ,b){}
a ,b

何时不用

如果你的项目将不遵循一致的逗号间距模式,请关闭此规则。

Version

This rule was introduced in ESLint v0.9.0.

Further Reading

Resources

更改语言