9 This rule enforces spacing around the colon in object literal properties. It can verify each property individually, or it can ensure horizontal alignment of adjacent properties in an object literal.
13 This rule enforces consistent spacing between keys and values in object literal properties. In the case of long lines, it is acceptable to add a new line wherever whitespace is allowed.
17 This rule has an object option:
19 * `"beforeColon": false (default) | true`
20 * `false`: disallows spaces between the key and the colon in object literals.
21 * `true`: requires at least one space between the key and the colon in object literals.
22 * `"afterColon": true (default) | false`
23 * `true`: requires at least one space between the colon and the value in object literals.
24 * `false`: disallows spaces between the colon and the value in object literals.
25 * `"mode": "strict" (default) | "minimum"`
26 * `"strict"`: enforces exactly one space before or after colons in object literals.
27 * `"minimum"`: enforces one or more spaces before or after colons in object literals.
28 * `"align": "value" | "colon"`
29 * `"value"`: enforces horizontal alignment of values in object literals.
30 * `"colon"` enforces horizontal alignment of both colons and values in object literals.
31 * `"align"` with an object value allows for fine-grained spacing when values are being aligned in object literals.
32 * `"singleLine"` specifies a spacing style for single-line object literals.
33 * `"multiLine"` specifies a spacing style for multi-line object literals.
35 Please note that you can either use the top-level options or the grouped options (`singleLine` and `multiLine`) but not both.
39 Examples of **incorrect** code for this rule with the default `{ "beforeColon": false }` option:
44 /*eslint key-spacing: ["error", { "beforeColon": false }]*/
46 var obj = { "foo" : 42 };
51 Examples of **correct** code for this rule with the default `{ "beforeColon": false }` option:
56 /*eslint key-spacing: ["error", { "beforeColon": false }]*/
58 var obj = { "foo": 42 };
63 Examples of **incorrect** code for this rule with the `{ "beforeColon": true }` option:
68 /*eslint key-spacing: ["error", { "beforeColon": true }]*/
70 var obj = { "foo": 42 };
75 Examples of **correct** code for this rule with the `{ "beforeColon": true }` option:
80 /*eslint key-spacing: ["error", { "beforeColon": true }]*/
82 var obj = { "foo" : 42 };
89 Examples of **incorrect** code for this rule with the default `{ "afterColon": true }` option:
94 /*eslint key-spacing: ["error", { "afterColon": true }]*/
96 var obj = { "foo":42 };
101 Examples of **correct** code for this rule with the default `{ "afterColon": true }` option:
106 /*eslint key-spacing: ["error", { "afterColon": true }]*/
108 var obj = { "foo": 42 };
113 Examples of **incorrect** code for this rule with the `{ "afterColon": false }` option:
118 /*eslint key-spacing: ["error", { "afterColon": false }]*/
120 var obj = { "foo": 42 };
125 Examples of **correct** code for this rule with the `{ "afterColon": false }` option:
130 /*eslint key-spacing: ["error", { "afterColon": false }]*/
132 var obj = { "foo":42 };
139 Examples of **incorrect** code for this rule with the default `{ "mode": "strict" }` option:
144 /*eslint key-spacing: ["error", { "mode": "strict" }]*/
154 Examples of **correct** code for this rule with the default `{ "mode": "strict" }` option:
159 /*eslint key-spacing: ["error", { "mode": "strict" }]*/
169 Examples of **correct** code for this rule with the `{ "mode": "minimum" }` option:
174 /*eslint key-spacing: ["error", { "mode": "minimum" }]*/
186 Examples of **incorrect** code for this rule with the `{ "align": "value" }` option:
191 /*eslint key-spacing: ["error", { "align": "value" }]*/
202 Examples of **correct** code for this rule with the `{ "align": "value" }` option:
207 /*eslint key-spacing: ["error", { "align": "value" }]*/
217 ijkl: 'Non-consecutive lines form a new group'
220 var obj = { a: "foo", longPropertyName: "bar" };
225 Examples of **incorrect** code for this rule with the `{ "align": "colon" }` option:
230 /*eslint key-spacing: ["error", { "align": "colon" }]*/
240 Examples of **correct** code for this rule with the `{ "align": "colon" }` option:
245 /*eslint key-spacing: ["error", { "align": "colon" }]*/
257 The `align` option can take additional configuration through the `beforeColon`, `afterColon`, `mode`, and `on` options.
259 If `align` is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
264 "beforeColon": false,
271 Examples of **correct** code for this rule with sample `{ "align": { } }` options:
276 /*eslint key-spacing: ["error", {
295 /*eslint key-spacing: ["error", {
297 "beforeColon": false,
311 ### align and multiLine
313 The `multiLine` and `align` options can differ, which allows for fine-tuned control over the `key-spacing` of your files. `align` will **not** inherit from `multiLine` if `align` is configured as an object.
315 `multiLine` is used any time an object literal spans multiple lines. The `align` configuration is used when there is a group of properties in the same object. For example:
319 key1: 1, // uses multiLine
321 key2: 2, // uses align (when defined)
322 key3: 3, // uses align (when defined)
324 key4: 4 // uses multiLine
329 Examples of **incorrect** code for this rule with sample `{ "align": { }, "multiLine": { } }` options:
334 /*eslint key-spacing: ["error", {
336 "beforeColon": false,
347 "myObjectFunction": function() {
357 Examples of **correct** code for this rule with sample `{ "align": { }, "multiLine": { } }` options:
362 /*eslint key-spacing: ["error", {
364 "beforeColon": false,
376 "myObjectFunction": function() {
379 }, // These are two separate groups, so no alignment between `myObjectFunction` and `one`
381 "seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
387 ### singleLine and multiLine
389 Examples of **correct** code for this rule with sample `{ "singleLine": { }, "multiLine": { } }` options:
394 /*eslint "key-spacing": [2, {
396 "beforeColon": false,
405 var obj = { one: 1, "two": 2, three: 3 };
414 ## When Not To Use It
416 If you have another convention for property spacing that might not be consistent with the available options, or if you want to permit multiple styles concurrently you can safely disable this rule.