1 # Require grouped accessor pairs in object literals and classes (grouped-accessor-pairs)
3 A getter and setter for the same property don't necessarily have to be defined adjacent to each other.
5 For example, the following statements would create the same object:
29 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.
31 In other words, if a property has a getter and a setter, the setter should be defined right after the getter, or vice versa.
35 This rule requires grouped definitions of accessor functions for the same property in object literals, class declarations and class expressions.
37 Optionally, this rule can also enforce consistent order (`getBeforeSet` or `setBeforeGet`).
39 This rule does not enforce the existence of the pair for a getter or a setter. See [accessor-pairs](accessor-pairs.md) if you also want to enforce getter/setter pairs.
41 Examples of **incorrect** code for this rule:
44 /*eslint grouped-accessor-pairs: "error"*/
87 Examples of **correct** code for this rule:
90 /*eslint grouped-accessor-pairs: "error"*/
126 static set a(value) {
135 This rule has a string option:
137 * `"anyOrder"` (default) does not enforce order.
138 * `"getBeforeSet"` if a property has both getter and setter, requires the getter to be defined before the setter.
139 * `"setBeforeGet"` if a property has both getter and setter, requires the setter to be defined before the getter.
143 Examples of **incorrect** code for this rule with the `"getBeforeSet"` option:
146 /*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/
167 static set a(value) {
176 Examples of **correct** code for this rule with the `"getBeforeSet"` option:
179 /*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/
203 static set a(value) {
211 Examples of **incorrect** code for this rule with the `"setBeforeGet"` option:
214 /*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/
238 static set a(value) {
244 Examples of **correct** code for this rule with the `"setBeforeGet"` option:
247 /*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/
268 static set a(value) {
279 Due to the limits of static analysis, this rule does not account for possible side effects and in certain cases
280 might require or miss to require grouping or order for getters/setters that have a computed key, like in the following example:
283 /*eslint grouped-accessor-pairs: "error"*/
287 // false warning (false positive)
298 // missed warning (false negative)
310 Also, this rule does not report any warnings for properties that have duplicate getters or setters.
312 See [no-dupe-keys](no-dupe-keys.md) if you also want to disallow duplicate keys in object literals.
314 See [no-dupe-class-members](no-dupe-class-members.md) if you also want to disallow duplicate names in class definitions.
318 * [Object Setters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set)
319 * [Object Getters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get)
320 * [Classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)
324 * [accessor-pairs](accessor-pairs.md)
325 * [no-dupe-keys](no-dupe-keys.md)
326 * [no-dupe-class-members](no-dupe-class-members.md)