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