Aller au contenu

useVarsOnTop

Ce contenu n’est pas encore disponible dans votre langue.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useVarsOnTop": "error"
}
}
}
}

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.

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: