From fbd9c35dd37c2466b809a89c79703583de77d275 Mon Sep 17 00:00:00 2001 From: Harrison Shoff Date: Sun, 14 Feb 2016 11:14:38 -0800 Subject: [PATCH] [eslint-v2][arrays] add array-callback-return rule --- README.md | 45 +++++++++++++++++++ .../rules/best-practices.js | 3 ++ 2 files changed, 48 insertions(+) diff --git a/README.md b/README.md index 46e78358..b52dbdcc 100644 --- a/README.md +++ b/README.md @@ -346,6 +346,51 @@ Other Style Guides const nodes = Array.from(foo); ``` + - [4.5](#4.5) Use return statements in array method callbacks. It's ok to omit the return if the function body consists of a single statement following [8.2](#8.2). eslint: [array-callback-return](http://eslint.org/docs/rules/array-callback-return) + + ```javascript + // good + [1, 2, 3].map((x) => { + const y = x + 1; + return x * y; + }); + + // good + [1, 2, 3].map(x => x + 1); + + // bad + const flat = {}; + [[0, 1], [2, 3], [4, 5]].reduce((memo, item, index) => { + const flatten = memo.concat(item); + flat[index] = memo.concat(item); + }); + + // good + const flat = {}; + [[0, 1], [2, 3], [4, 5]].reduce((memo, item, index) => { + const flatten = memo.concat(item); + flat[index] = flatten; + return flatten; + }); + + // bad + [1, 2, 3].filter((x) => { + if (x > 2) { + return true; + } else { + return false; + } + }); + + // good + [1, 2, 3].filter((x) => { + if (x > 2) { + return true; + } + return false; + }); + ``` + **[⬆ back to top](#table-of-contents)** ## Destructuring diff --git a/packages/eslint-config-airbnb/rules/best-practices.js b/packages/eslint-config-airbnb/rules/best-practices.js index 5a50af00..6f667924 100644 --- a/packages/eslint-config-airbnb/rules/best-practices.js +++ b/packages/eslint-config-airbnb/rules/best-practices.js @@ -2,6 +2,9 @@ module.exports = { 'rules': { // enforces getter/setter pairs in objects 'accessor-pairs': 0, + // enforces return statements in callbacks of array's methods + // http://eslint.org/docs/rules/array-callback-return + 'array-callback-return': 2, // treat var statements as if they were block scoped 'block-scoped-var': 2, // specify the maximum cyclomatic complexity allowed in a program