1 # Require Object Literal Shorthand Syntax (object-shorthand)
3 ECMAScript 6 provides a concise form for defining object literal methods and properties. This
4 syntax can make defining complex object literals much cleaner.
6 Here are a few common examples using the ES5 syntax:
23 Now here are ES6 equivalents:
40 This rule enforces the use of the shorthand syntax. This applies
41 to all methods (including generators) defined in object literals and any
42 properties defined where the key name matches name of the assigned variable.
44 Each of the following properties would warn:
47 /*eslint object-shorthand: "error"*/
58 In that case the expected syntax would have been:
61 /*eslint object-shorthand: "error"*/
72 This rule does not flag arrow functions inside of object literals.
73 The following will *not* warn:
76 /*eslint object-shorthand: "error"*/
86 - [`no-useless-rename`](https://eslint.org/docs/rules/no-useless-rename) which disallows renaming import, export, and destructured assignments to the same name.
90 The rule takes an option which specifies when it should be applied. It can be set to one of the following values:
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.
99 You can set the option in configuration like this:
103 "object-shorthand": ["error", "always"]
107 Additionally, the rule takes an optional object configuration:
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"`.
117 "object-shorthand": ["error", "always", { "avoidQuotes": true }]
121 Example of **incorrect** code for this rule with the `"always", { "avoidQuotes": true }` option:
124 /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
132 Example of **correct** code for this rule with the `"always", { "avoidQuotes": true }` option:
135 /*eslint object-shorthand: ["error", "always", { "avoidQuotes": true }]*/
139 "bar-baz": function() {},
144 ### `ignoreConstructors`
148 "object-shorthand": ["error", "always", { "ignoreConstructors": true }]
152 Example of **correct** code for this rule with the `"always", { "ignoreConstructors": true }` option:
155 /*eslint object-shorthand: ["error", "always", { "ignoreConstructors": true }]*/
159 ConstructorFunction: function() {}
163 ### `avoidExplicitReturnArrows`
167 "object-shorthand": ["error", "always", { "avoidExplicitReturnArrows": true }]
171 Example of **incorrect** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option:
174 /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
188 Example of **correct** code for this rule with the `"always", { "avoidExplicitReturnArrows": true }` option:
191 /*eslint object-shorthand: ["error", "always", { "avoidExplicitReturnArrows": true }]*/
199 qux: foobar => foobar * 2
203 Example of **incorrect** code for this rule with the `"consistent"` option:
206 /*eslint object-shorthand: [2, "consistent"]*/
215 Examples of **correct** code for this rule with the `"consistent"` option:
218 /*eslint object-shorthand: [2, "consistent"]*/
232 Example of **incorrect** code with the `"consistent-as-needed"` option, which is very similar to `"consistent"`:
235 /*eslint object-shorthand: [2, "consistent-as-needed"]*/
244 ## When Not To Use It
246 Anyone not yet in an ES6 environment would not want to apply this rule. Others may find the terseness of the shorthand
247 syntax harder to read and may not want to encourage it with this rule.
251 [Object initializer - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer)