This rule raises an issue when a getter or setter method recursively accesses the same property it defines, which causes infinite recursion.
When a getter or setter method accesses the same property it defines, it creates infinite recursion that leads to a stack overflow error.
For example, when you write get bar() { return this.bar; }, calling the getter triggers itself repeatedly because
this.bar invokes the same getter method. This creates an endless loop that consumes all available stack memory.
The same problem occurs with setters: set bar(value) { this.bar = value; } will call itself infinitely when you try to assign a value
to the property.
This type of error crashes your application at runtime with a "Maximum call stack size exceeded" error. It’s particularly dangerous because the code looks correct at first glance, making it easy to miss during code reviews.
The application will crash with a stack overflow error when the problematic getter or setter is accessed. This can lead to service downtime and poor user experience, especially if the property is accessed frequently in the application flow.
Use a different property name, typically with an underscore prefix to indicate it’s private, or access a different property that holds the actual value.
const user = {
get name() {
return this.name; // Noncompliant
},
set name(value) {
this.name = value; // Noncompliant
}
};
const user = {
get name() {
return this._name;
},
set name(value) {
this._name = value;
}
};