Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

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:

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

Reported:

const user = {
  name: name,
  save: function () {
    store(this);
  },
};

Not reported:

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:

const user = { name, save() {} };

With "consistent", a mix is reported:

const user = { name, email: address };

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

const user = { name: name, email: address };

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

const user = { name: name, email: email };

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

const handlers = { "user-created"() {} };
const handlers = { "user-created": function () {} };

With ignoreConstructors, this is allowed:

const models = { User: function () {} };

With avoidExplicitReturnArrows, this is reported:

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:

function createUser() {
  return {
    save: () => {
      store(this);
    },
  };
}