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 name(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"]
OptionWhat 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.
OptionDefaultWhat it does
enforceForTSTypesfalseAlso check get and set signatures in interfaces and type literals.
example.jswith "getBeforeSet"1 problem
const volume = {
  set level(value) {
    this.raw = value;
  },
  get level() {
    return this.raw;
  },
};
example.jswith "getBeforeSet"No problems
const 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 problem
interface Volume {
  get level(): number;
  mute(): void;
  set level(value: number);
}