Skip to content

Commit

Permalink
Update: Add never option to arrow-body-style (fixes eslint#6317)
Browse files Browse the repository at this point in the history
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 });
```
  • Loading branch information
ajhyndman committed Jun 3, 2016
1 parent 89580a4 commit f862809
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions lib/rules/arrow-body-style.js
Expand Up @@ -18,14 +18,15 @@ module.exports = {

schema: [
{
enum: ["always", "as-needed"]
enum: ["always", "as-needed", "never"]
}
]
},

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
Expand All @@ -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) {
Expand Down

0 comments on commit f862809

Please sign in to comment.