Index

one-var

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

🔧 Fixable

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

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

在 JavaScript 代码中,可以在任何位置使用 varletconstusingawait using 声明变量。关于变量声明有许多风格和偏好,其中之一是决定一个函数中应该允许多少个变量声明。

🌐 Variables can be declared at any point in JavaScript code using var, let, const, using, or await using. There are many styles and preferences related to the declaration of variables, and one of those is deciding on how many variable declarations should be allowed in a single function.

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

🌐 There are two schools of thought in this regard:

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

例如:

🌐 For instance:

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

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

单一声明学派的思想基于 ECMAScript 6 之前的行为,那时不存在块级作用域,只有函数作用域。由于所有 var 语句无论如何都会被提升到函数的顶部,一些人认为在函数顶部使用单一声明声明所有变量可以消除关于作用域规则的困惑。

🌐 The single-declaration school of thought is based in pre-ECMAScript 6 behaviors, where there was no such thing as block scope, only function scope. Since all var statements are hoisted to the top of the function anyway, some believe that declaring all variables in a single declaration at the top of the function removes confusion around scoping rules.

规则详情

🌐 Rule Details

此规则强制变量在每个函数(针对 var)或块(针对 letconstusingawait using)作用域中要么一起声明,要么单独声明。

🌐 This rule enforces variables to be declared either together or separately per function ( for var) or block (for let, const, using, and await using) scope.

选项

🌐 Options

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

🌐 This rule has one option, which can be a string option or an object option.

字符串选项:

🌐 String option:

  • "always"(默认)要求每个作用域只能有一个变量声明
  • "never" 每个作用域需要多个变量声明
  • "consecutive"允许每个作用域中有多个变量声明,但要求连续的变量声明合并为单个声明

对象选项:

🌐 Object option:

  • "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 声明必须合并为单个声明
  • "using": "always" 每个代码块需要一个 using 声明
  • "using": "never" 每个代码块需要多个 using 声明
  • "using": "consecutive" 要求连续的 using 声明必须合并为单个声明
  • "awaitUsing": "always" 每个代码块需要一个 await using 声明
  • "awaitUsing": "never" 每个块需要多个 await using 声明
  • "awaitUsing": "consecutive" 要求连续的 await using 声明必须合并为单个声明
  • "separateRequires": true 强制 requires 与声明分开

替代对象选项:

🌐 Alternate object option:

  • "initialized": "always" 每个作用域要求为已初始化的变量声明一次
  • "initialized": "never" 每个作用域内需要对已初始化的变量进行多次声明
  • "initialized": "consecutive" 要求已初始化的变量的连续声明必须为单一声明
  • "uninitialized": "always" 每个作用域只允许一个未初始化变量的声明
  • "uninitialized": "never" 需要在每个作用域中对未初始化的变量进行多次声明
  • "uninitialized": "consecutive" 要求未初始化变量的连续声明必须为单个声明

always

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

🌐 Examples of incorrect code for this rule with the default "always" option:

在线运行
/*eslint one-var: ["error", "always"]*/

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

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

