# id-length

> Enforce a minimum and a maximum length for identifier names

A one-letter name such as `e`, `n` or `x` sends you back to the declaration to find out what it holds. A very long name makes every line that uses it hard to scan.

The rule checks a name where your code introduces it: variables, parameters, functions, classes, imports, object keys and class members. By default a name needs at least 2 characters, and there's no maximum.

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

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

```toml bunfig.toml
[lint.rules]
"id-length" = ["error", {
  min = 2,
  # max = <number>,
  properties = "always", # "always" or "never"
  exceptions = [], # list of strings
  exceptionPatterns = [], # list of strings
}]
```

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

## Examples

Reported:

{/* bad {"count": 4} */}
```js
const n = items.length;

const prices = items.map((x) => x.price);

try {
  save();
} catch (e) {
  report(e);
}

const point = { y: 0 };
```

Not reported:

{/* good */}
```js
const count = items.length;

const prices = items.map((item) => item.price);

try {
  save();
} catch (error) {
  report(error);
}
```

Type parameters aren't checked:

{/* good */}
```ts
function first<T>(items: T[]): T {
  return items[0];
}
```

## Options

```toml bunfig.toml
[lint.rules]
id-length = ["error", { min = 3, max = 30, exceptions = ["i", "id"] }]
```

| Option | Default | What it does |
| --- | --- | --- |
| `min` | `2` | The shortest name allowed. |
| `max` | none | The longest name allowed. |
| `properties` | `"always"` | `"never"` stops checking object keys and the properties you assign to. |
| `exceptions` | `[]` | Names that are always allowed. |
| `exceptionPatterns` | `[]` | Regular expressions. A name that matches one is always allowed. |
| `checkGeneric` | `true` | Only for an oxlint config, where the rule also checks type parameters such as the `T` in `Box<T>`. `false` stops checking them. `bunfig.toml` rejects this option. |

{/* bad {"options": [{"max": 20}]} */}
```js
const numberOfItemsInTheShoppingCart = cart.items.length;
```

{/* good {"options": [{"exceptions": ["i"]}]} */}
```js
for (let i = 0; i < items.length; i++) {
  render(items[i]);
}
```

{/* good {"options": [{"exceptionPatterns": ["^[xyz]$"]}]} */}
```js
function move(x, y, z) {
  return [x + 1, y + 1, z + 1];
}
```

### `properties`

{/* good {"options": [{"properties": "never"}]} */}
```js
const point = { x: 0, y: 0 };
point.x = 10;
```

Variables and class members are still checked:

{/* bad {"options": [{"properties": "never"}], "count": 2} */}
```js
const p = { x: 0, y: 0 };

class Point {
  x = 0;
}
```
