key-spacing

在对象字面属性中强制键和值之间的间距一致

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

此规则在对象字面属性中强制冒号周围的间距。它可以单独验证每个属性,也可以确保对象字面量中相邻属性的水平对齐。

规则详情

此规则强制对象字面属性中键和值之间的间距一致。对于长行,可以在允许空格的地方添加新行。

选项

此规则有一个对象选项:

    YgQVMJTnNoAIWS04UOi9B2+DlaJ0yy9ezdaqbBh9rIoqmD+zA3aDF4fYH9j77RJBpqcAhDgxjCMqr05K37EXWEi2gk9enVyw2S5rRTI2FlwIrcprCSJCPIXnKbBbMQss6fsoaLOi3u04y6u27dk0JDJ2ewergT0v3sFdA5zjzs9ka0ZPcdNkAfey3i/m/72FO5BwIBKGc6tk5Be4wPtjoVP+Cfl25L27x/5IWoocXEzHbtjxVuuxy9OPMy1LWYHEEic0IfJ0qdFmnb3BCOyRFVA/yKz6bxaWOnEeH4aHLXwFd4qPE+VQ644qVbkzpfJ3ODcHLUZEoisrA5TPsOqzapyDuuVUGBkfOiIke7kAITdktTuTfCqkbWnG12FTF0cnLkB1SIEhvWL/vwj17jLilqf9qS1DAZ3bInmWCrFJvAtpkynKefKqfKhVaRRaio8s84W5NV5o1maeGkP+X+6/A/Z8TmIVKWc53OaRSWKoc0LBn++T3y/7LD66JJ8fVCy2SGlQjiyWm7056DryEnzvy43gQKzybwZs24gBtGItHBi/NMWJ2eM/jbX2y0y/4/35K207Ooa+2otvitomqza0cKqSUf+DeWc36rmQZbeXtxacNTgt5wH/PIWtbsnl6BA0tqVkDBoisuEaXucNPyJBIrRAyEEpi2XjuWxSVn0qd9bR1XC9eGj/o3xf8l5nNGS3A8di1GZeQYfNGK9n8S/1797WszbG2skth7hbI3cAaRz2j1EjyUuaQvdlDKFv/H7g10Q3qYScDF0qDO/wKimMJAYuiky6JGfe2NmYjrpX55EOy1BHeNJ2Jj1Du09N7ZHMDDs8DHavoG1dDj3CEf33e70wtJboqKA/dHrWt0xXmD2/A/uoJx4ti8O70ndOVbI77Vyim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FiWK1cnswUJa+WdQSsZtTOsQWzwZoiI7gh8ix2O3K1UYox6QYiOqnoTqgPuuZO9ZWjR410Iu1GXXb

/*eslint key-spacing: ["error", { "beforeColon": false }]*/

var obj = { "foo" : 42 };

gG4UUCWpCvzqVaU47Ruen2w1HQxA6az03ExyosiKGtVPis7W6JQFiWK1cnswUJa+WdQSsZtTOsQWzwZoiI7gh7ZxesPew3IfQEzGDVkE2RI5FuTzW7+5v/dvFuxYbZEZ

/*eslint key-spacing: ["error", { "beforeColon": false }]*/

var obj = { "foo": 42 };

TlLpo7HIJjIy2v3f96HaCUQdLQP49XJ1seQqgo02IV88hBKU+20fKYloZnNwHBGcLF2aNU3/sOCnFWRI8CIS4fJOxVNd5gzH6qHVTEa84Q6vMMN3dXfkmMhiEVMAjLpB

/*eslint key-spacing: ["error", { "beforeColon": true }]*/

var obj = { "foo": 42 };

TlLpo7HIJjIy2v3f96HaCUQdLQP49XJ1seQqgo02IV88hBKU+20fKYloZnNwHBGcFetUXdUk5SrzbD9GJZGbCvaEiynpfT6AQQ77dNh+NXfW90C7fYdba7SI83juWu+c

/*eslint key-spacing: ["error", { "beforeColon": true }]*/

var obj = { "foo" : 42 };

afterColon

gG4UUCWpCvzqVaU47Ruen7lGnQUr5Llr9cntK8MxbMJOd+fE2RmpAN4i5hiEkycDXA75PkOeNUKqerZjgi6p2s4qRHoNmU+14d03csMKnHLuuCDgEcfuB8uajTuAGjs9

/*eslint key-spacing: ["error", { "afterColon": true }]*/

var obj = { "foo":42 };

gG4UUCWpCvzqVaU47Ruen7lGnQUr5Llr9cntK8MxbMJOd+fE2RmpAN4i5hiEkycDXA75PkOeNUKqerZjgi6p2o2KdsxZqTct+Z+shoLTZd8ZzjjjpCZIX01lcOMCJtOP

/*eslint key-spacing: ["error", { "afterColon": true }]*/

var obj = { "foo": 42 };

TlLpo7HIJjIy2v3f96HaCfUovFk0Jc6rpPq43/OhnkHDgVi6AwEZjPbUD+9BaNUGOPjML46IRLvAUNsD8PDQZ1QsT+INKN0fba/he0bUwxDSbkgvCJQYMsN3fjJyJAXw

