useVarsOnTop
此内容尚不支持你的语言。
Summary
Section titled “Summary”- Rule available since:
v2.4.12 - Diagnostic Category:
lint/nursery/useVarsOnTop - This rule doesn’t have a fix.
- The default severity of this rule is warning.
- Sources:
- Same as
vars-on-top
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useVarsOnTop": "error" } } }}Description
Section titled “Description”Require var declarations to appear at the top of their containing scope.
Because var declarations are hoisted to the top of the nearest function,
script, module, or static block, placing them later in the body makes code
harder to follow. Keeping them at the top makes the scope’s variable
declarations easier to find. Note that this is not a problem for let and
const declarations, which are block-scoped and not hoisted.
This rule only allows leading standalone var statements. At module
scope, leading export var declarations are allowed too. Directives and
imports may appear before them.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function f() { doSomething(); var value = 1;}code-block.js:3:5 lint/nursery/useVarsOnTop ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This var declaration is not at the top of its containing scope.
1 │ function f() {
2 │ doSomething();
> 3 │ var value = 1;
│ ^^^^^^^^^^^^^
4 │ }
5 │
ℹ var declarations are hoisted to the top of their enclosing function, script, module, or static block, so declaring them later makes the scope harder to read.
ℹ Move this var declaration before other statements in the same scope. At module scope, imports may remain before it.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function f() { var value = 1; doSomething(value);}Related:
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.