From f8628094e4a8322c899e9691391e32f0f7d527e8 Mon Sep 17 00:00:00 2001 From: Andrew Hyndman Date: Fri, 3 Jun 2016 23:06:50 +1000 Subject: [PATCH] Update: Add never option to arrow-body-style (fixes #6317) Arrow functions that return object literals can look very similar to arrow functions with brace bodies. Some syntactic ambiguity can be avoided by disallowing block-style arrow functions in favour of ES5 function expressions. **Outcome** The following patterns are considered problems: ``` /*eslint arrow-body-style: ["error", "never"]*/ /*eslint-env es6*/ let foo = () => { return 0; }; let foo = (retv, name) => { retv[name] = true; return retv; }; ``` The following patterns are not considered problems: ``` /*eslint arrow-body-style: ["error", "never"]*/ /*eslint-env es6*/ let foo = () => 0; let foo = () => ({ key: 0 }); ``` --- lib/rules/arrow-body-style.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/rules/arrow-body-style.js b/lib/rules/arrow-body-style.js index 79fde90f805..1b2d1996a67 100644 --- a/lib/rules/arrow-body-style.js +++ b/lib/rules/arrow-body-style.js @@ -18,7 +18,7 @@ module.exports = { schema: [ { - enum: ["always", "as-needed"] + enum: ["always", "as-needed", "never"] } ] }, @@ -26,6 +26,7 @@ module.exports = { create: function(context) { var always = context.options[0] === "always"; var asNeeded = !context.options[0] || context.options[0] === "as-needed"; + var never = context.options[0] === "never"; /** * Determines whether a arrow function body needs braces @@ -36,18 +37,26 @@ module.exports = { var arrowBody = node.body; if (arrowBody.type === "BlockStatement") { - var blockBody = arrowBody.body; - - if (blockBody.length !== 1) { - return; - } - - if (asNeeded && blockBody[0].type === "ReturnStatement") { + if (never) { context.report({ node: node, loc: arrowBody.loc.start, message: "Unexpected block statement surrounding arrow body." }); + } else { + var blockBody = arrowBody.body; + + if (blockBody.length !== 1) { + return; + } + + if (asNeeded && blockBody[0].type === "ReturnStatement") { + context.report({ + node: node, + loc: arrowBody.loc.start, + message: "Unexpected block statement surrounding arrow body." + }); + } } } else { if (always) {