noInvalidUseBeforeDeclaration
¥Summary
-
规则生效日期:
v1.5.0¥Rule available since:
v1.5.0 -
诊断类别:
lint/correctness/noInvalidUseBeforeDeclaration¥Diagnostic Category:
lint/correctness/noInvalidUseBeforeDeclaration -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-use-before-define相同¥Same as
no-use-before-define
-
¥How to configure
{ "linter": { "rules": { "correctness": { "noInvalidUseBeforeDeclaration": "error" } } }}¥Description
禁止在声明之前使用变量、函数参数、类和枚举。
¥Disallow the use of variables, function parameters, classes, and enums before their declaration
JavaScript 不允许在声明之前使用块级作用域变量(let、const)、函数参数和类。同样,TypeScript 不允许在枚举声明之前使用枚举。在声明变量或参数之前,任何尝试访问该变量或参数都会抛出 ReferenceError。
¥JavaScript doesn’t allow the use of block-scoped variables (let, const), function parameters, and classes before their declaration.
Similarly TypeScript doesn’t allow the use of enums before their declaration.
A ReferenceError will be thrown with any attempt to access the variable or the parameter before its declaration.
该规则还报告在声明之前使用 var 声明的变量的使用情况。
¥The rule also reports the use of variables declared with var before their declarations.
¥Examples
¥Invalid
function f() { console.log(x); let x;}code-block.js:2:17 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This variable is used before its declaration.
1 │ function f() {
> 2 │ console.log(x);
│ ^
3 │ let x;
4 │ }
ℹ The variable is declared here:
1 │ function f() {
2 │ console.log(x);
> 3 │ let x;
│ ^
4 │ }
5 │
function f() { console.log(x); var x = 0;}code-block.js:2:17 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This variable is used before its declaration.
1 │ function f() {
> 2 │ console.log(x);
│ ^
3 │ var x = 0;
4 │ }
ℹ The variable is declared here:
1 │ function f() {
2 │ console.log(x);
> 3 │ var x = 0;
│ ^
4 │ }
5 │
function f(a = b, b = 0) {}code-block.js:1:16 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This parameter is used before its declaration.
> 1 │ function f(a = b, b = 0) {}
│ ^
2 │
ℹ The parameter is declared here:
> 1 │ function f(a = b, b = 0) {}
│ ^
2 │
new C();class C {}code-block.js:1:5 lint/correctness/noInvalidUseBeforeDeclaration ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This class is used before its declaration.
> 1 │ new C();
│ ^
2 │ class C {}
3 │
ℹ The class is declared here:
1 │ new C();
> 2 │ class C {}
│ ^
3 │
¥Valid
f();function f() {}// An export can reference a variable before its declaration.export { CONSTANT };const CONSTANT = 0;function f() { return CONSTANT; }const CONSTANT = 0;function f() { new C();}let c: C;class C {}¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号