useAccessorPairs (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/useAccessorPairs - This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
accessor-pairs
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useAccessorPairs": "error" } } }}Description
Section titled “Description”Require a getter for every setter in objects and classes.
A property that has a setter but no getter can be written but not read:
reading it always returns undefined. This is rarely what you want and
usually means the getter was forgotten.
This rule checks setters in object literals and classes. Static and
instance members of a class are checked separately, so a static getter
doesn’t pair with an instance setter.
The rule also checks property descriptors passed to Object.create(),
Object.defineProperty(), Object.defineProperties(), and
Reflect.defineProperty(): a descriptor with a set function must also
have a get function.
Getters without setters are allowed, because read-only properties are common.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const user = { set name(value) { this._name = value; },};code-block.js:2:5 lint/nursery/useAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This setter has no matching getter.
1 │ const user = {
> 2 │ set name(value) {
│ ^^^^^^^^
3 │ this._name = value;
4 │ },
ℹ Without a getter, reading the property always returns undefined.
ℹ Add a getter for the property, or remove the setter.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class User { set name(value) { this._name = value; }}code-block.js:2:5 lint/nursery/useAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This setter has no matching getter.
1 │ class User {
> 2 │ set name(value) {
│ ^^^^^^^^
3 │ this._name = value;
4 │ }
ℹ Without a getter, reading the property always returns undefined.
ℹ Add a getter for the property, or remove the setter.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
Object.defineProperty(user, "name", { set(value) { this._name = value; },});code-block.js:2:5 lint/nursery/useAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This property descriptor has a setter but no getter.
1 │ Object.defineProperty(user, "name", {
> 2 │ set(value) {
│ ^^^
3 │ this._name = value;
4 │ },
ℹ Without a getter, reading the property always returns undefined.
ℹ Add a getter for the property, or remove the setter.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const user = { get name() { return this._name; }, set name(value) { this._name = value; },};
class Counter { get count() { return this._count; }}
Object.defineProperty(user, "name", { get() { return this._name; }, set(value) { this._name = value; },});See Also
Section titled “See Also”- If you want getters and setters for the same property to be next to each other, see
useGroupedAccessorPairs.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.