版本

prefer-const

要求const声明从未重新赋值的变量

🔧 可修复

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

如果变量从未重新赋值,则使用const声明更好。

const声明告诉读者,“此变量从未重新赋值”,从而减少认知负担并提高可维护性。

规则详细信息

此规则旨在标记使用let关键字声明但从未在初始赋值后重新赋值的变量。

此规则的错误代码示例

在游乐场中打开
/*eslint prefer-const: "error"*/

// it's initialized and never reassigned.
let a = 3;
console.log(a);

let b;
b = 0;
console.log(b);

class C {
    static {
        let a;
        a = 0;
        console.log(a);
    }
}

// `i` is redefined (not reassigned) on each loop step.
for (let i in [1, 2, 3]) {
    console.log(i);
}

// `a` is redefined (not reassigned) on each loop step.
for (let a of [1, 2, 3]) {
    console.log(a);
}

此规则的正确代码示例

在游乐场中打开
/*eslint prefer-const: "error"*/

// using const.
const a = 0;

// it's never initialized.
let b;
console.log(b);

// it's reassigned after initialized.
let c;
c = 0;
c = 1;
console.log(c);

// it's initialized in a different block from the declaration.
let d;
if (true) {
    d = 0;
}
console.log(d);

// it's initialized in a different scope.
let e;
class C {
    #x;
    static {
        e = obj => obj.#x;
    }
}

// it's initialized at a place that we cannot write a variable declaration.
let f;
if (true) f = 0;
console.log(f);

// `i` gets a new binding each iteration
for (const i in [1, 2, 3]) {
  console.log(i);
}

// `a` gets a new binding each iteration
for (const a of [1, 2, 3]) {
  console.log(a);
}

// `end` is never reassigned, but we cannot separate the declarations without modifying the scope.
for (let i = 0, end = 10; i < end; ++i) {
    console.log(i);
}

// `predicate` is only assigned once but cannot be separately declared as `const`
let predicate;
[object.type, predicate] = foo();

// `g` is only assigned once but cannot be separately declared as `const`
let g;
const h = {};
({ g, c: h.c } = func());

// suggest to use `no-var` rule.
var i = 3;
console.log(i);

选项

{
    "prefer-const": ["error", {
        "destructuring": "any",
        "ignoreReadBeforeAssign": false
    }]
}

解构

处理解构中变量的方式。有两个值

  • "any"(默认值) - 如果解构中的任何变量都应为const,则此规则会警告这些变量。
  • "all" - 如果解构中的所有变量都应为const,则此规则会警告变量。否则,忽略它们。

默认{"destructuring": "any"}选项的错误代码示例

在游乐场中打开
/*eslint prefer-const: "error"*/

let {a, b} = obj;    /*error 'b' is never reassigned, use 'const' instead.*/
a = a + 1;

默认{"destructuring": "any"}选项的正确代码示例

在游乐场中打开
/*eslint prefer-const: "error"*/

// using const.
const {a: a0, b} = obj;
const a = a0 + 1;

// all variables are reassigned.
let {c, d} = obj;
c = c + 1;
d = d + 1;

{"destructuring": "all"}选项的错误代码示例

在游乐场中打开
/*eslint prefer-const: ["error", {"destructuring": "all"}]*/

// all of `a` and `b` should be const, so those are warned.
let {a, b} = obj;    /*error 'a' is never reassigned, use 'const' instead.
                             'b' is never reassigned, use 'const' instead.*/

{"destructuring": "all"}选项的正确代码示例

在游乐场中打开
/*eslint prefer-const: ["error", {"destructuring": "all"}]*/

// 'b' is never reassigned, but all of `a` and `b` should not be const, so those are ignored.
let {a, b} = obj;
a = a + 1;

ignoreReadBeforeAssign

这是一个选项,用于避免与no-use-before-define规则(无"nofunc"选项)冲突。如果指定了true,则此规则将忽略在声明和第一次赋值之间读取的变量。默认为false

{"ignoreReadBeforeAssign": true}选项的正确代码示例

在游乐场中打开
/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": true}]*/

let timer;
function initialize() {
    if (foo()) {
        clearInterval(timer);
    }
}
timer = setInterval(initialize, 100);

默认{"ignoreReadBeforeAssign": false}选项的正确代码示例

在游乐场中打开
/*eslint prefer-const: ["error", {"ignoreReadBeforeAssign": false}]*/

const timer = setInterval(initialize, 100);
function initialize() {
    if (foo()) {
        clearInterval(timer);
    }
}

何时不使用它

如果您不想收到有关在初始赋值后从未重新赋值的变量的通知,可以安全地禁用此规则。

版本

此规则在 ESLint v0.23.0 中引入。

资源

更改语言