function foo3() {
    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" 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default "always" option:

在线运行
/*eslint one-var: ["error", "always"]*/

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

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

function foo3() {
    var bar,
        qux;

    if (baz) {
        qux = true;
    }
}

function foo4(){
    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" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint one-var: ["error", "never"]*/

function foo1() {
    var bar,
        baz;
    const qux = true,
        foobar = false;
}

function foo2() {
    var bar,
        qux;

    if (baz) {
        qux = true;
    }
}

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

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

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

🌐 Examples of correct code for this rule with the "never" option:

在线运行
/*eslint one-var: ["error", "never"]*/

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

function foo2() {
    var bar;

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

function foo3() {
    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" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "consecutive" option:

在线运行
/*eslint one-var: ["error", "consecutive"]*/

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

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

    qux();

    var qux = 3;
    var quux;
}

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

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

🌐 Examples of correct code for this rule with the "consecutive" option:

在线运行
/*eslint one-var: ["error", "consecutive"]*/

function foo1() {
    var bar,
        baz;
}

function foo2(){
    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、using 和 awaitUsing

🌐 var, let, const, using, and awaitUsing

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

🌐 Examples of incorrect code for this rule with the { var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" } option:

在线运行
/*eslint one-var: ["error", { var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" }]*/

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

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

async function foo3() {
    using bar = 1,
          baz = 2;
    await using qux = 3,
                norf = 4;
}

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

🌐 Examples of correct code for this rule with the { var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" } option:

在线运行
/*eslint one-var: ["error", { var: "always", let: "never", const: "never", using: "never", awaitUsing: "never" }]*/

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

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

async function foo3() {
    using bar = 1;
    using baz = 2;
    await using qux = 3;
    await using norf = 4;
}

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

🌐 Examples of incorrect code for this rule with the { var: "never" } option:

在线运行
/*eslint one-var: ["error", { var: "never" }]*/

function foo() {
    var bar,
        baz;
}

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

🌐 Examples of correct code for this rule with the { var: "never" } option:

在线运行
/*eslint one-var: ["error", { var: "never" }]*/

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

    // `const`, `let`, `using` and `await using` declarations are ignored if they are not specified
    const foobar = 1;
    const foobaz = 2;
    const barfoo = 1, bazfoo = 2;
    let qux;
    let norf;
    let fooqux, foonorf;
    using foobarfoo = 1;
    using foobazfoo = 2;
    using bazbarfoo = 1, bazfoobar = 2;
    await using foobarbaz = 1;
    await using foobazqux = 2;
    await using bazbarqux = 1, bazfooqux = 2;
}

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

🌐 Examples of incorrect code for this rule with the { separateRequires: true } option:

在线运行
/*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/

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

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

🌐 Examples of correct code for this rule with the { separateRequires: true } option:

在线运行
/*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/

var foo = require("foo");
var bar = "bar";
在线运行
/*eslint one-var: ["error", { separateRequires: true, var: "always" }]*/

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

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

🌐 Examples of incorrect code for this rule with the { var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" } option:

在线运行
/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" }]*/

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

    var d,
        e;
}

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

    var d,
        e;
}

function foo3() {
    using a = 1,
        b = 2;
    using c = 3;

    var d,
        e;
}

async function foo4() {
    await using a = 1,
        b = 2;
    await using c = 3;

    var d,
        e;
}

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

🌐 Examples of correct code for this rule with the { var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" } option:

在线运行
/*eslint one-var: ["error", { var: "never", let: "consecutive", const: "consecutive", using: "consecutive", awaitUsing: "consecutive" }]*/

function foo1() {
    let a,
        b;

    var d;
    var e;

    let f;
}

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

    var c;
    var d;

    const e = 3;
}

function foo3() {
    using a = 1,
          b = 2;

    var c;
    var d;

    using e = 3;
}

async function foo4() {
    await using a = 1,
          b = 2;

    var c;
    var d;

    await using e = 3;
}

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

🌐 Examples of incorrect code for this rule with the { var: "consecutive" } option:

在线运行
/*eslint one-var: ["error", { var: "consecutive" }]*/

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

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

🌐 Examples of correct code for this rule with the { var: "consecutive" } option:

在线运行
/*eslint one-var: ["error", { var: "consecutive" }]*/

async function foo() {
    var a,
        b;
 
    // `const`, `let`, `using`, and `await using` declarations are ignored if they are not specified
    const c = 1;
    const d = 2;
    let e;
    let f;
    using g = 3;
    using h = 4;
    await using i = 5;
    await using j = 6;
}

initialized 和 uninitialized

🌐 initialized and uninitialized

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

🌐 Examples of incorrect code for this rule with the { "initialized": "always", "uninitialized": "never" } option:

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

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

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

🌐 Examples of correct code for this rule with the { "initialized": "always", "uninitialized": "never" } option:

在线运行
/*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" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "initialized": "never" } option:

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

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

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

🌐 Examples of correct code for this rule with the { "initialized": "never" } option:

在线运行
/*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" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "initialized": "consecutive", "uninitialized": "never" } option:

在线运行
/*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" } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "initialized": "consecutive", "uninitialized": "never" } option:

在线运行
/*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" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "initialized": "consecutive" } option:

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

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

    foo();

    var c = 3;
    var d = 4;
}

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

🌐 Examples of correct code for this rule with the { "initialized": "consecutive" } option:

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

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

    foo();

    var c = 3,
        d = 4;
}

兼容性

🌐 Compatibility

  • JSHint:此规则对应 onevar JSHint 规则,但允许 letconst 分别进行配置。
  • JSCS: 这条规则大致对应 disallowMultipleVarDecl
  • JSCS:此规则选项 separateRequires 大致对应于 requireMultipleVarDecl

版本

此规则是在 ESLint v0.0.9 中引入。

资源