one-var

强制变量在函数中一起或单独声明

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

可以使用 varletconst 在 JavaScript 代码中的任何位置声明变量。有许多与变量声明相关的样式和偏好,其中之一就是决定在单个函数中应该允许多少个变量声明。

在这方面有两种思想流派:

  1. 函数中的所有变量都应该只有一个变量声明。该声明通常出现在函数的顶部。
  2. 您应该为要定义的每个变量使用一个变量声明。

例如:

// one variable declaration per function
function foo() {
    var bar, baz;
}

// multiple variable declarations per function
function foo() {
    var bar;
    var baz;
}

VZLcHbPPYuRCsohoIYEgXyEZYtOCND6m3tMA21jdcokKq2ER4s0XSZKCqD6zib4HgoxiSwioT2taPc4ZqICWPs3sWE40/R8s0ekYERBj4eBdgYg/DQwKZqkIru44ShbP0O/I/IC7/FL7aaZmbTOLwZ6JxtuqnI2fzRMXXDCQVq7jQxlE6hkg/vG//SfrDZ0mXEX+Ln+qxMsHKaCnchwkHBI3eCD0+65BpwzAtDukrxXs8Q10cM/dkQbmNcdEV++JWZGaXdjLX2uVdpmlkIx60tMK6hLkstEcOOqJJfYIN/CG98L2aFtp9CBY4j6h6nO+WXqLoO2RGiKXos1rWORTza+hGVLkIf3hqf07dlqbeVCcHyPzVxa9/fbER0qpXoXAh6Vehjci3UO81zSZlkMtsA==

规则详情

wp0JWBnKtdYwhvNmFPbjOvUjcy8+NEReektDZOzOqn/Vw8e30jbBxfbWITmFnkonoHmhweCwQcvIZwtBRbs4JaZOaRMnfKZS+X6VIW3owF8BfV6QSBdwlhW/l69v+S2kMK8kPWeCkzMphhxBWYZgFIaBzjSgjMxroasFr/y/RpBHBL84q1pVYuyQZRQnXHpO4HSK/+yfvqJ/lsh55iMfwg==

选项

i/QWu02im19/lo5J+rk5/bqKbtYRc/gUWXDy5VMj1O2Q4rN/XTMGxwcboa96uZhEHIeS+X1+9TAevRbtWS5Xnlgq1K5ZPtcybIRrwph64f8=

Pj+D0CO1BzCbtd4WG/uHBBdQ2g85Ey8Owh/LH8k9Mto=

    dsP7+2mA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RePYnCop2rcpUlddwFuut94sjtxg8MWEtyf8g8HlATKOPardkshq42lJQVfC6L3MoWozioerGQyUUYV/PGUVk6fqnW3d7kIidkN9tQ0kewfbSH9J6F00RDuKTspajxLIv9lUcMvPRsgz5reoQdf6yqTlTm4y8Dq7FsH4AFXI3wNEJu7X8dcrYBxzZ77Ka9jliIAgXBpViDBGjsMBvQsr8traTEFIWppGy48MiDM+wekudS3SZFtJENNJXR8YGKpos505MROdtwh1GFr9M3rE0F1KqQ0nnZ4+/qQVpdsUhjRr0KzjsDHWPpncuRa2WV0ADT41EvRIsaRfG8vy5E2qHgobGyJh1hPLweRVzgd2bwDLym6bq2ofdxiJELCLeHRKeVLuJjZnVKCfRviYpIbEsosDjPkCUmzd8pu0by1Ze

always

0UzW5qZvZRhpd4yfdcqJqaHg0WxfRMA5dazNQ6ftv+37B0PXoD17yHTJRjAVAHv44i9zE8uAFOIGZQlACmIzR+5vaCC326gsSewj+hQTnVU=

/*eslint one-var: ["error", "always"]*/

function foo() {
    var bar;
    var baz;
    let qux;
    let norf;
}

function foo(){
    const bar = false;
    const baz = true;
    let qux;
    let norf;
}

function foo() {
    var bar;

    if (baz) {
        var qux = true;
    }
}

class C {
    static {
        var foo;
        var bar;
    }

    static {
        var foo;
        if (bar) {
            var baz = true;
        }
    }

    static {
        let foo;
        let bar;
    }
}

0UzW5qZvZRhpd4yfdcqJqaHg0WxfRMA5dazNQ6ftv+37B0PXoD17yHTJRjAVAHv4lZcgw8F99TCqqqoIXAodGXoqcCJKx0b8VOeRP9DIeZA=

/*eslint one-var: ["error", "always"]*/

