2 title: object-shorthand
8 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
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.
16 Here are a few common examples using the ES5 syntax:
33 Now here are ES6 equivalents:
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.
54 Each of the following properties would warn:
57 /*eslint object-shorthand: "error"*/
68 In that case the expected syntax would have been:
71 /*eslint object-shorthand: "error"*/
82 This rule does not flag arrow functions inside of object literals.
83 The following will *not* warn:
86 /*eslint object-shorthand: "error"*/
96 The rule takes an option which specifies when it should be applied. It can be set to one of the following values:
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.
105 You can set the option in configuration like this:
109 "object-shorthand": ["error", "always"]
113 Additionally, the rule takes an optional object configuration:
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"`.
124 "object-shorthand": ["error", "always", { "avoidQuotes": true }]
128 Example of **incorrect** code for this rule with the `"always", { "avoidQuotes": true }` option:
133 /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
143 Example of **correct** code for this rule with the `"always", { "avoidQuotes": true }` option:
148 /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
152 "bar-baz": function() {},
159 ### `ignoreConstructors`
163 "object-shorthand": ["error", "always", { "ignoreConstructors": true }]
167 Example of **correct** code for this rule with the `"always", { "ignoreConstructors": true }` option:
172 /*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/
176 ConstructorFunction: function() {}
182 ### `methodsIgnorePattern`
184 Example of **correct** code for this rule with the `"always", { "methodsIgnorePattern": "^bar$" }` option:
189 /*eslint object-shorthand: ["error", "always", { "methodsIgnorePattern": "^bar$" }]*/
198 ### `avoidExplicitReturnArrows`
202 "object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
206 Example of **incorrect** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option:
211 /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
227 Example of **correct** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option:
232 /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
240 qux: foobar => foobar * 2
246 Example of **incorrect** code for this rule with the `"consistent"` option:
251 /*eslint object-shorthand: [2, "consistent"]*/
262 Examples of **correct** code for this rule with the `"consistent"` option:
267 /*eslint object-shorthand: [2, "consistent"]*/
283 Example of **incorrect** code with the `"consistent-as-needed"` option, which is very similar to `"consistent"`:
288 /*eslint object-shorthand: [2, "consistent-as-needed"]*/
299 ## When Not To Use It
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.