Skip to content

useAdjacentGetterSetter

Enforce that getters and setters for the same property are adjacent in class and object definitions.

When defining a property in a class or object, it’s common to have both a getter and a setter. This rule enforces that getter is defined right before the setter, making the code more maintainable and easier to read.

Name getter and setter are not adjacent:

class User {
get name() { return this._name; }
constructor() {}
set name(value) { this._name = value; }
}
code-block.js:2:7 lint/nursery/useAdjacentGetterSetter ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Getter should be defined right before the setter.

1 │ class User {
> 2 │ get name() { return this._name; }
^^^^
3 │ constructor() {}
4 │ set name(value) { this._name = value; }

Move this setter after the getter.

2 │ get name() { return this._name; }
3 │ constructor() {}
> 4 │ set name(value) { this._name = value; }
^^^^
5 │ }
6 │

Getter should go before the setter.

const user = {
set name(value) { this._name = value; },
get name() { return this._name; }
};
code-block.js:3:7 lint/nursery/useAdjacentGetterSetter ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Getter should be defined right before the setter.

1 │ const user = {
2 │ set name(value) { this._name = value; },
> 3 │ get name() { return this._name; }
^^^^
4 │ };
5 │

Move this setter after the getter.

1 │ const user = {
> 2 │ set name(value) { this._name = value; },
^^^^
3 │ get name() { return this._name; }
4 │ };

class User {
get name() { return this._name; }
set name(value) { this._name = value; }
get age() { return this._age; }
set age(age) { this._age = age; }
}

This rule does not enforce the existence of both getter and setter for a property. Single getters without setters and setters without getters are ignored.

biome.json
{
"linter": {
"rules": {
"nursery": {
"useAdjacentGetterSetter": "error"
}
}
}
}