
grouped-accessor-pairs
要求在对象字面量和类中分组访问器对
同一属性的 getter 和 setter 不一定需要彼此相邻定义。
例如,以下语句将创建相同的对象
const o = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
},
b: 1
};
const o1 = {
get a() {
return this.val;
},
b: 1,
set a(value) {
this.val = value;
}
};
虽然允许在对象或类定义中的任何位置定义 getter 或 setter 对,但将同一属性的访问器函数分组被认为是最佳实践。
换句话说,如果一个属性有一个 getter 和一个 setter,setter 应该在 getter 之后立即定义,反之亦然。
规则详情
此规则要求在对象字面量、类声明和类表达式中,同一属性的访问器函数定义分组。
可选地,此规则还可以强制执行一致的顺序(getBeforeSet
或 setBeforeGet
)。
此规则不强制要求 getter 或 setter 对的存在。如果您还想强制执行 getter/setter 对,请参阅 accessor-pairs。
此规则的错误代码示例
在 Playground 中打开
/*eslint grouped-accessor-pairs: "error"*/
const foo = {
get a() {
return this.val;
},
b: 1,
(value) {
this.val = value;
}
};
const bar = {
set b(value) {
this.val = value;
},
a: 1,
() {
return this.val;
}
}
class Foo {
set a(value) {
this.val = value;
}
b(){}
() {
return this.val;
}
}
const Bar = class {
static get a() {
return this.val;
}
b(){}
(value) {
this.val = value;
}
}
此规则的正确代码示例
在 Playground 中打开
/*eslint grouped-accessor-pairs: "error"*/
const foo = {
get a() {
return this.val;
},
set a(value) {
this.val = value;
},
b: 1
};
const 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(){}
}
选项
此规则有一个字符串选项
"anyOrder"
(默认)不强制执行顺序。"getBeforeSet"
如果一个属性同时具有 getter 和 setter,则要求 getter 在 setter 之前定义。"setBeforeGet"
如果一个属性同时具有 getter 和 setter,则要求 setter 在 getter 之前定义。
getBeforeSet
使用 "getBeforeSet"
选项时,此规则的错误代码示例
在 Playground 中打开
/*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/
const foo = {
set a(value) {
this.val = value;
},
() {
return this.val;
}
};
class Foo {
set a(value) {
this.val = value;
}
() {
return this.val;
}
}
const Bar = class {
static set a(value) {
this.val = value;
}
() {
return this.val;
}
}
使用 "getBeforeSet"
选项时,此规则的正确代码示例
在 Playground 中打开
/*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/
const 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"
选项时,此规则的错误代码示例
在 Playground 中打开
/*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/
const foo = {
get a() {
return this.val;
},
(value) {
this.val = value;
}
};
class Foo {
get a() {
return this.val;
}
(value) {
this.val = value;
}
}
const Bar = class {
static get a() {
return this.val;
}
(value) {
this.val = value;
}
}
使用 "setBeforeGet"
选项时,此规则的正确代码示例
在 Playground 中打开
/*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/
const 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;
}
}
已知限制
由于静态分析的限制,此规则不考虑可能的副作用,在某些情况下,对于具有计算键的 getter/setter(如下例所示),可能需要或遗漏要求分组或排序
/*eslint grouped-accessor-pairs: "error"*/
let a = 1;
// false warning (false positive)
const foo = {
get [a++]() {
return this.val;
},
b: 1,
set [a++](value) {
this.val = value;
}
};
// missed warning (false negative)
const bar = {
get [++a]() {
return this.val;
},
b: 1,
set [a](value) {
this.val = value;
}
};
此外,此规则不报告任何具有重复 getter 或 setter 的属性的警告。
如果您还想禁止对象字面量中的重复键,请参阅 no-dupe-keys。
如果您还想禁止类定义中的重复名称,请参阅 no-dupe-class-members。
相关规则
版本
此规则在 ESLint v6.7.0 中引入。
进一步阅读


