]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/object-shorthand.md
08651268a8adb9b3eac8d59e29c80eb7ea18dd16
[pve-eslint.git] / eslint / docs / src / rules / object-shorthand.md
1 ---
2 title: object-shorthand
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - no-useless-rename
7 further_reading:
8 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
9 ---
10
11
12
13 ECMAScript 6 provides a concise form for defining object literal methods and properties. This
14 syntax can make defining complex object literals much cleaner.
15
16 Here are a few common examples using the ES5 syntax:
17
18 ```js
19 // properties
20 var foo = {
21 x: x,
22 y: y,
23 z: z,
24 };
25
26 // methods
27 var foo = {
28 a: function() {},
29 b: function() {}
30 };
31 ```
32
33 Now here are ES6 equivalents:
34
35 ```js
36 /*eslint-env es6*/
37
38 // properties
39 var foo = {x, y, z};
40
41 // methods
42 var foo = {
43 a() {},
44 b() {}
45 };
46 ```
47
48 ## Rule Details
49
50 This rule enforces the use of the shorthand syntax. This applies
51 to all methods (including generators) defined in object literals and any
52 properties defined where the key name matches name of the assigned variable.
53
54 Each of the following properties would warn:
55
56 ```js
57 /*eslint object-shorthand: "error"*/
58 /*eslint-env es6*/
59
60 var foo = {
61 w: function() {},
62 x: function *() {},
63 [y]: function() {},
64 z: z
65 };
66 ```
67
68 In that case the expected syntax would have been:
69
70 ```js
71 /*eslint object-shorthand: "error"*/
72 /*eslint-env es6*/
73
74 var foo = {
75 w() {},
76 *x() {},
77 [y]() {},
78 z
79 };
80 ```
81
82 This rule does not flag arrow functions inside of object literals.
83 The following will *not* warn:
84
85 ```js
86 /*eslint object-shorthand: "error"*/
87 /*eslint-env es6*/
88
89 var foo = {
90 x: (y) => y
91 };
92 ```
93
94 ## Options
95
96 The rule takes an option which specifies when it should be applied. It can be set to one of the following values:
97
98 * `"always"` (default) expects that the shorthand will be used whenever possible.
99 * `"methods"` ensures the method shorthand is used (also applies to generators).
100 * `"properties"` ensures the property shorthand is used (where the key and variable name match).
101 * `"never"` ensures that no property or method shorthand is used in any object literal.
102 * `"consistent"` ensures that either all shorthand or all long-form will be used in an object literal.
103 * `"consistent-as-needed"` ensures that either all shorthand or all long-form will be used in an object literal, but ensures all shorthand whenever possible.
104
105 You can set the option in configuration like this:
106
107 ```json
108 {
109 "object-shorthand": ["error", "always"]
110 }
111 ```
112
113 Additionally, the rule takes an optional object configuration:
114
115 * `"avoidQuotes": true` indicates that long-form syntax is preferred whenever the object key is a string literal (default: `false`). Note that this option can only be enabled when the string option is set to `"always"`, `"methods"`, or `"properties"`.
116 * `"ignoreConstructors": true` can be used to prevent the rule from reporting errors for constructor functions. (By default, the rule treats constructors the same way as other functions.) Note that this option can only be enabled when the string option is set to `"always"` or `"methods"`.
117 * `"methodsIgnorePattern"` (`string`) for methods whose names match this regex pattern, the method shorthand will not be enforced. Note that this option can only be used when the string option is set to `"always"` or `"methods"`.
118 * `"avoidExplicitReturnArrows": true` indicates that methods are preferred over explicit-return arrow functions for function properties. (By default, the rule allows either of these.) Note that this option can only be enabled when the string option is set to `"always"` or `"methods"`.
119
120 ### `avoidQuotes`
121
122 ```json
123 {
124 "object-shorthand": ["error", "always", { "avoidQuotes": true }]
125 }
126 ```
127
128 Example of **incorrect** code for this rule with the `"always", { "avoidQuotes": true }` option:
129
130 ::: incorrect
131
132 ```js
133 /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
134 /*eslint-env es6*/
135
136 var foo = {
137 "bar-baz"() {}
138 };
139 ```
140
141 :::
142
143 Example of **correct** code for this rule with the `"always", { "avoidQuotes": true }` option:
144
145 ::: correct
146
147 ```js
148 /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
149 /*eslint-env es6*/
150
151 var foo = {
152 "bar-baz": function() {},
153 "qux": qux
154 };
155 ```
156
157 :::
158
159 ### `ignoreConstructors`
160
161 ```json
162 {
163 "object-shorthand": ["error", "always", { "ignoreConstructors": true }]
164 }
165 ```
166
167 Example of **correct** code for this rule with the `"always", { "ignoreConstructors": true }` option:
168
169 ::: correct
170
171 ```js
172 /*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/
173 /*eslint-env es6*/
174
175 var foo = {
176 ConstructorFunction: function() {}
177 };
178 ```
179
180 :::
181
182 ### `methodsIgnorePattern`
183
184 Example of **correct** code for this rule with the `"always", { "methodsIgnorePattern": "^bar$" }` option:
185
186 ::: correct
187
188 ```js
189 /*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/
190
191 var foo = {
192 bar: function() {}
193 };
194 ```
195
196 :::
197
198 ### `avoidExplicitReturnArrows`
199
200 ```json
201 {
202 "object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
203 }
204 ```
205
206 Example of **incorrect** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option:
207
208 ::: incorrect
209
210 ```js
211 /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
212 /*eslint-env es6*/
213
214 var foo = {
215 foo: (bar, baz) => {
216 return bar + baz;
217 },
218
219 qux: (foobar) => {
220 return foobar * 2;
221 }
222 };
223 ```
224
225 :::
226
227 Example of **correct** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option:
228
229 ::: correct
230
231 ```js
232 /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
233 /*eslint-env es6*/
234
235 var foo = {
236 foo(bar, baz) {
237 return bar + baz;
238 },
239
240 qux: foobar => foobar * 2
241 };
242 ```
243
244 :::
245
246 Example of **incorrect** code for this rule with the `"consistent"` option:
247
248 ::: incorrect
249
250 ```js
251 /*eslint object-shorthand: [2, "consistent"]*/
252 /*eslint-env es6*/
253
254 var foo = {
255 a,
256 b: "foo",
257 };
258 ```
259
260 :::
261
262 Examples of **correct** code for this rule with the `"consistent"` option:
263
264 ::: correct
265
266 ```js
267 /*eslint object-shorthand: [2, "consistent"]*/
268 /*eslint-env es6*/
269
270 var foo = {
271 a: a,
272 b: "foo"
273 };
274
275 var bar = {
276 a,
277 b,
278 };
279 ```
280
281 :::
282
283 Example of **incorrect** code with the `"consistent-as-needed"` option, which is very similar to `"consistent"`:
284
285 ::: incorrect
286
287 ```js
288 /*eslint object-shorthand: [2, "consistent-as-needed"]*/
289 /*eslint-env es6*/
290
291 var foo = {
292 a: a,
293 b: b,
294 };
295 ```
296
297 :::
298
299 ## When Not To Use It
300
301 Anyone not yet in an ES6 environment would not want to apply this rule. Others may find the terseness of the shorthand
302 syntax harder to read and may not want to encourage it with this rule.