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.
Configure#
Off by default. To turn it on:
[lint.rules]
"one-var" = "error" # or "warn"Every option, set to its default:
[lint.rules]
"one-var" = ["error", "always"] # "always", "never" or "consecutive"Examples#
function area(shape) {
const width = shape.width;
const height = shape.height;Combine this with the previous const statement.
return width * height;
}function area(shape) {
const width = shape.width,
height = shape.height;
return width * height;
}Options#
[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:
"never"1 problemlet first = 1, second = 2;Split let declarations into multiple statements.And this isn't. A for loop's head is exempt:
"never"No problemslet first = 1;
let second = 2;
for (let i = 0, end = 10; i < end; i++) {}With "consecutive", this is reported:
"consecutive"1 problemlet first = 1;
let second = 2;Combine this with the previous let statement.And this isn't:
"consecutive"No problemslet 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:
{ "let": "never" }No problemslet first;
let second;
const width = 1, height = 2;With { "var": "always", "separateRequires": true }, this is reported:
{ "var": "always", "separateRequires": true }1 problemvar fs = require("fs"), limit = 10;Split requires to be separated into a single block.And this isn't:
{ "var": "always", "separateRequires": true }No problemsvar 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:
{ "initialized": "never", "uninitialized": "always" }No problemslet first, second;
let width = 1;
let height = 2;