
grouped-accessor-pairs
要求物件實字和類別中成組的存取器配對
同一個屬性的 getter 和 setter 不一定必須定義在彼此相鄰的位置。
例如,以下陳述式會建立相同的物件
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 的配對,但最好將同一個屬性的存取器函式分組。
換句話說,如果一個屬性有 getter 和 setter,則 setter 應定義在 getter 之後,反之亦然。
規則詳情
此規則要求物件實字、類別宣告和類別表達式中,同一個屬性的存取器函式定義需成組。
選擇性地,此規則還可以強制執行一致的順序(getBeforeSet
或 setBeforeGet
)。
此規則不會強制要求 getter 或 setter 的配對存在。如果您也想強制要求 getter/setter 配對,請參閱 accessor-pairs。
此規則的不正確程式碼範例
在遊樂場中開啟
/*eslint grouped-accessor-pairs: "error"*/
var foo = {
get a() {
return this.val;
},
b: 1,
(value) {
this.val = value;
}
};
var 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;
}
}
此規則的正確程式碼範例
在遊樂場中開啟
/*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(){}
}
選項
此規則有一個字串選項
"anyOrder"
(預設) 不強制執行順序。"getBeforeSet"
如果一個屬性同時具有 getter 和 setter,則要求 getter 定義在 setter 之前。"setBeforeGet"
如果一個屬性同時具有 getter 和 setter,則要求 setter 定義在 getter 之前。
getBeforeSet
此規則使用 "getBeforeSet"
選項的不正確程式碼範例
在遊樂場中開啟
/*eslint grouped-accessor-pairs: ["error", "getBeforeSet"]*/
var 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"
選項的正確程式碼範例
在遊樂場中開啟
/*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"
選項的不正確程式碼範例
在遊樂場中開啟
/*eslint grouped-accessor-pairs: ["error", "setBeforeGet"]*/
var 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"
選項的正確程式碼範例
在遊樂場中開啟
/*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;
}
}
已知限制
由於靜態分析的限制,此規則不考慮可能的副作用,在某些情況下,可能會要求或遺漏要求具有計算鍵的 getter/setter 的分組或順序,如下例所示
/*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 的屬性的任何警告。
如果您也想禁止物件實字中的重複鍵,請參閱 no-dupe-keys。
如果您也想禁止類別定義中的重複名稱,請參閱 no-dupe-class-members。
相關規則
版本
此規則在 ESLint v6.7.0 中引入。
延伸閱讀


