
org.sonar.l10n.javascript.rules.javascript.RedeclaredFunction.html Maven / Gradle / Ivy
This rule checks that functions declared in same scope don't have identical names. Indeed, it is possible to declare 2 functions with the same name, but only the last definition will be kept by the JavaScript engine before starting execution of the code.
This use of duplicate function name is often unwanted and can lead to bugs and more generally to confusing code.
Noncompliant Code Example
fun(); // prints "bar"
// first declaration of the function
function fun() {
print("foo");
}
fun(); // prints "bar"
// redeclaration of the "fun" function: this definition overrides the previous one
function fun() {
print("bar");
}
fun(); // prints "bar"
Compliant Solution
fun(); // prints "foo"
function fun() {
print("foo");
}
fun(); // prints "foo"
or
fun(); // prints "foo"
function fun() {
print("foo");
}
fun(); // prints "foo"
function printBar() {
print("bar");
}
fun(); // prints "foo"
This rule is deprecated, use {rule:javascript:S2814} instead.
© 2015 - 2025 Weber Informatics LLC | Privacy Policy