useAdjacentGetterSetter
Summary
Section titled “Summary”- Rule available since:
v2.0.0
- Diagnostic Category:
lint/nursery/useAdjacentGetterSetter
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
grouped-accessor-pairs
- Same as
Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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.
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useAdjacentGetterSetter": "error" } } }}