object-shorthand
Require or disallow the short forms { name } and { save() {} } in object literals
{ name: name } can be written { name }, and { save: function () {} } can be written { save() {} }. The short forms say the same thing with less to read. This rule makes a codebase use one form everywhere.
--fix rewrites a property to the form you chose. The "consistent" modes have no fix.
Configure#
Off by default. To turn it on:
[lint.rules]
"object-shorthand" = "error" # or "warn"Every option, set to its default. The ones that are commented out are unset by default.
[lint.rules]
"object-shorthand" = ["error", "always", {
avoidQuotes = false,
ignoreConstructors = false,
# methodsIgnorePattern = <string>,
avoidExplicitReturnArrows = false,
}]Examples#
const user = {
name: nameExpected property shorthand.,
save: function () {Expected method shorthand.
store(this);
},
};const user = {
name,
email: address,
save() {
store(this);
},
load: () => fetchUser(name),
};Options#
[lint.rules]
object-shorthand = ["error", "always", { avoidQuotes = true }]The first option:
| Option | What it does |
|---|---|
"always" (default) | Require the short form for properties and methods. |
"methods" | Require it for methods only. |
"properties" | Require it for properties only. |
"never" | Report every short form. |
"consistent" | Within one object, either all properties are short or none are. |
"consistent-as-needed" | The same, and if all of them can be short, they must be. |
The second option is an object:
| Option | Default | Works with | What it does |
|---|---|---|---|
avoidQuotes | false | "always", "methods", "properties" | Require the long form where the key is a string literal. |
ignoreConstructors | false | "always", "methods" | Allow the long form where the key starts with a capital letter. A method can't be called with new. |
methodsIgnorePattern | none | "always", "methods" | Allow the long form where the key matches this regular expression, such as "^on". |
avoidExplicitReturnArrows | false | "always", "methods" | Also require a method instead of an arrow function with a block body. |
With "never", both properties are reported:
"never"2 problemsconst user = { nameExpected longform property syntax., save() {}Expected longform method syntax. };With "consistent", a mix is reported:
"consistent"1 problemconst user = { name, email: address }Unexpected mix of shorthand and non-shorthand properties.;email has no short form, so name must be long too:
"consistent"No problemsconst user = { name: name, email: address };With "consistent-as-needed", this is reported, because both properties can be short:
"consistent-as-needed"1 problemconst user = { name: name, email: email }Expected shorthand for all properties.;With avoidQuotes, the first object is reported and the second isn't:
"always", { "avoidQuotes": true }1 problemconst handlers = { "user-created"() {}Expected longform method syntax for string literal keys. };"always", { "avoidQuotes": true }No problemsconst handlers = { "user-created": function () {} };With ignoreConstructors, this is allowed:
"always", { "ignoreConstructors": true }No problemsconst models = { User: function () {} };With avoidExplicitReturnArrows, this is reported:
"always", { "avoidExplicitReturnArrows": true }1 problemconst user = {
save: () => {Expected method shorthand.
store(name);
},
};An arrow function that uses this, super, arguments, or new.target is left alone, because they would mean something else in a method:
"always", { "avoidExplicitReturnArrows": true }No problemsfunction createUser() {
return {
save: () => {
store(this);
},
};
}