org.sonar.l10n.javascript.rules.javascript.S2432.html Maven / Gradle / Ivy
Why is this an issue?
In JavaScript, a setter is a special type of function that is used to set the value of a property on an object. Setters are defined using the
set
keyword followed by the name of the property that the setter is associated with.
To set the property, we simply assign a value to it as if it were a regular property. The setter function is automatically called with the value
that we assign to the property.
Functions declared with the set
keyword will automatically return the values they were passed. Thus any value explicitly returned from
a setter will be ignored, and explicitly returning a value is a mistake.
let person = {
// ...
set firstname(first) {
this.first = first;
return 42; // Noncompliant: The return value 42 will be ignored
}
};
console.log(person.firstname = 'bob'); // Prints 'bob'
Since return values in setters are ignored, you should remove return statements altogether.
let person = {
// ...
set firstname(first) {
this.first = first;
}
};
console.log(person.firstname = 'bob'); // Prints 'bob'
Resources
Documentation
- MDN web docs - set