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