comma-dangle

要求或不允许尾随逗号

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

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

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

尾随逗号简化了向对象和数组添加和删除项目,因为只有您正在修改的行必须被触及。支持尾随逗号的另一个论点是,当从对象或数组中添加或删除项目时,它提高了差异的清晰度:

不太晰:

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

y+6UrFn0OjSkxebtp/pStA==

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

规则详情

e0YCKL2W8xl2Xy461n+ca1lZH2U3dUJKk6erd6v6twIkKqZFnuchWIP0HxjSM9WmEytGSkvpyZhJ3v/mHhoivw/c58hnnDOWpsE5gdAAQYA=

选项

FCjx99K/4aI1xO9VjYxFd4tsm9OTQKveZuvQ7xgbcj3Fo+fnaBV2zCWJF0V4C54wi2UjruYmOgqAdi9e4PDFiQ==

{
    "comma-dangle": ["error", "never"],
    // or
    "comma-dangle": ["error", {
        "arrays": "never",
        "objects": "never",
        "imports": "never",
        "exports": "never",
        "functions": "never"
    }]
}
    LxgcRFo0yveqXgUhjEnDfXH22hnZwfrSiQg7fQuJu3bhcqrviBXyTLeByAc9Ta2Jr8dHmPgdFz7jJ7AyU+JoLDgsccad4vI5KGPMUgbmOKdxEb8o0rwtY2nczo8iA5evwkY9tq9YbEiCNULGXVlBQDTwexo2TSXirFq74IMKH1YZPN8tbeKug5NyLjodUzPny5kxPTuw6Y3foTmSKpBxHoIyR2Ikm74HyHYP46KrbzKJvaPBONKb+J5bi9I25E0RAhX8gOwJ76O+JWG6Vs7KYOobADk0jCLW9buE0pHI8MCezY4UNxMvC3VT2p+PlooLX5cWWcQ8nYz9cKYwJy/H58K31KJuGfLx/KuMjxH4kpmBKWCEXABUI+WTUOHR+OV4Jay7q9fGRJl6vU2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xOsRU64XCSSxTv73Pf6QDm21tBtYPm8GOKOA6iFt5JWLn3Sd656ljy29gVSJVrcSWc2mc1O+b9Jng5JEoq6OU6+2GRqBB+UdDtUQo80KtQ93QvMUZ4UEvw+gvRkwTrdMhYa2DOShA9Qz6SBFmxDxRlvIRLGAW3PgHPfyTc5USVvOAX6w4ZAcdJHBQgqudbQM5LzUurvj0LQ00qVTfGGrJrKqrL14VEskXiqogsLAfAmxcPyEoZ9NiJcKTIqxuoe6ZDcGkaIS9HsJ4RQlfYnbXr4onhilmsTKYoZWX9CyhNZ1pxawQru2Jnb+qLI0BBAhroVs5Vl8+6ROvzntE8Q1LU1LjqVIfvTX0+HRwkCyg/lddjLFLiRQIbrNn2vBbt98iB9vSkce3GweM2KMr36rXjA4gEpO0lvaDsuKMtDVOZrw9GqkeOdRkQ+7upRtGE01OhEba4PgHVgvJlzPUt9boFBhtEjiRnEGBYyjbhux1SdJV5tEr8V1L8G8HoGlq0qEUNIoVo0J7F0vsgecLWbDhpb93c1SB7Iazl6lDIPQkKQyeGZxMmi2yVXWX6dS3+AeXVne6cxPa5L5SGMNOMg2WZae2vfL4oKJdBjy5sDaR7/tNK4gCw/WlYCGl9fWQEgeyKoYMYWrdeVAjWuk1gc3eN1v3IdymPzi+O/TNkmj7oMHD5107JORMBq6PuSb23Vro9aPyjMx+MgMOoyLsusxuokGMK3AlOfbfsErScOHIICQa/hv7xrgy0Mk1Vf3wM8xWFMPCKaSbWuTIOiho1h0L7q9jrkzqTBYLj/A==

never

xpQlHQir/OjetBCVQdvE2ZgQPCW760NC0Hx4ME/LU7WZqQw2tVqnpVglryC7D7glUPCH/1g4iXuHfFxvajF53psS7eDCaB/Zh8dFo91+Xdk=

/*eslint comma-dangle: ["error", "never"]*/

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