function foo() {
    var bar,
        baz;
    let qux,
        norf;
}

function foo(){
    const bar = true,
        baz = false;
    let qux,
        norf;
}

function foo() {
    var bar,
        qux;

    if (baz) {
        qux = true;
    }
}

function foo(){
    let bar;

    if (baz) {
        let qux;
    }
}

class C {
    static {
        var foo, bar;
    }

    static {
        var foo, baz;
        if (bar) {
            baz = true;
        }
    }

    static {
        let foo, bar;
    }

    static {
        let foo;
        if (bar) {
            let baz;
        }
    }
}

never

vLn68i7rFOv8nwd/Nio849hlSHRj4wJbKa8XHVQ2BS8LVn7/nY6/uOMFu+AIbVOczvlvnA97EgdibIokjQwlyA4MXwfAovJG/kTYpZxYxQw=

/*eslint one-var: ["error", "never"]*/

function foo() {
    var bar,
        baz;
    const bar = true,
        baz = false;
}

function foo() {
    var bar,
        qux;

    if (baz) {
        qux = true;
    }
}

function foo(){
    let bar = true,
        baz = false;
}

class C {
    static {
        var foo, bar;
        let baz, qux;
    }
}

vLn68i7rFOv8nwd/Nio849hlSHRj4wJbKa8XHVQ2BS+yCKYnZJrBOkWioZjKGPk/qyfWmMK4DyA6uOKv19yHTfWh9EOOKv0ZHij7BWjwiVM=

/*eslint one-var: ["error", "never"]*/

function foo() {
    var bar;
    var baz;
}

function foo() {
    var bar;

    if (baz) {
        var qux = true;
    }
}

function foo() {
    let bar;

    if (baz) {
        let qux = true;
    }
}

class C {
    static {
        var foo;
        var bar;
        let baz;
        let qux;
    }
}

// declarations with multiple variables are allowed in for-loop initializers
for (var i = 0, len = arr.length; i < len; i++) {
    doSomething(arr[i]);
}

consecutive

vLn68i7rFOv8nwd/Nio84xTgXhlufefR2DJYgHy1BOHqHjWqLVEP5bX3wLaUP6R6247AgNISJZ/uMzVUXNf3YJEti9YySq5h8OtLHpVbp50=

/*eslint one-var: ["error", "consecutive"]*/

function foo() {
    var bar;
    var baz;
}

function foo(){
    var bar = 1;
    var baz = 2;

    qux();

    var qux = 3;
    var quux;
}

class C {
    static {
        var foo;
        var bar;
        let baz;
        let qux;
    }
}

vLn68i7rFOv8nwd/Nio84xTgXhlufefR2DJYgHy1BOHqHjWqLVEP5bX3wLaUP6R6ryBMbim5yl6eHQViLk5HmlJEfSn0X7m8zNEf9Uz4wyQ=

/*eslint one-var: ["error", "consecutive"]*/

function foo() {
    var bar,
        baz;
}

function foo(){
    var bar = 1,
        baz = 2;

    qux();

    var qux = 3,
        quux;
}

class C {
    static {
        var foo, bar;
        let baz, qux;
        doSomething();
        let quux;
        var quuux;
    }
}

var、let、和 const

vLn68i7rFOv8nwd/Nio84+OBdrBkOBSrMNWoeEU5xso32aL2tzCbm0a6lBt52CLGyGJF5LLupQ2Krfqs7tGz8MEOTJkhzwItEjCVQznFJ558mgthStEP1yLI7akbL+ZgzR7owe1Szb6VrztMrnMHyg==

/*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/
/*eslint-env es6*/

function foo() {
    var bar;
    var baz;
    let qux,
        norf;
}

function foo() {
    const bar = 1,
          baz = 2;
    let qux,
        norf;
}

vLn68i7rFOv8nwd/Nio84+OBdrBkOBSrMNWoeEU5xso32aL2tzCbm0a6lBt52CLGyGJF5LLupQ2Krfqs7tGz8MEOTJkhzwItEjCVQznFJ559HdWX8+QxzfhaHokG31dF35L8qU9VEo2yb2MVYmSwUQ==

/*eslint one-var: ["error", { var: "always", let: "never", const: "never" }]*/
/*eslint-env es6*/

function foo() {
    var bar,
        baz;
    let qux;
    let norf;
}

function foo() {
    const bar = 1;
    const baz = 2;
    let qux;
    let norf;
}

vLn68i7rFOv8nwd/Nio8433g5OJmce4MUUrgcMZCTHwB+bAmGyYYtAPDbt8hv/jommyQwglKLrrQjrL+K12phRkCHglLTirJHDBlHP7Gab8=

