
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 中引入。
延伸閱讀