var arr = [1,2,];

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

xpQlHQir/OjetBCVQdvE2ZgQPCW760NC0Hx4ME/LU7WZqQw2tVqnpVglryC7D7glx7DItMr0uYkuj7F7hdtcVB3CEwxrcS6b/moKkxJT4PA=

/*eslint comma-dangle: ["error", "never"]*/

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

var arr = [1,2];

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

always

gXTXeFewMhKUGG8XamsAXQRgAGcsgsoyrXG8mzkouAQaGdwi6ld4KZTiafGUsGotAwCW9RvZFAXUQLISRluMJn7UuCMdh50RF3PUvteThdM=

/*eslint comma-dangle: ["error", "always"]*/

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

var arr = [1,2];

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

gXTXeFewMhKUGG8XamsAXQRgAGcsgsoyrXG8mzkouAROUgZElaJGFgdBR/y6ubPXIyQGBTDDgSIVrm4o38YcXZoDesgTXIYm3ERjILZIW5k=

/*eslint comma-dangle: ["error", "always"]*/

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

var arr = [1,2,];

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

always-multiline

gXTXeFewMhKUGG8XamsAXQ+W3zEDM3IIMSuJXqC7qPwb4D9G6b8vKO0z5aBma2OSVAa1NS9HzPBE0vSjhS0Z+rh6K47TnXRTc4o22iPg4Sc=

/*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"
});

gXTXeFewMhKUGG8XamsAXQ+W3zEDM3IIMSuJXqC7qPwb4D9G6b8vKO0z5aBma2OS5MqJfTSSr9tqP8Zft+ky5kgVM52E5g3e5Zk1gZqpXwI=

/*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

gXTXeFewMhKUGG8XamsAXR/UBJcEc9Y+IrF4AXqjzkOgfP4DRIcuxOLUfAfmDQfAeHizBB9cAWyCQFSThEB2uEtOnLbFkTiwJe2T5l6Dp2M=

/*eslint comma-dangle: ["error", "only-multiline"]*/

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

var arr = [1,2,];

var arr = [1,
    2,];

gXTXeFewMhKUGG8XamsAXR/UBJcEc9Y+IrF4AXqjzkOgfP4DRIcuxOLUfAfmDQfA/ArK1Xk1YLi3ncPamBy948sgicBLooPMvZbnqVAk+Js=

/*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

gXTXeFewMhKUGG8XamsAXayDflfUN5zbzAml332VyrnCocQXCIbi608CiIOM+EaIkwcmJR0zElM8Av+Pzepq2ciYqAEtEwjNIZfosUX8SiCl96ZikUKvQru1y8Ed3o/S

/*eslint comma-dangle: ["error", {"functions": "never"}]*/

function foo(a, b,) {
}

foo(a, b,);
new foo(a, b,);

gXTXeFewMhKUGG8XamsAXayDflfUN5zbzAml332VyrnCocQXCIbi608CiIOM+EaItkEeWK4WgVcR5ZqpTDutzuH2/xVn4mGEExbevvjL58+mKYk41pKMLpGeL0+2A3l/

/*eslint comma-dangle: ["error", {"functions": "never"}]*/

function foo(a, b) {
}

foo(a, b);
new foo(a, b);

gXTXeFewMhKUGG8XamsAXayDflfUN5zbzAml332VyrlPpaknCuz+D5V1dYSCJZzQFwI5gNM148/7vsZaz5mJiBce8zR+jautJa0LDT4fV7rLTdtzvb3Pb3Zi4GrBriV4

/*eslint comma-dangle: ["error", {"functions": "always"}]*/

function foo(a, b) {
}

foo(a, b);
new foo(a, b);

gXTXeFewMhKUGG8XamsAXayDflfUN5zbzAml332VyrlPpaknCuz+D5V1dYSCJZzQURTFQ6YmUaMAALM4LjBP8RM0QWWPHP7zvk8YbEGaBvfqGGXPpsK1/2t8HU3l/PDQ

/*eslint comma-dangle: ["error", {"functions": "always"}]*/

function foo(a, b,) {
}

foo(a, b,);
new foo(a, b,);

何时不使用

mMx/FQV1L6oUUbC3LYsGWvE3UsCAMhteYmTXHZ7e2F4zBl9I9g1vlH/RWqtZhgdkCQ8PiwgvjGAOdEWCLuI8iA==