src/content/docs/linter/rules/use-grouped-accessor-pairs.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.0.0` - Diagnostic Category: [`lint/style/useGroupedAccessorPairs`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`grouped-accessor-pairs`](https://eslint.org/docs/latest/rules/grouped-accessor-pairs){
"linter": {
"rules": {
"style": {
"useGroupedAccessorPairs": "error"
}
}
}
}
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; }
}
Getter should go before the setter.
const user = {
set name(value) { this._name = value; },
get name() { return this._name; }
};
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.