1 # Disallow or enforce spaces inside of computed properties (computed-property-spacing)
3 While formatting preferences are very personal, a number of style guides require
4 or disallow spaces between computed properties in the following situations:
9 var obj = { prop: "value" };
11 var x = obj[a]; // computed property in object member expression
15 [a]: "value" // computed property key in object literal (ECMAScript 6)
21 This rule enforces consistent spacing inside computed property brackets.
23 It either requires or disallows spaces between the brackets and the values inside of them.
24 This rule does not apply to brackets that are separated from the adjacent value by a newline.
28 This rule has two options, a string option and an object option.
32 * `"never"` (default) disallows spaces inside computed property brackets
33 * `"always"` requires one or more spaces inside computed property brackets
37 * `"enforceForClassMembers": true` (default) additionally applies this rule to class members.
41 Examples of **incorrect** code for this rule with the default `"never"` option:
44 /*eslint computed-property-spacing: ["error", "never"]*/
53 Examples of **correct** code for this rule with the default `"never"` option:
56 /*eslint computed-property-spacing: ["error", "never"]*/
67 Examples of **incorrect** code for this rule with the `"always"` option:
70 /*eslint computed-property-spacing: ["error", "always"]*/
81 Examples of **correct** code for this rule with the `"always"` option:
84 /*eslint computed-property-spacing: ["error", "always"]*/
93 #### enforceForClassMembers
95 With `enforceForClassMembers` set to `true` (default), the rule also disallows/enforces spaces inside of computed keys of class methods, getters and setters.
97 Examples of **incorrect** code for this rule with `"never"` and `{ "enforceForClassMembers": true }` (default):
100 /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
112 static get [ c ]() {}
113 static set [ c ](value) {}
117 Examples of **correct** code for this rule with `"never"` and `{ "enforceForClassMembers": true }` (default):
120 /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": true }]*/
133 static set [c](value) {}
137 Examples of **correct** code for this rule with `"never"` and `{ "enforceForClassMembers": false }`:
140 /*eslint computed-property-spacing: ["error", "never", { "enforceForClassMembers": false }]*/
152 static get [ c ]() {}
153 static set [ c ](value) {}
157 ## When Not To Use It
159 You can turn this rule off if you are not concerned with the consistency of computed properties.
163 * [array-bracket-spacing](array-bracket-spacing.md)
164 * [comma-spacing](comma-spacing.md)
165 * [space-in-parens](space-in-parens.md)