no-comma-dangle

不允许在对象和数组字面量中使用尾随逗号。

¥Disallows trailing commas in object and array literals.

根据 ECMAScript 5(和 ECMAScript 3!)规范,对象字面中的尾随逗号是有效的,但是 IE8(当不在 IE8 文档模式下)及以下版本在遇到 JavaScript 中的尾随逗号时会抛出错误。

¥Trailing commas in object literals are valid according to the ECMAScript 5 (and ECMAScript 3!) spec, however IE8 (when not in IE8 document mode) and below will throw an error when it encounters trailing commas in JavaScript.

var foo = {
    bar: "baz",
    qux: "quux",
};

规则详情

¥Rule Details

此规则旨在检测对象字面中的尾随逗号。因此,它会在遇到对象字面中的尾随逗号时触发警告。

¥This rule is aimed at detecting trailing commas in object literals. As such, it will warn whenever it encounters a trailing comma in an object literal.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

var foo = {
    bar: "baz",
    qux: "quux",
};

var arr = [1,2,];

foo({
  bar: "baz",
  qux: "quux",
});

此规则的正确代码示例:

¥Examples of correct code for this rule:

var foo = {
    bar: "baz",
    qux: "quux"
};

var arr = [1,2];

foo({
  bar: "baz",
  qux: "quux"
});

何时不使用

¥When Not To Use It

如果你的代码不会在 IE8 或更低版本(例如 Node.js 应用)中运行,并且你希望允许尾随逗号,请关闭此规则。

¥If your code will not be run in IE8 or below (a Node.js application, for example) and you’d prefer to allow trailing commas, turn this rule off.

版本

此规则是在 ESLint v0.0.9 中引入,并在 v1.0.0-rc-1 中删除。