2 title: computed-property-spacing
6 - array-bracket-spacing
13 While formatting preferences are very personal, a number of style guides require
14 or disallow spaces between computed properties in the following situations:
19 var obj = { prop: "value" };
21 var x = obj[a]; // computed property in object member expression
25 [a]: "value" // computed property key in object literal (ECMAScript 6)
28 var obj = { prop: "value" };
30 var { [a]: x } = obj; // computed property key in object destructuring pattern (ECMAScript 6)
35 This rule enforces consistent spacing inside computed property brackets.
37 It either requires or disallows spaces between the brackets and the values inside of them.
38 This rule does not apply to brackets that are separated from the adjacent value by a newline.
42 This rule has two options, a string option and an object option.
46 * `"never"` (default) disallows spaces inside computed property brackets
47 * `"always"` requires one or more spaces inside computed property brackets
51 * `"enforceForClassMembers": true` (default) additionally applies this rule to class members.
55 Examples of **incorrect** code for this rule with the default `"never"` option:
60 /*eslint computed-property-spacing: ["error", "never"]*/
68 const { [ a ]: someProp } = obj;
69 ({ [ b ]: anotherProp } = anotherObj);
74 Examples of **correct** code for this rule with the default `"never"` option:
79 /*eslint computed-property-spacing: ["error", "never"]*/
87 const { [a]: someProp } = obj;
88 ({ [b]: anotherProp } = anotherObj);
95 Examples of **incorrect** code for this rule with the `"always"` option:
100 /*eslint computed-property-spacing: ["error", "always"]*/
109 const { [a]: someProp } = obj;
110 ({ [b ]: anotherProp } = anotherObj);
115 Examples of **correct** code for this rule with the `"always"` option:
120 /*eslint computed-property-spacing: ["error", "always"]*/
127 const { [ a ]: someProp } = obj;
128 ({ [ b ]: anotherProp } = anotherObj);
133 #### enforceForClassMembers
135 With `enforceForClassMembers` set to `true` (default), the rule also disallows/enforces spaces inside of computed keys of class methods, getters and setters.
137 Examples of **incorrect** code for this rule with `"never"` and `{ "enforceForClassMembers": true }` (default):
142 /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
154 static get [ c ]() {}
155 static set [ c ](value) {}
161 Examples of **correct** code for this rule with `"never"` and `{ "enforceForClassMembers": true }` (default):
166 /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
179 static set [c](value) {}
185 Examples of **correct** code for this rule with `"never"` and `{ "enforceForClassMembers": false }`:
190 /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": false }]*/
202 static get [ c ]() {}
203 static set [ c ](value) {}
209 ## When Not To Use It
211 You can turn this rule off if you are not concerned with the consistency of computed properties.