/*eslint one-var: ["error", { var: "never" }]*/
/*eslint-env es6*/

function foo() {
    var bar,
        baz;
}

vLn68i7rFOv8nwd/Nio8433g5OJmce4MUUrgcMZCTHwB+bAmGyYYtAPDbt8hv/joTPG68LhrdRLyAgeCiMOONWfnWZ5mWR8FNeZHahVsDbE=

/*eslint one-var: ["error", { var: "never" }]*/
/*eslint-env es6*/

function foo() {
    var bar,
        baz;
    const bar = 1; // `const` and `let` declarations are ignored if they are not specified
    const baz = 2;
    let qux;
    let norf;
}

vLn68i7rFOv8nwd/Nio842QD8pP42Ds4l3SZarXHsCNYvHl3WFtSYHs82rZ0PL/DlEkNmb2oUa/0bJ9qrNYs2olE7DatvjtnF0waKRAwF8GN3xjm+BQAQf8lx4dMC7Pi

/*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/
/*eslint-env node*/

var foo = require("foo"),
    bar = "bar";

vLn68i7rFOv8nwd/Nio842QD8pP42Ds4l3SZarXHsCNYvHl3WFtSYHs82rZ0PL/DlEkNmb2oUa/0bJ9qrNYs2k5rWfbo+CXY0v32bM/FoZPpCmJHDnLJpudvAQYdsFGV

/*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/
/*eslint-env node*/

var foo = require("foo");
var bar = "bar";
var foo = require("foo"),
    bar = require("bar");

vLn68i7rFOv8nwd/Nio8433g5OJmce4MUUrgcMZCTHzCQpQTLUKc67HGuikcKvxzAyRanhsYa8MUIupA6expNdLZmkMlCPLzlzBOW1BBRhN5O/U0gHlBKvKisDgq0TRqX9fviDu+uyxIC8zH9+VQihjVtUl4n5RD4/cOnwQig7s=

/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/
/*eslint-env es6*/

function foo() {
    let a,
        b;
    let c;

    var d,
        e;
}

function foo() {
    const a = 1,
        b = 2;
    const c = 3;

    var d,
        e;
}

vLn68i7rFOv8nwd/Nio8433g5OJmce4MUUrgcMZCTHzCQpQTLUKc67HGuikcKvxzAyRanhsYa8MUIupA6expNdLZmkMlCPLzlzBOW1BBRhN5O/U0gHlBKvKisDgq0TRq2bgRgXbE7nCFKWGivLM6i9JbpduznsrDrhj+TfeR4WM=

/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive" }]*/
/*eslint-env es6*/

function foo() {
    let a,
        b;

    var d;
    var e;

    let f;
}

function foo() {
    const a = 1,
          b = 2;

    var c;
    var d;

    const e = 3;
}

vLn68i7rFOv8nwd/Nio847yK93Y3QsC2GBMAc9h/3qtZajR4iqASkxtm8kdcnlLgw4CzF3e24hLIQ0QPA5PY1E/FF81B197v7J9XUvgDcmm8TEuVUyBplNE+JaVHUxOJ

/*eslint one-var: ["error", { var: "consecutive" }]*/
/*eslint-env es6*/

function foo() {
    var a;
    var b;
}

vLn68i7rFOv8nwd/Nio847yK93Y3QsC2GBMAc9h/3qtZajR4iqASkxtm8kdcnlLgmCqV8OWjevG6hm8f77RhAyIH6M4u6omFu08fglHGby9JNZ/dnbznroDjzR7O8Rr7

/*eslint one-var: ["error", { var: "consecutive" }]*/
/*eslint-env es6*/

function foo() {
    var a,
        b;
    const c = 1; // `const` and `let` declarations are ignored if they are not specified
    const d = 2;
    let e;
    let f;
}

initialized 和 uninitialized

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3oJ/n04Bmlkn6fLOrGMO0qJLjphWjNUDXUoeqdh8hAUesn9mQKMqh/qBJQAWwNkIdaWVO5HzGsJSnIRuT8OCjcsiR+qWN69l4kXAw425T4Kdvi/WGsKdKR3L43ShH5Dgog=

/*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/
/*eslint-env es6*/

function foo() {
    var a, b, c;
    var foo = true;
    var bar = false;
}

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3oJ/n04Bmlkn6fLOrGMO0qJLjphWjNUDXUoeqdh8hAUesn9mQKMqh/qBJQAWwNkIdaGFlEyFSElzR0JdbyrIVLvGoQ/3eZhWPkfZlyHD1bu8c0H1/bwfvcQli7073E5Hcc=

