one-var

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

fixable

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.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"one-var" = "error" # or "warn"

Every option, set to its default:

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

Examples#

example.js1 problem
function area(shape) {
  const width = shape.width;
  const height = shape.height;
  return width * height;
}
example.jsNo problems
function area(shape) {
  const width = shape.width,
    height = shape.height;
  return width * height;
}

Options#

bunfig.toml
[lint.rules]
one-var = ["error", "never"]
OptionWhat 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:

example.jswith "never"1 problem
let first = 1, second = 2;

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

example.jswith "never"No problems
let first = 1;
let second = 2;

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

With "consecutive", this is reported:

example.jswith "consecutive"1 problem
let first = 1;
let second = 2;

And this isn't:

example.jswith "consecutive"No problems
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".

OptionWhat it does
var, let, const, using, awaitUsingThe mode for that kind of declaration. A kind you leave out isn't checked.
separateRequiresWith "always", variables that are set to a require() call get their own statement. Default false.

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

example.jswith { "let": "never" }No problems
let first;
let second;
const width = 1, height = 2;

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

example.jswith { "var": "always", "separateRequires": true }1 problem
var fs = require("fs"), limit = 10;

And this isn't:

example.jswith { "var": "always", "separateRequires": true }No problems
var fs = require("fs"), path = require("path");
var limit = 10;

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

OptionWhat it does
initialized, uninitializedThe mode for variables with and without a value, for all kinds.

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

example.jswith { "initialized": "never", "uninitialized": "always" }No problems
let first, second;
let width = 1;
let height = 2;