/*eslint key-spacing: ["error", { "afterColon": false }]*/

var obj = { "foo": 42 };

TlLpo7HIJjIy2v3f96HaCfUovFk0Jc6rpPq43/OhnkHDgVi6AwEZjPbUD+9BaNUGGcJ8l8pY2vcYeKlf33GD90oGoN5JeAu0RUHhbatQLJ2FYzG0orE8UCWPI/x/tR6Q

/*eslint key-spacing: ["error", { "afterColon": false }]*/

var obj = { "foo":42 };

mode

gG4UUCWpCvzqVaU47Ruen5aZr2yFkv9EeRKAaougQGZVhCfYJ5VGTjkNMTr3CQukjN0y4CHKbtV04pa3aODAqdqSMr9CXDs7gVNCvdtKDXyuarMu/u4O1aOlJ+kll60O

/*eslint key-spacing: ["error", { "mode": "strict" }]*/

call({
    foobar: 42,
    bat:    2 * 2
});

gG4UUCWpCvzqVaU47Ruen5aZr2yFkv9EeRKAaougQGZVhCfYJ5VGTjkNMTr3CQukjN0y4CHKbtV04pa3aODAqX2Vr8lmhAg3TyJpGehh+uE3mcSg4zx67U2Q8tuRNshz

/*eslint key-spacing: ["error", { "mode": "strict" }]*/

call({
    foobar: 42,
    bat: 2 * 2
});

TlLpo7HIJjIy2v3f96HaCVQdNX8lcHG2sAUE5h4kIXEWAXGRYp/y5Wo6uwbiH1he2jgR7albjUXMwJDyL1gomhGXKA7zBmB4WZtgaZYMDDwRlUoWD4WLtUlQpeOg0SCd

/*eslint key-spacing: ["error", { "mode": "minimum" }]*/

call({
    foobar: 42,
    bat:    2 * 2
});

align

TlLpo7HIJjIy2v3f96HaCdet8/D+hvLrkQl55NsDTmAgbdxHrX0uHKsfjBFNbcKjLSe3ympsY6tkK2cjD4e65qw+tMioa7qR7QPzQo4EGtL+YOKdy1xw0PE4Gsf1yp26

/*eslint key-spacing: ["error", { "align": "value" }]*/

var obj = {
    a: value,
    bcde:  42,
    fg :   foo()
};

TlLpo7HIJjIy2v3f96HaCdet8/D+hvLrkQl55NsDTmAgbdxHrX0uHKsfjBFNbcKj679AZatkUXCQSbXjL2KIpmjDbGUXOT+G97EGo4pDEHv50ytH9xPckzzheAAJREFw

/*eslint key-spacing: ["error", { "align": "value" }]*/

var obj = {
    a:    value,
    bcde: 42,

    fg: foo(),
    h:  function() {
        return this.a;
    },
    ijkl: 'Non-consecutive lines form a new group'
};

var obj = { a: "foo", longPropertyName: "bar" };

TlLpo7HIJjIy2v3f96HaCdet8/D+hvLrkQl55NsDTmCOImX+wl3VzkBgJ7bAHMXPkC7vZzAhvEQVAVdqclsOo9MMSPwKh2jFHx+uag/5I98+wO2wwEdYVbTUAPNX0mRJ

/*eslint key-spacing: ["error", { "align": "colon" }]*/

call({
    foobar: 42,
    bat:    2 * 2
});

TlLpo7HIJjIy2v3f96HaCdet8/D+hvLrkQl55NsDTmCOImX+wl3VzkBgJ7bAHMXP6R2z7xueytlsFawGyzrle0CgFasgwt2Aduk0C6vH5afRZh18NTY88WCtTTML5f0g

/*eslint key-spacing: ["error", { "align": "colon" }]*/

call({
    foobar: 42,
    bat   : 2 * 2
});

align

30eQ81ahkAPg02de8KGsmYyK1Gi1CANOmvSvw026eWlJv6zF3H8bVG48U405dnqjOnz3DVOMFT0ts76+rxgNi0iOdYfuL69gJKrDdXExs9VcV5SBkVGcfnxlq6AF0Hzkn7VYKuhAeMhip+2q2BlCZ69mAUrS6QfwbZRNknCJhzPoL9pvk7o/MNoGooacX181WDupPFeLHp33iRdmvOTQ0A==

y51P1l8Urgr8HNJ9vnC3urxeFVXGtaeY1H36L0rtn3kR/Hxb4sVOSf+S5ivg2DcnpDGtj9FkeN4Hovt2sKT+Mpler2t3iZ/XFo49Q7EDEIJofkrUoEbcqTC8mzxkE63V8ytsUxiRmMX4ujJ9uKBML6NQxsllOhDYwjVS75mUeBo=

// Defaults
align: {
    "beforeColon": false,
    "afterColon": true,
    "on": "colon",
    "mode": "strict"
}

