useConst
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/style/useConst¥Diagnostic Category:
lint/style/useConst -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
与
prefer-const相同¥Same as
prefer-const
-
¥How to configure
{ "linter": { "rules": { "style": { "useConst": "error" } } }}¥Description
要求仅分配一次的变量进行 const 声明。
¥Require const declarations for variables that are only assigned once.
已初始化且从未重新分配的变量和仅分配一次的变量可以声明为 const。
¥Variables that are initialized and never reassigned and
variables that are only assigned once can be declared as const.
¥Examples
¥Invalid
let a = 3;console.log(a);code-block.js:1:1 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This let declares a variable that is only assigned once.
> 1 │ let a = 3;
│ ^^^
2 │ console.log(a);
3 │
ℹ ‘a’ is never reassigned.
> 1 │ let a = 3;
│ ^
2 │ console.log(a);
3 │
ℹ Safe fix: Use const instead.
1 │ - let·a·=·3;
1 │ + const·a·=·3;
2 2 │ console.log(a);
3 3 │
// `a` is redefined (not reassigned) on each loop step.for (let a of [1, 2, 3]) { console.log(a);}code-block.js:2:6 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This let declares a variable that is only assigned once.
1 │ // `a` is redefined (not reassigned) on each loop step.
> 2 │ for (let a of [1, 2, 3]) {
│ ^^^
3 │ console.log(a);
4 │ }
ℹ ‘a’ is never reassigned.
1 │ // `a` is redefined (not reassigned) on each loop step.
> 2 │ for (let a of [1, 2, 3]) {
│ ^
3 │ console.log(a);
4 │ }
ℹ Safe fix: Use const instead.
1 1 │ // `a` is redefined (not reassigned) on each loop step.
2 │ - for·(let·a·of·[1,·2,·3])·{
2 │ + for·(const·a·of·[1,·2,·3])·{
3 3 │ console.log(a);
4 4 │ }
// `a` is redefined (not reassigned) on each loop step.for (let a in [1, 2, 3]) { console.log(a);}code-block.js:2:6 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This let declares a variable that is only assigned once.
1 │ // `a` is redefined (not reassigned) on each loop step.
> 2 │ for (let a in [1, 2, 3]) {
│ ^^^
3 │ console.log(a);
4 │ }
ℹ ‘a’ is never reassigned.
1 │ // `a` is redefined (not reassigned) on each loop step.
> 2 │ for (let a in [1, 2, 3]) {
│ ^
3 │ console.log(a);
4 │ }
ℹ Safe fix: Use const instead.
1 1 │ // `a` is redefined (not reassigned) on each loop step.
2 │ - for·(let·a·in·[1,·2,·3])·{
2 │ + for·(const·a·in·[1,·2,·3])·{
3 3 │ console.log(a);
4 4 │ }
let a;a = 0;code-block.js:1:1 lint/style/useConst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This let declares a variable that is only assigned once.
> 1 │ let a;
│ ^^^
2 │ a = 0;
3 │
ℹ ‘a’ is only assigned here.
1 │ let a;
> 2 │ a = 0;
│ ^
3 │
let a = 3;{ let a = 4; a = 2;}code-block.js:1:1 lint/style/useConst FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This let declares a variable that is only assigned once.
> 1 │ let a = 3;
│ ^^^
2 │ {
3 │ let a = 4;
ℹ ‘a’ is never reassigned.
> 1 │ let a = 3;
│ ^
2 │ {
3 │ let a = 4;
ℹ Safe fix: Use const instead.
1 │ - let·a·=·3;
1 │ + const·a·=·3;
2 2 │ {
3 3 │ let a = 4;
¥Valid
let a = 2;a = 3;console.log(a);let a = 1, b = 2;b = 3;let a;a; // the variable is read before its assignmenta = 0;¥Caveats
自 v2.2 版本起,该规则不再报告在内部函数中读取后再写入的变量。这可能会导致误报。例如,以下代码现在有效:
¥Since v2.2, the rule no longer reports variables that are read in an inner function before being written. This can result in false negatives. For example, the following code is now valid:
let a;function f() { return a; // read}a = 0; // written¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号