rspec/rules/S2814/rule.adoc

65 lines
1.2 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:48:07 +02:00
This rule checks that a declaration doesn't use a name that is already in use. Indeed, it is possible to use the same symbol multiple times as either a variable or a function, but doing so is likely to confuse maintainers. Further it's possible that such reassignments are made in error, with the developer not realizing that the value of the variable is overwritten by the new assignment.
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:07 +02:00
This rule also applies to function parameters.
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:07 +02:00
----
var a = 'foo';
function a() {} // Noncompliant
console.log(a); // prints "foo"
function myFunc(arg) {
var arg = "event"; // Noncompliant, argument value is lost
}
fun(); // prints "bar"
function fun() {
console.log("foo");
}
fun(); // prints "bar"
function fun() { // Noncompliant
console.log("bar");
}
fun(); // prints "bar"
----
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:07 +02:00
----
var a = 'foo';
function otherName() {}
console.log(a);
function myFunc(arg) {
var newName = "event";
}
fun(); // prints "foo"
function fun() {
print("foo");
}
fun(); // prints "foo"
function printBar() {
print("bar");
}
printBar(); // prints "bar"
----