noInvalidUseBeforeDeclaration
诊断类别:lint/correctness/noInvalidUseBeforeDeclaration
¥Diagnostic Category: lint/correctness/noInvalidUseBeforeDeclaration
自从:v1.5.0
¥Since: v1.5.0
来源:
¥Sources:
-
与以下相同:
no-use-before-define
¥Same as:
no-use-before-define
-
与以下相同:
@typescript-eslint/no-use-before-define
¥Same as:
@typescript-eslint/no-use-before-define
禁止在声明之前使用变量和函数参数
¥Disallow the use of variables and function parameters before their declaration
JavaScript 不允许在声明之前使用块范围变量(let
、const
)和函数参数。在声明变量或参数之前,任何尝试访问该变量或参数都会抛出 ReferenceError
。
¥JavaScript doesn’t allow the use of block-scoped variables (let
, const
) and function parameters 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
code-block.js:3:11 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ Const declarations must have an initialized value.
1 │ function f() {
2 │ console.log(x);
> 3 │ const x;
│ ^
4 │ }
5 │
ℹ This variable needs to be initialized.
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 │
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 │
¥Valid
¥Related links