grouped-accessor-pairs
要求对象字面量和类中的分组访问器对
相同属性的 getter 和 setter 不一定必须彼此相邻定义。
¥A getter and setter for the same property don’t necessarily have to be defined adjacent to each other.
例如,以下语句将创建相同的对象:
¥For example, the following statements would create the same object:
var o = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
},
b: 1
};
var o = {
get a() {
return this.val;
},
b: 1,
set a(value) {
this.val = value;
}
};
虽然允许在对象或类定义中的任何位置为 getter 或 setter 定义对,但将访问器函数分组为同一属性被认为是最佳实践。
¥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.
换句话说,如果一个属性有一个 getter 和一个 setter,那么 setter 应该在 getter 之后定义,反之亦然。
¥In other words, if a property has a getter and a setter, the setter should be defined right after the getter, or vice versa.
规则详情
¥Rule Details
此规则要求对象字面量、类声明和类表达式中相同属性的访问器函数的分组定义。
¥This rule requires grouped definitions of accessor functions for the same property in object literals, class declarations and class expressions.
或者,此规则还可以强制执行一致的顺序(getBeforeSet
或 setBeforeGet
)。
¥Optionally, this rule can also enforce consistent order (getBeforeSet
or setBeforeGet
).
此规则不强制对 getter 或 setter 的存在。如果你还想强制执行 getter/setter 对,请参阅 accessor-pairs。
¥This rule does not enforce the existence of the pair for a getter or a setter. See accessor-pairs if you also want to enforce getter/setter pairs.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint grouped-accessor-pairs: "error"*/
var foo = {
get a() {
return this.val;
},
b: 1,
set a(value) {
this.val = value;
}
};
var bar = {
set b(value) {
this.val = value;
},
a: 1,
get b() {
return this.val;
}
}
class Foo {
set a(value) {
this.val = value;
}
b(){}
get a() {
return this.val;
}
}
const Bar = class {
static get a() {
return this.val;
}
b(){}
static set a(value) {
this.val = value;
}
}
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint grouped-accessor-pairs: "error"*/
var foo = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
},
b: 1
};
var bar = {
set b(value) {
this.val = value;
},
get b() {
return this.val;
},
a: 1
}
class Foo {
set a(value) {
this.val = value;
}
get a() {
return this.val;
}
b(){}
}
const Bar = class {
static get a() {
return this.val;
}
static set a(value) {
this.val = value;
}
b(){}
}
选项
¥Options
此规则有一个字符串选项:
¥This rule has a string option:
-
"anyOrder"
(默认)不强制执行命令。¥
"anyOrder"
(default) does not enforce order. -
"getBeforeSet"
如果属性同时具有 getter 和 setter,则要求 getter 在 setter 之前定义。¥
"getBeforeSet"
if a property has both getter and setter, requires the getter to be defined before the setter. -
"setBeforeGet"
如果属性同时具有 getter 和 setter,则要求在 getter 之前定义 setter。¥
"setBeforeGet"
if a property has both getter and setter, requires the setter to be defined before the getter.
getBeforeSet
使用 "getBeforeSet"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "getBeforeSet"
option:
/*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/
var foo = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
}
};
class Foo {
set a(value) {
this.val = value;
}
get a() {
return this.val;
}
}
const Bar = class {
static set a(value) {
this.val = value;
}
static get a() {
return this.val;
}
}
使用 "getBeforeSet"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "getBeforeSet"
option:
/*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/
var foo = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
}
};
class Foo {
get a() {
return this.val;
}
set a(value) {
this.val = value;
}
}
const Bar = class {
static get a() {
return this.val;
}
static set a(value) {
this.val = value;
}
}
setBeforeGet
使用 "setBeforeGet"
选项的此规则的错误代码示例:
¥Examples of incorrect code for this rule with the "setBeforeGet"
option:
/*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/
var foo = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
}
};
class Foo {
get a() {
return this.val;
}
set a(value) {
this.val = value;
}
}
const Bar = class {
static get a() {
return this.val;
}
static set a(value) {
this.val = value;
}
}
使用 "setBeforeGet"
选项的此规则的正确代码示例:
¥Examples of correct code for this rule with the "setBeforeGet"
option:
/*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/
var foo = {
set a(value) {
this.val = value;
},
get a() {
return this.val;
}
};
class Foo {
set a(value) {
this.val = value;
}
get a() {
return this.val;
}
}
const Bar = class {
static set a(value) {
this.val = value;
}
static get a() {
return this.val;
}
}
已知限制
¥Known Limitations
由于静态分析的限制,此规则不考虑可能的副作用,并且在某些情况下可能需要或未要求对具有计算键的 getter/setter 进行分组或排序,如下例所示:
¥Due to the limits of static analysis, this rule does not account for possible side effects and in certain cases might require or miss to require grouping or order for getters/setters that have a computed key, like in the following example:
/*eslint grouped-accessor-pairs: "error"*/
var a = 1;
// false warning (false positive)
var foo = {
get [a++]() {
return this.val;
},
b: 1,
set [a++](value) {
this.val = value;
}
};
// missed warning (false negative)
var bar = {
get [++a]() {
return this.val;
},
b: 1,
set [a](value) {
this.val = value;
}
};
此外,对于具有重复 getter 或 setter 的属性,此规则不会报告任何警告。
¥Also, this rule does not report any warnings for properties that have duplicate getters or setters.
如果你还想禁止对象字面量中的重复键,请参阅 no-dupe-keys。
¥See no-dupe-keys if you also want to disallow duplicate keys in object literals.
如果你还想在类定义中禁止重复名称,请参阅 no-dupe-class-members。
¥See no-dupe-class-members if you also want to disallow duplicate names in class definitions.
相关规则
版本
此规则是在 ESLint v6.7.0 中引入。