useGroupedAccessorPairs
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/style/useGroupedAccessorPairs¥Diagnostic Category:
lint/style/useGroupedAccessorPairs -
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
¥Same as
grouped-accessor-pairs
-
¥How to configure
{ "linter": { "rules": { "style": { "useGroupedAccessorPairs": "error" } } }}¥Description
强制要求在类和对象定义中,同一属性的 getter 和 setter 必须相邻。
¥Enforce that getters and setters for the same property are adjacent in class and object definitions.
在类或对象中定义属性时,通常会同时包含 getter 和 setter。此规则强制要求 getter 必须在 setter 之前定义,从而使代码更易于维护和阅读。
¥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
名称 getter 和 setter 不相邻:
¥Name getter and setter are not adjacent:
class User { get name() { return this._name; } constructor() {} set name(value) { this._name = value; }}code-block.js:2:7 lint/style/useGroupedAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Getter should be defined right before the setter.
1 │ class User {
> 2 │ get name() { return this._name; }
│ ^^^^
3 │ constructor() {}
4 │ set name(value) { this._name = value; }
ℹ Move this setter after the getter.
2 │ get name() { return this._name; }
3 │ constructor() {}
> 4 │ set name(value) { this._name = value; }
│ ^^^^
5 │ }
6 │
Getter 方法应该放在 Setter 方法之前。
¥Getter should go before the setter.
const user = { set name(value) { this._name = value; }, get name() { return this._name; }};code-block.js:3:7 lint/style/useGroupedAccessorPairs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Getter should be defined right before the setter.
1 │ const user = {
2 │ set name(value) { this._name = value; },
> 3 │ get name() { return this._name; }
│ ^^^^
4 │ };
5 │
ℹ Move this setter after the getter.
1 │ const user = {
> 2 │ set name(value) { this._name = value; },
│ ^^^^
3 │ get name() { return this._name; }
4 │ };
¥Valid
class User { get name() { return this._name; } set name(value) { this._name = value; } get age() { return this._age; } set age(age) { this._age = age; }}此规则不强制要求属性必须同时存在 getter 和 setter。忽略没有 setter 的单个 getter 和没有 getter 的 setter。
¥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.
¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号