# 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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml 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.

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```js
const user = {
  name: name,
  save: function () {
    store(this);
  },
};
```

Not reported:

{/* good */}
```js
const user = {
  name,
  email: address,
  save() {
    store(this);
  },
  load: () => fetchUser(name),
};
```

## Options

```toml bunfig.toml
[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:

{/* bad {"options": ["never"], "count": 2} */}
```js
const user = { name, save() {} };
```

With `"consistent"`, a mix is reported:

{/* bad {"options": ["consistent"]} */}
```js
const user = { name, email: address };
```

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

{/* good {"options": ["consistent"]} */}
```js
const user = { name: name, email: address };
```

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

{/* bad {"options": ["consistent-as-needed"]} */}
```js
const user = { name: name, email: email };
```

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

{/* bad {"options": ["always", {"avoidQuotes": true}]} */}
```js
const handlers = { "user-created"() {} };
```

{/* good {"options": ["always", {"avoidQuotes": true}]} */}
```js
const handlers = { "user-created": function () {} };
```

With `ignoreConstructors`, this is allowed:

{/* good {"options": ["always", {"ignoreConstructors": true}]} */}
```js
const models = { User: function () {} };
```

With `avoidExplicitReturnArrows`, this is reported:

{/* bad {"options": ["always", {"avoidExplicitReturnArrows": true}]} */}
```js
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:

{/* good {"options": ["always", {"avoidExplicitReturnArrows": true}]} */}
```js
function createUser() {
  return {
    save: () => {
      store(this);
    },
  };
}
```
