comma-dangle
要求或禁止尾随逗号
此规则报告的一些问题可通过 --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.
根据 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",
};
尾随逗号简化了向对象和数组添加或删除项的操作,因为只需要修改你正在更改的行。另一个支持使用尾随逗号的理由是,当从对象或数组中添加或删除某个项时,它可以提高差异(diff)的清晰度:
🌐 Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched. Another argument in favor of trailing commas is that it improves the clarity of diffs when an item is added or removed from an object or array:
不太晰:
🌐 Less clear:
var foo = {
- bar: "baz",
- qux: "quux"
+ bar: "baz"
};
更清晰:
🌐 More clear:
var foo = {
bar: "baz",
- qux: "quux",
};
规则详情
🌐 Rule Details
此规则强制在对象和数组字面中一致使用尾随逗号。
🌐 This rule enforces consistent use of trailing commas in object and array literals.
选项
🌐 Options
此规则有一个字符串选项或一个对象选项:
🌐 This rule has a string option or an object option:
{
"comma-dangle": ["error", "never"],
// or
"comma-dangle": ["error", {
"arrays": "never",
"objects": "never",
"imports": "never",
"exports": "never",
"functions": "never"
}]
}
"never"(默认)不允许尾随逗号"always"需要尾随逗号"always-multiline"要求当最后一个元素或属性与闭合的]或}不在同一行时使用尾随逗号,并且当最后一个元素或属性与闭合的]或}在同一行时禁止使用尾随逗号"only-multiline"允许(但不要求)在最后一个元素或属性与关闭的]或}不在同一行时使用尾随逗号,并且当最后一个元素或属性与关闭的]或}在同一行时,不允许使用尾随逗号
你也可以使用对象选项为每种语法类型配置此规则。以下每个选项都可以设置为 "never"、"always"、"always-multiline"、"only-multiline" 或 "ignore"。除非另有说明,否则每个选项的默认值为 "never"。
🌐 You can also use an object option to configure this rule for each type of syntax.
Each of the following options can be set to "never", "always", "always-multiline", "only-multiline", or "ignore".
The default for each option is "never" unless otherwise specified.
arrays用于数组字面量和解构的数组模式。(例如let [a,] = [1,];)objects用于对象字面量和解构的对象模式。(例如let {a,} = {a: 1};)imports用于 ES 模块的导入声明。(例如import {a,} from "foo";)exports用于 ES 模块的导出声明。(例如export {a,};)functions用于函数声明和函数调用。(例如(function(a,){ })(b,);)functions仅在对 ECMAScript 2017 或更高版本进行 lint 时启用。
never
使用默认 "never" 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default "never" option:
/*eslint comma-dangle: ["error", "never"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var arr = [1,2,];
foo({
bar: "baz",
qux: "quux",
});
使用默认 "never" 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default "never" option:
/*eslint comma-dangle: ["error", "never"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var arr = [1,2];
foo({
bar: "baz",
qux: "quux"
});
always
使用 "always" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "always" option:
/*eslint comma-dangle: ["error", "always"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var arr = [1,2];
foo({
bar: "baz",
qux: "quux"
});
使用 "always" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always" option:
/*eslint comma-dangle: ["error", "always"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var arr = [1,2,];
foo({
bar: "baz",
qux: "quux",
},);
always-multiline
使用 "always-multiline" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "always-multiline" option:
/*eslint comma-dangle: ["error", "always-multiline"]*/
var foo = {
bar: "baz",
qux: "quux"
};
var foo = { bar: "baz", qux: "quux", };
var arr = [1,2,];
var arr = [1,
2,];
var arr = [
1,
2
];
foo({
bar: "baz",
qux: "quux"
});
使用 "always-multiline" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "always-multiline" option:
/*eslint comma-dangle: ["error", "always-multiline"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var foo = {bar: "baz", qux: "quux"};
var arr = [1,2];
var arr = [1,
2];
var arr = [
1,
2,
];
foo({
bar: "baz",
qux: "quux",
});
only-multiline
使用 "only-multiline" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "only-multiline" option:
/*eslint comma-dangle: ["error", "only-multiline"]*/
var foo = { bar: "baz", qux: "quux", };
var arr = [1,2,];
var arr = [1,
2,];
使用 "only-multiline" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "only-multiline" option:
/*eslint comma-dangle: ["error", "only-multiline"]*/
var foo = {
bar: "baz",
qux: "quux",
};
var foo = {
bar: "baz",
qux: "quux"
};
var foo = {bar: "baz", qux: "quux"};
var arr = [1,2];
var arr = [1,
2];
var arr = [
1,
2,
];
var arr = [
1,
2
];
foo({
bar: "baz",
qux: "quux",
});
foo({
bar: "baz",
qux: "quux"
});
functions
使用 {"functions": "never"} 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the {"functions": "never"} option:
/*eslint comma-dangle: ["error", {"functions": "never"}]*/
function foo(a, b,) {
}
foo(a, b,);
new foo(a, b,);
使用 {"functions": "never"} 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the {"functions": "never"} option:
/*eslint comma-dangle: ["error", {"functions": "never"}]*/
function foo(a, b) {
}
foo(a, b);
new foo(a, b);
使用 {"functions": "always"} 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the {"functions": "always"} option:
/*eslint comma-dangle: ["error", {"functions": "always"}]*/
function foo(a, b) {
}
foo(a, b);
new foo(a, b);
使用 {"functions": "always"} 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the {"functions": "always"} option:
/*eslint comma-dangle: ["error", {"functions": "always"}]*/
function foo(a, b,) {
}
foo(a, b,);
new foo(a, b,);
何时不使用
🌐 When Not To Use It
如果你不关心悬空逗号,你可以关闭此规则。
🌐 You can turn this rule off if you are not concerned with dangling commas.
版本
此规则是在 ESLint v0.16.0 中引入。