usUFST+XNaC0G4FBYCpW1goWOG2woefcgjy7r0FIKES2L14mX+dhCp1LeXe1SLq7XsE45fM8LI/ioWqXsjTdufuxDi2H/yOWBgKRjJyK9/s3qwtgxtwxI8DuNtQ+Ps3+

/*eslint key-spacing: ["error", {
    "align": {
        "beforeColon": true,
        "afterColon": true,
        "on": "colon"
    }
}]*/

var obj = {
    "one"   : 1,
    "seven" : 7
}
/*eslint key-spacing: ["error", {
    "align": {
        "beforeColon": false,
        "afterColon": false,
        "on": "value"
    }
}]*/

var obj = {
    "one":  1,
    "seven":7
}

align 和 multiLine

GZwy4W+6AydQmCJKR/U4t3sy+3QPm1+gtRcOH43+8bInbfn5hXJ3+yWUvpew1CQChgSrNTgucgCkyhOv4VDwB1e/u72HOwnZjBmslY16zHHKfxUsc2LMTtoJ9AqrRUU7Te3NyOrKzitQnNkzJZOOmIVy522N7yqbIHI6O3evqPoQuE0u0YQbxMlHZp4a6WuRZj8xg695vePLELFftXXQQC4Ydim4jD4CDCYB5ECTO/4nSGaxFHSGiLfyAaIfJYFiz3tAsrlm76aOTNjKFZYAXhOgjcHj324ayokqhAS2aZBVI9U/30g5I17lEI+g85YPr0U07/7AvKwSbX7EHSxb+Q==

GZwy4W+6AydQmCJKR/U4t9KGsEAo9rs+oFo/4VGOL08aPwfb5Gg0ho2wDXEplKB6wDSEIQyXzJjgOKkuuUA6omhpr6LrJ3J3VdsBdOpxIFEKILUnNW6RjfC+G1rlYojUG2GdcDSowTIJi/17IT3dzivebcZh0p/xjMeP+QzoYSvLEu2lziTyhGVsr8HmudBHNSNMRs12DnxD7bh+UdPsVw==

var myObj = {
  key1: 1, // uses multiLine

  key2: 2, // uses align (when defined)
  key3: 3, // uses align (when defined)

  key4: 4 // uses multiLine
}

usUFST+XNaC0G4FBYCpW1goWOG2woefcgjy7r0FIKERmG8rr3NwqMXZbWvoMvbBkyxZ/KhBhuE8kfWZhQm2fXSi8zHo8OlTQgdo8RkjfelDYZyo7mCUtK7zGVDyw6ozsvhnSGkG8oFdp9Ihd5ouGvw==

/*eslint key-spacing: ["error", {
    "multiLine": {
        "beforeColon": false,
        "afterColon":true
    },
    "align": {
        "beforeColon": true,
        "afterColon": true,
        "on": "colon"
    }
}]*/

var obj = {
    "myObjectFunction": function() {
        // Do something
    },
    "one"             : 1,
    "seven"           : 7
}

usUFST+XNaC0G4FBYCpW1goWOG2woefcgjy7r0FIKERmG8rr3NwqMXZbWvoMvbBkyxZ/KhBhuE8kfWZhQm2fXSi8zHo8OlTQgdo8RkjfelCt1TsoCS9rYfI4gcqqN1koBgr9ZiL1n+boa4hkTlX5Kg==

/*eslint key-spacing: ["error", {
    "multiLine": {
        "beforeColon": false,
        "afterColon": true

    },
    "align": {
        "beforeColon": true,
        "afterColon": true,
        "on": "colon"
    }
}]*/

var obj = {
    "myObjectFunction": function() {
        // Do something
        //
    }, // These are two separate groups, so no alignment between `myObjectFunction` and `one`
    "one"   : 1,
    "seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
}

singleLine 和 multiLine

usUFST+XNaC0G4FBYCpW1gHajJR0+gCHUDv83L7Lp/w/RnoHL7PmtOn1lG48GfzkO+o5QtTYmWNgBzs8dCtOfZKcUtJlGoHfKT2w1PsQ+5iT77xl6h7RAnlVMze5JsHyZDmT00Y8F0P/xTt0HSgU1g==

/*eslint "key-spacing": [2, {
    "singleLine": {
        "beforeColon": false,
        "afterColon": true
    },
    "multiLine": {
        "beforeColon": true,
        "afterColon": true,
        "align": "colon"
    }
}]*/
var obj = { one: 1, "two": 2, three: 3 };
var obj2 = {
    "two" : 2,
    three : 3
};

何时不使用

LN7XuyHS+GjqUmIR0UnjyHmdp+Mmc59uS/xFfhKmvsZTxa9qlk8f3KfRKt0M2Xs/S6/i5dKEDCLR0MtVZDkC28FZ8X7v3bfXaUjv469Rdp/8YVH516Hp3ij2lZxwn1wNdl29LYFGb+wcc9HfMCeU7d6uUHlq8aiu/vn45gHadtS9Coqz9ZSdXZaPCnECztc2hJVIBzS1F8UXl8OiljVT+A==