array-callback-return
強制陣列方法的回呼函數中必須有 return
陳述式
💡 hasSuggestions
此規則回報的一些問題可以透過編輯器建議手動修正
Array
有幾種用於過濾、映射和摺疊的方法。如果我們忘記在這些方法的回呼函數中寫入 return
陳述式,這可能是一個錯誤。如果您不想使用 return 或不需要回傳的結果,請考慮改用 .forEach。
// example: convert ['a', 'b', 'c'] --> {a: 0, b: 1, c: 2}
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
}, {}); // Error: cannot set property 'b' of undefined
規則詳細資訊
此規則強制在陣列方法的回呼函數中使用 return
陳述式。此外,它也可能強制 forEach
陣列方法的回呼函數不回傳值,透過使用 checkForEach
選項。
此規則會尋找以下方法的回呼函數,然後檢查 return
陳述式的使用情況。
Array.from
Array.prototype.every
Array.prototype.filter
Array.prototype.find
Array.prototype.findIndex
Array.prototype.findLast
Array.prototype.findLastIndex
Array.prototype.flatMap
Array.prototype.forEach
(選用,基於checkForEach
參數)Array.prototype.map
Array.prototype.reduce
Array.prototype.reduceRight
Array.prototype.some
Array.prototype.sort
Array.prototype.toSorted
- 以及上述的類型陣列。
錯誤程式碼範例(此規則)
在 Playground 中開啟
/*eslint array-callback-return: "error"*/
const indexMap = myArray.reduce((memo, item, index) {
memo[item] = index;
}, {});
const foo = Array.from(nodes, (node) {
if (node.tagName === "DIV") {
return true;
}
});
const bar = foo.filter(function(x) {
if (x) {
return true;
} else {
}
});
正確程式碼範例(此規則)
在 Playground 中開啟
/*eslint array-callback-return: "error"*/
const indexMap = myArray.reduce(function(memo, item, index) {
memo[item] = index;
return memo;
}, {});
const foo = Array.from(nodes, function(node) {
if (node.tagName === "DIV") {
return true;
}
return false;
});
const bar = foo.map(node => node.getAttribute("id"));
選項
此規則接受具有三個選項的設定物件
"allowImplicit": false
(預設) 當設定為true
時,允許需要回傳值的方法的回呼函數,使用不包含表達式的return
陳述式隱含地回傳undefined
。"checkForEach": false
(預設) 當設定為true
時,規則也會回報回傳值的forEach
回呼函數。"allowVoid": false
(預設) 當設定為true
時,允許在forEach
回呼函數中使用void
,因此規則不會回報具有void
運算子的回傳值。
注意: 只有在 checkForEach
選項設定為 true
時,{ "allowVoid": true }
才有效。
allowImplicit
{ "allowImplicit": true }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint array-callback-return: ["error", { allowImplicit: true }]*/
const undefAllTheThings = myArray.map(function(item) {
return;
});
checkForEach
{ "checkForEach": true }
選項的錯誤程式碼範例
在 Playground 中開啟
/*eslint array-callback-return: ["error", { checkForEach: true }]*/
myArray.forEach(function(item) {
});
myArray.forEach(function(item) {
if (item < 0) {
}
handleItem(item);
});
myArray.forEach(function(item) {
if (item < 0) {
}
handleItem(item);
});
myArray.forEach(item handleItem(item));
myArray.forEach(item void handleItem(item));
myArray.forEach(item => {
});
myArray.forEach(item => {
});
{ "checkForEach": true }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint array-callback-return: ["error", { checkForEach: true }]*/
myArray.forEach(function(item) {
handleItem(item)
});
myArray.forEach(function(item) {
if (item < 0) {
return;
}
handleItem(item);
});
myArray.forEach(function(item) {
handleItem(item);
return;
});
myArray.forEach(item => {
handleItem(item);
});
allowVoid
{ "allowVoid": true }
選項的正確程式碼範例
在 Playground 中開啟
/*eslint array-callback-return: ["error", { checkForEach: true, allowVoid: true }]*/
myArray.forEach(item => void handleItem(item));
myArray.forEach(item => {
return void handleItem(item);
});
myArray.forEach(item => {
if (item < 0) {
return void x;
}
handleItem(item);
});
已知限制
此規則檢查具有給定名稱的方法的回呼函數,即使具有該方法的物件不是陣列。
何時不該使用
如果您不希望在使用陣列方法的回呼函數中的 return
陳述式時收到警告,那麼停用此規則是安全的。
版本
此規則在 ESLint v2.0.0-alpha-1 中引入。