Back to Biomejs

useGroupedAccessorPairs

src/content/docs/linter/rules/use-grouped-accessor-pairs.mdx

latest5.8 KB
Original Source

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)

How to configure

json
{
	"linter": {
		"rules": {
			"style": {
				"useGroupedAccessorPairs": "error"
			}
		}
	}
}

Description

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.

Examples

Invalid

Name getter and setter are not adjacent:

js
class User {
  get name() { return this._name; }
  constructor() {}
  set name(value) { this._name = value; }
}
<pre class="language-text"><code class="language-text">code-block.js:2:7 <a href="https://biomejs.dev/linter/rules/use-grouped-accessor-pairs">lint/style/useGroupedAccessorPairs</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Getter should be defined right before the setter.</span> <strong>1 │ </strong>class User &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> get name() &#123; return this.&#95;name; &#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> constructor() &#123;&#125; <strong>4 │ </strong> set name(value) &#123; this.&#95;name = value; &#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Move this setter after the getter.</span> <strong>2 │ </strong> get name() &#123; return this.&#95;name; &#125; <strong>3 │ </strong> constructor() &#123;&#125; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> set name(value) &#123; this.&#95;name = value; &#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong>&#125; <strong>6 │ </strong> </code></pre>

Getter should go before the setter.

js
const user = {
  set name(value) { this._name = value; },
  get name() { return this._name; }
};
<pre class="language-text"><code class="language-text">code-block.js:3:7 <a href="https://biomejs.dev/linter/rules/use-grouped-accessor-pairs">lint/style/useGroupedAccessorPairs</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Getter should be defined right before the setter.</span> <strong>1 │ </strong>const user = &#123; <strong>2 │ </strong> set name(value) &#123; this.&#95;name = value; &#125;, <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> get name() &#123; return this.&#95;name; &#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>4 │ </strong>&#125;; <strong>5 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Move this setter after the getter.</span> <strong>1 │ </strong>const user = &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> set name(value) &#123; this.&#95;name = value; &#125;, <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> get name() &#123; return this.&#95;name; &#125; <strong>4 │ </strong>&#125;; </code></pre>

Valid

js
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.

</TabItem> </Tabs>