eslint/grouped-accessor-pairs Style β
What it does β
Require grouped accessor pairs in object literals and classes
Why is this bad? β
While it is allowed to define the pair for a getter or a setter anywhere in an object or class definition, itβs considered a best practice to group accessor functions for the same property.
Examples β
Examples of incorrect code for this rule:
js
const foo = {
get a() {
return this.val;
},
b: 1,
set a(value) {
this.val = value;
},
};
Examples of correct code for this rule:
js
const foo = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
},
b: 1,
};
Examples of incorrect code for this rule with the getBeforeSet
option:
js
const foo = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
},
};
Examples of correct code for this rule with the getBeforeSet
option:
js
const foo = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
},
};
Examples of incorrect code for this rule with the setBeforeGet
option:
js
const foo = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
},
};
Examples of correct code for this rule with the setBeforeGet
option:
js
const foo = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
},
};
How to use β
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny grouped-accessor-pairs
json
{
"rules": {
"grouped-accessor-pairs": "error"
}
}