# one-var

> Require variables to be declared in one statement, or in one statement each

`let a, b;` and `let a; let b;` do the same thing. This rule makes a codebase pick one of the two.

By default it asks for one `var` statement in each function, and one `let` and one `const` statement in each block. `"never"` asks for the opposite, one variable in each statement, so you can add, remove, or move a line without touching its neighbors.

`--fix` splits a statement, and joins a statement with the one right before it.

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

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"one-var" = ["error", "always"] # "always", "never" or "consecutive"
```

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

## Examples

Reported:

{/* bad */}
```js
function area(shape) {
  const width = shape.width;
  const height = shape.height;
  return width * height;
}
```

Not reported:

{/* good */}
```js
function area(shape) {
  const width = shape.width,
    height = shape.height;
  return width * height;
}
```

## Options

```toml bunfig.toml
[lint.rules]
one-var = ["error", "never"]
```

| Option | What it does |
| --- | --- |
| `"always"` (default) | One statement for each kind of declaration in a scope. |
| `"never"` | One variable in each statement. |
| `"consecutive"` | Statements of the same kind that follow each other must be joined. Statements with other code between them are allowed. |

With `"never"`, this is reported:

{/* bad {"options": ["never"]} */}
```js
let first = 1, second = 2;
```

And this isn't. A `for` loop's head is exempt:

{/* good {"options": ["never"]} */}
```js
let first = 1;
let second = 2;

for (let i = 0, end = 10; i < end; i++) {}
```

With `"consecutive"`, this is reported:

{/* bad {"options": ["consecutive"]} */}
```js
let first = 1;
let second = 2;
```

And this isn't:

{/* good {"options": ["consecutive"]} */}
```js
let first = 1;
log(first);
let second = 2;
```

Instead of the string you can pass an object, in one of two forms. Each mode is `"always"`, `"never"`, or `"consecutive"`.

| Option | What it does |
| --- | --- |
| `var`, `let`, `const`, `using`, `awaitUsing` | The mode for that kind of declaration. A kind you leave out isn't checked. |
| `separateRequires` | With `"always"`, variables that are set to a `require()` call get their own statement. Default `false`. |

With `{ "let": "never" }`, `const` isn't checked:

{/* good {"options": [{"let": "never"}]} */}
```js
let first;
let second;
const width = 1, height = 2;
```

With `{ "var": "always", "separateRequires": true }`, this is reported:

{/* bad {"options": [{"var": "always", "separateRequires": true}]} */}
```js
var fs = require("fs"), limit = 10;
```

And this isn't:

{/* good {"options": [{"var": "always", "separateRequires": true}]} */}
```js
var fs = require("fs"), path = require("path");
var limit = 10;
```

You can't mix the other form with the first:

| Option | What it does |
| --- | --- |
| `initialized`, `uninitialized` | The mode for variables with and without a value, for all kinds. |

With `{ "initialized": "never", "uninitialized": "always" }`, this is allowed:

{/* good {"options": [{"initialized": "never", "uninitialized": "always"}]} */}
```js
let first, second;
let width = 1;
let height = 2;
```
