# func-names

> Require or disallow names for `function` expressions

A callback written as `function () {}` shows up as `<anonymous>` in stack traces and profiles. When several callbacks are involved, you can't tell from the trace which one failed.

Arrow functions and methods aren't checked.

{/* 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]
"func-names" = "error" # or "warn"
```

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

```toml bunfig.toml
[lint.rules]
"func-names" = [
  "error",
  "always", # "always", "as-needed" or "never"
  {
    # generators = <"always", "as-needed" or "never">,
  },
]
```

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

## Examples

Reported:

{/* bad */}
```js
button.addEventListener("click", function () {
  save();
});
```

{/* bad */}
```js
export default function () {
  return "ready";
}
```

Not reported:

{/* good */}
```js
button.addEventListener("click", function onClick() {
  save();
});

button.addEventListener("blur", () => save());
```

## Options

```toml bunfig.toml
[lint.rules]
func-names = ["error", "as-needed"]
```

| Option | What it does |
| --- | --- |
| `"always"` (default) | Every function expression needs a name. |
| `"as-needed"` | A name is only needed where JavaScript doesn't give the function one on its own. |
| `"never"` | No function expression can have a name, unless the function refers to itself by that name. |

A second option, `{ "generators": "always" | "as-needed" | "never" }`, sets the same choice for generator functions only. Without it, generators follow the first option.

### `"as-needed"`

A function that's assigned to a variable or used as the value of an object key takes that name, so it doesn't need its own:

{/* good {"options": ["as-needed"]} */}
```js
const save = function () {};

const handlers = {
  close: function () {},
};
```

With `"always"`, both are reported:

{/* bad {"count": 2} */}
```js
const save = function () {};

const handlers = {
  close: function () {},
};
```

A callback doesn't get a name that way, and neither does a function assigned to `object.property`. Both are still reported:

{/* bad {"options": ["as-needed"], "count": 2} */}
```js
items.forEach(function (item) {
  render(item);
});

handlers.open = function () {};
```

### `"never"`

{/* bad {"options": ["never"]} */}
```js
items.forEach(function renderItem(item) {
  render(item);
});
```

{/* good {"options": ["never"]} */}
```js
items.forEach(function (item) {
  render(item);
});

const factorial = function fact(n) {
  return n <= 1 ? 1 : n * fact(n - 1);
};
```

### `generators`

{/* good {"options": ["always", {"generators": "never"}]} */}
```js
run(function* () {
  yield 1;
});
```
