# unicorn/no-accessor-recursion

> Disallow a getter or setter that uses its own property on `this`

A getter that reads `this.name` inside `get name()` calls itself. So does a setter that assigns to `this.name` inside `set name()`. The call never ends, and the program throws `RangeError: Maximum call stack size exceeded` the first time the property is used.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/no-accessor-recursion" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```js
class User {
  get name() {
    return this.name;
  }

  set name(value) {
    this.name = value.trim();
  }
}
```

Destructuring the property from `this` reads it too:

{/* bad */}
```js
const settings = {
  get theme() {
    const { theme } = this;
    return theme ?? "light";
  },
};
```

Not reported:

{/* good */}
```js
class User {
  #name = "";

  get name() {
    return this.#name;
  }

  set name(value) {
    // Reading this.name in the setter is fine: it calls the getter.
    if (this.name !== value) {
      this.#name = value.trim();
    }
  }
}
```
