useSingleVarDeclarator
Diagnostic Category: lint/style/useSingleVarDeclarator
Since: v1.0.0
Sources:
- Same as:
one-var
Description
Section titled DescriptionDisallow multiple variable declarations in the same variable statement
In JavaScript, multiple variables can be declared within a single var
, const
or let
declaration.
It is often considered a best practice to declare every variable separately.
That is what this rule enforces.
Examples
Section titled ExamplesInvalid
Section titled Invalidlet foo = 0, bar, baz;
code-block.js:1:1 lint/style/useSingleVarDeclarator FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Declare variables separately
> 1 │ let foo = 0, bar, baz;
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Unsafe fix: Break out into multiple declarations
1 │ - let·foo·=·0,·bar,·baz;
1 │ + let·foo·=·0;
2 │ + let·bar;
3 │ + let·baz;
2 4 │
Valid
Section titled Validconst foo = 0;let bar;let baz;
for (let i = 0, x = 1; i < arr.length; i++) {}
How to configure
Section titled How to configure{ "linter": { "rules": { "style": { "useSingleVarDeclarator": "error" } } }}