rspec/rules/S1518/rule.adoc

63 lines
1.0 KiB
Plaintext
Raw Permalink Normal View History

== Why is this an issue?
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
2022-02-04 17:28:24 +01:00
[source,text]
----
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
2022-02-04 17:28:24 +01:00
[source,text]
----
fun(); // prints "foo"
function fun() {
print("foo");
}
fun(); // prints "foo"
----
or
2022-02-04 17:28:24 +01:00
[source,text]
----
fun(); // prints "foo"
function fun() {
print("foo");
}
fun(); // prints "foo"
function printBar() {
print("bar");
}
fun(); // prints "foo"
----