# one-var

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

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

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

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

例如:

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

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

单一声明学派基于 ECMAScript 6 之前的行为,其中没有块作用域,只有函数作用域。由于无论如何所有 var 语句都被提升到函数的顶部,一些人认为在函数顶部的单个声明中声明所有变量可以消除范围规则的混乱。

# 规则详情

此规则强制每个函数(对于 var)或块(对于 letconst)作用域一起或单独声明变量。

# 选项

此规则有一个选项,可以是字符串选项或对象选项。

字符串选项:

  • "always"(默认)每个范围需要一个变量声明
  • "never" 每个范围需要多个变量声明
  • "consecutive" 允许每个作用域有多个变量声明,但需要将连续的变量声明组合成一个声明

对象选项:

  • "var": "always" 每个函数需要一个 var 声明
  • "var": "never" 每个函数需要多个 var 声明
  • "var": "consecutive" 要求连续的 var 声明为单个声明
  • "let": "always" 要求每个块有一个 let 声明
  • "let": "never" 要求每个块有多个 let 声明
  • "let": "consecutive" 要求连续的 let 声明为单个声明
  • "const": "always" 要求每个块有一个 const 声明
  • "const": "never" 要求每个块有多个 const 声明
  • "const": "consecutive" 要求连续的 const 声明为单个声明
  • "separateRequires": true 强制 requires 与声明分开

替代对象选项:

  • "initialized": "always" 需要为每个范围的初始化变量声明一个变量
  • "initialized": "never" 需要为每个作用域的初始化变量声明多个变量
  • "initialized": "consecutive" 要求初始化变量的连续变量声明为单个声明
  • "uninitialized": "always" 需要为每个范围内的未初始化变量声明一个变量
  • "uninitialized": "never" 需要为每个作用域的未初始化变量声明多个变量
  • "uninitialized": "consecutive" 要求未初始化变量的连续变量声明为单个声明

# always

此规则使用默认 "always" 选项的错误代码示例:

/*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;
    }
}

此规则使用默认 "always" 选项的正确代码示例:

/*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

此规则使用 "never" 选项的错误代码示例:

/*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;
    }
}

此规则使用 "never" 选项的正确代码示例:

/*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

此规则使用 "consecutive" 选项的错误代码示例:

/*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;
    }
}

此规则使用 "consecutive" 选项的正确代码示例:

/*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

此规则使用 { var: "always", let: "never", const: "never" } 选项的错误代码示例:

/*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;
}

此规则使用 { var: "always", let: "never", const: "never" } 选项的正确代码示例:

/*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;
}

此规则使用 { var: "never" } 选项的错误代码示例:

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

function foo() {
    var bar,
        baz;
}

此规则使用 { var: "never" } 选项的正确代码示例:

/*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;
}

此规则使用 { separateRequires: true } 选项的错误代码示例:

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

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

此规则使用 { separateRequires: true } 选项的正确代码示例:

/*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");

此规则使用 { var: "never", let: "consecutive", const: "consecutive" } 选项的错误代码示例:

/*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;
}

此规则使用 { var: "never", let: "consecutive", const: "consecutive" } 选项的正确代码示例:

/*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;
}

此规则使用 { var: "consecutive" } 选项的错误代码示例:

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

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

此规则使用 { var: "consecutive" } 选项的正确代码示例:

/*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

此规则使用 { "initialized": "always", "uninitialized": "never" } 选项的错误代码示例:

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

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

此规则使用 { "initialized": "always", "uninitialized": "never" } 选项的正确代码示例:

/*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);
}

此规则使用 { "initialized": "never" } 选项的错误代码示例:

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

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

此规则使用 { "initialized": "never" } 选项的正确代码示例:

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

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

此规则使用 { "initialized": "consecutive", "uninitialized": "never" } 选项的错误代码示例:

/*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;
}

此规则使用 { "initialized": "consecutive", "uninitialized": "never" } 选项的正确代码示例:

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

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

此规则使用 { "initialized": "consecutive" } 选项的错误代码示例:

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

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

    foo();

    var c = 3;
    var d = 4;
}

此规则使用 { "initialized": "consecutive" } 选项的正确代码示例:

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

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

    foo();

    var c = 3,
        d = 4;
}

# 兼容性

  • JSHint:此规则映射到 onevar JSHint 规则,但允许单独配置 letconst
  • JSCS:这条规则大致映射到 disallowMultipleVarDecl
  • JSCS:此规则选项 separateRequires 大致映射到 requireMultipleVarDecl
Last Updated: 5/13/2023, 8:55:38 PM