/*eslint one-var: ["error", { "initialized": "always", "uninitialized": "never" }]*/

function foo() {
    var a;
    var b;
    var c;
    var foo = true,
        bar = false;
}

for (let z of foo) {
    doSomething(z);
}

let z;
for (z of foo) {
    doSomething(z);
}

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3q51j4rzb5tjrXKQwT2h79DvyzjBDvbTw4b+/n0jGFcfhK96r2XWErfcjSAofsL9boBLbMOEpkjCeMbTM6EDp+b

/*eslint one-var: ["error", { "initialized": "never" }]*/
/*eslint-env es6*/

function foo() {
    var foo = true,
        bar = false;
}

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3q51j4rzb5tjrXKQwT2h79DvyzjBDvbTw4b+/n0jGFcfsxHZW2CqjEd2IuLrHDRYYbhR0cuFvp4A89bZcLAcvoz

/*eslint one-var: ["error", { "initialized": "never" }]*/

function foo() {
    var foo = true;
    var bar = false;
    var a, b, c; // Uninitialized variables are ignored
}

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3psfdLuXy94MvjTCzDOAgRajuNVjhfUoYdlUAcb5H57sRteT1ofxN9xMV94PhMdd6uN/Wn4dYu88IqKdWeA1UrvSmzfqgoXa596XiBMWGNdI7thHzPjEBr7mGqVa6Qm75g=

/*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/

function foo() {
    var a = 1;
    var b = 2;
    var c,
        d;
    var e = 3;
    var f = 4;
}

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3psfdLuXy94MvjTCzDOAgRajuNVjhfUoYdlUAcb5H57sRteT1ofxN9xMV94PhMdd6uN/Wn4dYu88IqKdWeA1UrvuKdiKKjFp2ufj5/buzntaJpByxS9RzCc1LppB9hQ5lU=

/*eslint one-var: ["error", { "initialized": "consecutive", "uninitialized": "never" }]*/

function foo() {
    var a = 1,
        b = 2;
    var c;
    var d;
    var e = 3,
        f = 4;
}

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3psfdLuXy94MvjTCzDOAgRagY9cn6CIpb+jsQd2OWBS4e3136l7+DcOS5yCsfbMBVPif2c5Y/6Kxd2/MYNFcv+n

/*eslint one-var: ["error", { "initialized": "consecutive" }]*/

function foo() {
    var a = 1;
    var b = 2;

    foo();

    var c = 3;
    var d = 4;
}

vLn68i7rFOv8nwd/Nio84xQgjhmdXsTuDCVdFnOUo3psfdLuXy94MvjTCzDOAgRagY9cn6CIpb+jsQd2OWBS4aZ5bb9/mDKGioeOMsNgUEeYUjdr06Mfma84odiUCTfS

/*eslint one-var: ["error", { "initialized": "consecutive" }]*/

function foo() {
    var a = 1,
        b = 2;

    foo();

    var c = 3,
        d = 4;
}

兼容性

    eRA1m6FiUV8RzZuYLRYI5TZ7gsOIEuxpCu7AhCgV87f6KeYK+C3VvN9B46RX8MkYCeKu+fy26a0ZGpNjRHUrRx5lrL6J9DIrjECT2jt+wJvmmHPlXKZ2FGJaQae2eZw4x+yXVM6/fXbDJIyFSVDXsWs0L6ULGYwPAVG6uDSiQiZ2nLecY3ID37yogaE60zL371IOepfJGM5zCWP+ypoI4hu/29bsGyi4FFdn7xFROLZ3zju//+vHRqQW1XkFDzj26PckttjX1801gD5nspX9LdpCrZiRweLfdL7a53doo6YhudRDZNyN/tt0VJ61xWPPDXzHNPFdtKC2VhCiOHxyURZKqK2qaPPPu0a4LpdA4O56BgmFsK/Xk6xHFbKteXWxXob4fljk9tK+lZ50R6D3Ay5n5cdHoQgqSNUb0lhXlkjwJznlP45YsmOHP6zsn2pcYnaTyxgmGPSbrvmcvViYQcjAj6ayYJpHIuXab65/raO2ZYd8YIFmjiKEfVgOJ41cs6MajLoaozTxmnmNoiGavACBQalxK+xqt5esd2g+GUdKBq2yuEMUm9mfGWcVG5Nb0JVNqUba8RbbcpIvC4+j0e7rFCyArbsP1iMy289twlg=