sonarjs/no-variable-usage-before-declaration
eslint-plugin-sonarjs
No autofix
eslint.config.js
export default [
{
rules: {
"sonarjs/no-variable-usage-before-declaration": "error",
},
},
];Variables declared with "var" should be declared before they are used
var x = 1;
function fun(){
alert(x); // Noncompliant as x is declared later in the same scope
if(something) {
var x = 42; // Declaration in function scope (not block scope!) shadows global variable
}
}
fun(); // Unexpectedly alerts "undefined" instead of "1"var x = 1;
function fun() {
print(x);
if (something) {
x = 42;
}
}
fun(); // Print "1"Examples from the ESLint documentation.
No tracked preset enables this rule, so it has no reach yet.
None of the tracked presets enable this rule. Turn it on with the config below.
sonarjs/no-variable-usage-before-declaration
eslint-plugin-sonarjs
export default [
{
rules: {
"sonarjs/no-variable-usage-before-declaration": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Unused function parameters should be removed
Arithmetic operators should only have numbers as operands
Arithmetic operations should not result in "NaN"