noUselessUndefinedInitialization
¥Summary
-
规则生效日期:
v1.7.2¥Rule available since:
v1.7.2 -
诊断类别:
lint/complexity/noUselessUndefinedInitialization¥Diagnostic Category:
lint/complexity/noUselessUndefinedInitialization -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
no-undef-init相同¥Same as
no-undef-init
-
¥How to configure
{ "linter": { "rules": { "complexity": { "noUselessUndefinedInitialization": "error" } } }}¥Description
禁止将变量初始化为 undefined。
¥Disallow initializing variables to undefined.
声明但未初始化为任何值的变量会自动获得 undefined 的值。避免将变量初始化为 undefined 被认为是最佳实践。
¥A variable that is declared and not initialized to any value automatically gets the value of undefined.
It’s considered a best practice to avoid initializing variables to undefined.
请注意,在自动修复时,附加到初始化值或变量的任何内联注释都将移动到变量声明的末尾。请注意,这与 Eslint 的行为不同。
¥Please note that any inline comments attached to the initialization value or variable will be moved at the end of the variable declaration on auto-fix. Please be also aware that this differs from Eslint’s behaviour.
¥Examples
¥Invalid
var a = undefined;code-block.js:1:7 lint/complexity/noUselessUndefinedInitialization FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━
ℹ It’s not necessary to initialize a to undefined.
> 1 │ var a = undefined;
│ ^^^^^^^^^^^
2 │
ℹ A variable that is declared and not initialized to any value automatically gets the value of undefined.
ℹ Safe fix: Remove undefined initialization.
1 │ var·a·=·undefined;
│ -----------
let b = undefined, c = 1, d = 2;code-block.js:1:7 lint/complexity/noUselessUndefinedInitialization FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━
ℹ It’s not necessary to initialize b to undefined.
> 1 │ let b = undefined, c = 1, d = 2;
│ ^^^^^^^^^^^
2 │
ℹ A variable that is declared and not initialized to any value automatically gets the value of undefined.
ℹ Safe fix: Remove undefined initialization.
1 │ let·b·=·undefined,·c·=·1,·d·=·2;
│ -----------
for (let i = 0; i < 100; i++) { let i = undefined;}code-block.js:2:8 lint/complexity/noUselessUndefinedInitialization FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━
ℹ It’s not necessary to initialize i to undefined.
1 │ for (let i = 0; i < 100; i++) {
> 2 │ let i = undefined;
│ ^^^^^^^^^^^
3 │ }
4 │
ℹ A variable that is declared and not initialized to any value automatically gets the value of undefined.
ℹ Safe fix: Remove undefined initialization.
2 │ → let·i·=·undefined;
│ -----------
let f = /**/undefined/**/ ;code-block.js:1:7 lint/complexity/noUselessUndefinedInitialization FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━
ℹ It’s not necessary to initialize f to undefined.
> 1 │ let f = /**/undefined/**/ ;
│ ^^^^^^^^^^^^^^^
2 │
ℹ A variable that is declared and not initialized to any value automatically gets the value of undefined.
ℹ Safe fix: Remove undefined initialization.
1 │ - let·f·=·/**/undefined/**/·;
1 │ + let·f·;/**//**/
2 2 │
¥Valid
var a = 1;class Foo { bar = undefined;}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号