Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add except-object option to arrow-body-style rules. #5934

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -18,14 +18,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 @@ -42,12 +43,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 @@ -56,6 +67,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 @@ -30,7 +30,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 @@ -64,6 +66,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." }
]
}
]
});