object-shorthand

Require or disallow the short forms { name } and { save() {} } in object literals

fixable

{ 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:

bunfig.toml
[lint.rules]
"object-shorthand" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"object-shorthand" = ["error", "always", {
  avoidQuotes = false,
  ignoreConstructors = false,
  # methodsIgnorePattern = <string>,
  avoidExplicitReturnArrows = false,
}]

Examples#

example.js2 problems
const user = {
  name: name,
  save: function () {
    store(this);
  },
};
example.jsNo problems
const user = {
  name,
  email: address,
  save() {
    store(this);
  },
  load: () => fetchUser(name),
};

Options#

bunfig.toml
[lint.rules]
object-shorthand = ["error", "always", { avoidQuotes = true }]

The first option:

OptionWhat 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:

OptionDefaultWorks withWhat it does
avoidQuotesfalse"always", "methods", "properties"Require the long form where the key is a string literal.
ignoreConstructorsfalse"always", "methods"Allow the long form where the key starts with a capital letter. A method can't be called with new.
methodsIgnorePatternnone"always", "methods"Allow the long form where the key matches this regular expression, such as "^on".
avoidExplicitReturnArrowsfalse"always", "methods"Also require a method instead of an arrow function with a block body.

With "never", both properties are reported:

example.jswith "never"2 problems
const user = { name, save() {} };

With "consistent", a mix is reported:

example.jswith "consistent"1 problem
const user = { name, email: address };

email has no short form, so name must be long too:

example.jswith "consistent"No problems
const user = { name: name, email: address };

With "consistent-as-needed", this is reported, because both properties can be short:

example.jswith "consistent-as-needed"1 problem
const user = { name: name, email: email };

With avoidQuotes, the first object is reported and the second isn't:

example.jswith "always", { "avoidQuotes": true }1 problem
const handlers = { "user-created"() {} };
example.jswith "always", { "avoidQuotes": true }No problems
const handlers = { "user-created": function () {} };

With ignoreConstructors, this is allowed:

example.jswith "always", { "ignoreConstructors": true }No problems
const models = { User: function () {} };

With avoidExplicitReturnArrows, this is reported:

example.jswith "always", { "avoidExplicitReturnArrows": true }1 problem
const user = {
  save: () => {
    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:

example.jswith "always", { "avoidExplicitReturnArrows": true }No problems
function createUser() {
  return {
    save: () => {
      store(this);
    },
  };
}