All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.javascript.rules.javascript.S2432.html Maven / Gradle / Ivy

There is a newer version: 10.17.0.28100
Show newest version

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy