Skip to content

Commit

Permalink
New: except-object option for arrow-body-style rule. (fixes eslin…
Browse files Browse the repository at this point in the history
…t#5936)

Blockless arrow functions are awesome, but when returning an object literal it's necessary to enclose the return value in parentheses. This rule extension enforces blockless arrow functions excluding cases where the arrow function returns an object literal.
  • Loading branch information
duncanbeevers committed Apr 22, 2016
1 parent 864c5eb commit f73e826
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
24 changes: 24 additions & 0 deletions docs/rules/arrow-body-style.md
Expand Up @@ -12,6 +12,7 @@ The rule takes one option, a string, which can be:

* `"always"` enforces braces around the function body
* `"as-needed"` enforces no braces where they can be omitted (default)
* `"except-object"` enforces no braces, except when returning object literals

### "always"

Expand Down Expand Up @@ -70,3 +71,26 @@ let foo = () => {
// do nothing.
};
```

### "except-object"

When the rule is set to `"except-object"` the following patterns are considered problems:

```js
/*eslint arrow-body-style: ["error", "except-object"]*/
/*eslit-env es6*/

let foo = () => ({ key: 'bar' });
```

The following patterns are not considered problems:

```js

/*eslint arrow-body-style: ["error", "except-object"]*/
/*eslit-env es6*/

let foo = () => {
return { key: 'bar' };
};
```
31 changes: 24 additions & 7 deletions lib/rules/arrow-body-style.js
Expand Up @@ -20,14 +20,15 @@ module.exports = {

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

create: function(context) {
var always = context.options[0] === "always";
var asNeeded = !context.options[0] || context.options[0] === "as-needed";
var exceptObject = context.options[0] === "except-object";

/**
* Determines whether a arrow function body needs braces
Expand All @@ -44,12 +45,22 @@ module.exports = {
return;
}

if (asNeeded && blockBody[0].type === "ReturnStatement") {
context.report({
node: node,
loc: arrowBody.loc.start,
message: "Unexpected block statement surrounding arrow body."
});
if (blockBody[0].type === "ReturnStatement") {
if (blockBody[0].argument.type !== "ObjectExpression") {
if (exceptObject || asNeeded) {
context.report({
node: node,
loc: arrowBody.loc.start,
message: "Unexpected block statement surrounding arrow body."
});
} else if (exceptObject) {
context.report({
node: node,
loc: arrowBody.loc.start,
message: "Expected block statement surrounding arrow body returning object literal."
});
}
}
}
} else {
if (always) {
Expand All @@ -58,6 +69,12 @@ module.exports = {
loc: arrowBody.loc.start,
message: "Expected block statement surrounding arrow body."
});
} else if (exceptObject && node.body.type === "ObjectExpression") {
context.report({
node: node,
loc: arrowBody.loc.start,
message: "Unexpected object literal returned from blockless arrow body."
});
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion tests/lib/rules/arrow-body-style.js
Expand Up @@ -32,7 +32,9 @@ ruleTester.run("arrow-body-style", rule, {
{ code: "var foo = () => { b = a };", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => { bar: 1 };", parserOptions: { ecmaVersion: 6 } },
{ code: "var foo = () => { return 0; };", parserOptions: { ecmaVersion: 6 }, options: ["always"] },
{ code: "var foo = () => { return bar(); };", parserOptions: { ecmaVersion: 6 }, options: ["always"] }
{ code: "var foo = () => { return bar(); };", parserOptions: { ecmaVersion: 6 }, options: ["always"] },
{ code: "var foo = () => { return { bar: 1 }; };", parserOptions: { ecmaVersion: 6 }, options: ["except-object"] },
{ code: "var foo = () => 0;", parserOptions: { ecmaVersion: 6 }, options: ["except-object"] }
],
invalid: [
{
Expand Down Expand Up @@ -66,6 +68,22 @@ ruleTester.run("arrow-body-style", rule, {
errors: [
{ line: 1, column: 17, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
},
{
code: "var foo = () => ({ bar: 1 });",
parserOptions: { ecmaVersion: 6 },
options: ["except-object"],
errors: [
{ line: 1, column: 18, type: "ArrowFunctionExpression", message: "Unexpected object literal returned from blockless arrow body." }
]
},
{
code: "var foo = () => { return 0 };",
parserOptions: { ecmaVersion: 6 },
options: ["except-object"],
errors: [
{ line: 1, column: 17, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
}
]
});

0 comments on commit f73e826

Please sign in to comment.