1 # enforce consistent spacing between keys and values in object literal properties (key-spacing)
3 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.
7 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.
11 This rule has an object option:
13 * `"beforeColon": false (default) | true`
14 * `false`: disallows spaces between the key and the colon in object literals.
15 * `true`: requires at least one space between the key and the colon in object literals.
16 * `"afterColon": true (default) | false`
17 * `true`: requires at least one space between the colon and the value in object literals.
18 * `false`: disallows spaces between the colon and the value in object literals.
19 * `"mode": "strict" (default) | "minimum"`
20 * `"strict"`: enforces exactly one space before or after colons in object literals.
21 * `"minimum"`: enforces one or more spaces before or after colons in object literals.
22 * `"align": "value" | "colon"`
23 * `"value"`: enforces horizontal alignment of values in object literals.
24 * `"colon"` enforces horizontal alignment of both colons and values in object literals.
25 * `"align"` with an object value allows for fine-grained spacing when values are being aligned in object literals.
26 * `"singleLine"` specifies a spacing style for single-line object literals.
27 * `"multiLine"` specifies a spacing style for multi-line object literals.
29 Please note that you can either use the top-level options or the grouped options (`singleLine` and `multiLine`) but not both.
33 Examples of **incorrect** code for this rule with the default `{ "beforeColon": false }` option:
36 /*eslint key-spacing: ["error", { "beforeColon": false }]*/
38 var obj = { "foo" : 42 };
41 Examples of **correct** code for this rule with the default `{ "beforeColon": false }` option:
44 /*eslint key-spacing: ["error", { "beforeColon": false }]*/
46 var obj = { "foo": 42 };
49 Examples of **incorrect** code for this rule with the `{ "beforeColon": true }` option:
52 /*eslint key-spacing: ["error", { "beforeColon": true }]*/
54 var obj = { "foo": 42 };
57 Examples of **correct** code for this rule with the `{ "beforeColon": true }` option:
60 /*eslint key-spacing: ["error", { "beforeColon": true }]*/
62 var obj = { "foo" : 42 };
67 Examples of **incorrect** code for this rule with the default `{ "afterColon": true }` option:
70 /*eslint key-spacing: ["error", { "afterColon": true }]*/
72 var obj = { "foo":42 };
75 Examples of **correct** code for this rule with the default `{ "afterColon": true }` option:
78 /*eslint key-spacing: ["error", { "afterColon": true }]*/
80 var obj = { "foo": 42 };
83 Examples of **incorrect** code for this rule with the `{ "afterColon": false }` option:
86 /*eslint key-spacing: ["error", { "afterColon": false }]*/
88 var obj = { "foo": 42 };
91 Examples of **correct** code for this rule with the `{ "afterColon": false }` option:
94 /*eslint key-spacing: ["error", { "afterColon": false }]*/
96 var obj = { "foo":42 };
101 Examples of **incorrect** code for this rule with the default `{ "mode": "strict" }` option:
104 /*eslint key-spacing: ["error", { "mode": "strict" }]*/
112 Examples of **correct** code for this rule with the default `{ "mode": "strict" }` option:
115 /*eslint key-spacing: ["error", { "mode": "strict" }]*/
123 Examples of **correct** code for this rule with the `{ "mode": "minimum" }` option:
126 /*eslint key-spacing: ["error", { "mode": "minimum" }]*/
136 Examples of **incorrect** code for this rule with the `{ "align": "value" }` option:
139 /*eslint key-spacing: ["error", { "align": "value" }]*/
148 Examples of **correct** code for this rule with the `{ "align": "value" }` option:
151 /*eslint key-spacing: ["error", { "align": "value" }]*/
161 ijkl: 'Non-consecutive lines form a new group'
164 var obj = { a: "foo", longPropertyName: "bar" };
167 Examples of **incorrect** code for this rule with the `{ "align": "colon" }` option:
170 /*eslint key-spacing: ["error", { "align": "colon" }]*/
178 Examples of **correct** code for this rule with the `{ "align": "colon" }` option:
181 /*eslint key-spacing: ["error", { "align": "colon" }]*/
191 The `align` option can take additional configuration through the `beforeColon`, `afterColon`, `mode`, and `on` options.
193 If `align` is defined as an object, but not all of the parameters are provided, undefined parameters will default to the following:
198 "beforeColon": false,
205 Examples of **correct** code for this rule with sample `{ "align": { } }` options:
208 /*eslint key-spacing: ["error", {
223 /*eslint key-spacing: ["error", {
225 "beforeColon": false,
237 ### align and multiLine
239 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.
241 `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:
245 key1: 1, // uses multiLine
247 key2: 2, // uses align (when defined)
248 key3: 3, // uses align (when defined)
250 key4: 4 // uses multiLine
255 Examples of **incorrect** code for this rule with sample `{ "align": { }, "multiLine": { } }` options:
258 /*eslint key-spacing: ["error", {
260 "beforeColon": false,
271 "myObjectFunction": function() {
279 Examples of **correct** code for this rule with sample `{ "align": { }, "multiLine": { } }` options:
282 /*eslint key-spacing: ["error", {
284 "beforeColon": false,
296 "myObjectFunction": function() {
299 }, // These are two separate groups, so no alignment between `myObjectFuction` and `one`
301 "seven" : 7 // `one` and `seven` are in their own group, and therefore aligned
305 ### singleLine and multiLine
307 Examples of **correct** code for this rule with sample `{ "singleLine": { }, "multiLine": { } }` options:
310 /*eslint "key-spacing": [2, {
312 "beforeColon": false,
321 var obj = { one: 1, "two": 2, three: 3 };
328 ## When Not To Use It
330 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.