grouped-accessor-pairs
Require the getter and setter of a property to be next to each other
When other members sit between a property's getter and setter, someone who finds the getter can miss that there's a setter further down.
The rule checks object literals and classes. In a class, a static pair and an instance pair with the same name are separate.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"grouped-accessor-pairs" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"grouped-accessor-pairs" = [
"error",
"anyOrder", # "anyOrder", "getBeforeSet" or "setBeforeGet"
{
enforceForTSTypes = false,
},
]Examples#
example.js1 problem
class User {
get name() {
return this.first;
}
save() {}
set nameAccessor pair getter name and setter name should be grouped.(value) {
this.first = value;
}
}example.jsNo problems
class User {
get name() {
return this.first;
}
set name(value) {
this.first = value;
}
save() {}
}Options#
bunfig.toml
[lint.rules]
grouped-accessor-pairs = ["error", "getBeforeSet"]| Option | What it does |
|---|---|
"anyOrder" (default) | The pair must be adjacent, in either order. |
"getBeforeSet" | The getter must also come first. |
"setBeforeGet" | The setter must also come first. |
| Option | Default | What it does |
|---|---|---|
enforceForTSTypes | false | Also check get and set signatures in interfaces and type literals. |
example.jswith
"getBeforeSet"1 problemconst volume = {
set level(value) {
this.raw = value;
},
get levelExpected getter level to be before setter level.() {
return this.raw;
},
};example.jswith
"getBeforeSet"No problemsconst volume = {
get level() {
return this.raw;
},
set level(value) {
this.raw = value;
},
};enforceForTSTypes#
example.tsNo problems
interface Volume {
get level(): number;
mute(): void;
set level(value: number);
}example.tswith
"anyOrder", { "enforceForTSTypes": true }1 probleminterface Volume {
get level(): number;
mute(): void;
set levelAccessor pair getter level and setter level should be grouped.(value: number);
}