noShadow
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
¥Diagnostic Category:
lint/nursery/noShadow -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
¥How to configure
{ "linter": { "rules": { "nursery": { "noShadow": "error" } } }}¥Description
禁止变量声明覆盖外部作用域中声明的变量。
¥Disallow variable declarations from shadowing variables declared in the outer scope.
变量阴影是指局部变量与其所在作用域中的变量同名。这可能会导致阅读代码时产生困惑,并导致无法访问全局变量。
¥Shadowing is the process by which a local variable shares the same name as a variable in its containing scope. This can cause confusion while reading the code and make it impossible to access the global variable.
¥See also: noShadowRestrictedNames
¥Examples
¥Invalid
const foo = "bar";if (true) { const foo = "baz";}code-block.js:3:10 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This variable shadows another variable with the same name in the outer scope.
1 │ const foo = “bar”;
2 │ if (true) {
> 3 │ const foo = “baz”;
│ ^^^
4 │ }
5 │
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ const foo = “bar”;
│ ^^^
2 │ if (true) {
3 │ const foo = “baz”;
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
函数中的变量声明可能会覆盖外部作用域中的变量:
¥Variable declarations in functions can shadow variables in the outer scope:
const foo = "bar";const bar = function () { const foo = 10;}code-block.js:3:11 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This variable shadows another variable with the same name in the outer scope.
1 │ const foo = “bar”;
2 │ const bar = function () {
> 3 │ const foo = 10;
│ ^^^
4 │ }
5 │
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ const foo = “bar”;
│ ^^^
2 │ const bar = function () {
3 │ const foo = 10;
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
函数参数名称可能会覆盖外部作用域中的变量:
¥Function argument names can shadow variables in the outer scope:
const foo = "bar";function bar(foo) { foo = 10;}code-block.js:2:14 lint/nursery/noShadow ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This variable shadows another variable with the same name in the outer scope.
1 │ const foo = “bar”;
> 2 │ function bar(foo) {
│ ^^^
3 │ foo = 10;
4 │ }
ℹ This is the shadowed variable, which is now inaccessible in the inner scope.
> 1 │ const foo = “bar”;
│ ^^^
2 │ function bar(foo) {
3 │ foo = 10;
ℹ Consider renaming this variable. It’s easy to confuse the origin of variables if they share the same name.
¥Valid
const foo = "bar";if (true) { const qux = "baz";}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号