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

Update: Add never option to arrow-body-style (fixes #6317) #6318

Merged
merged 4 commits into from Jun 9, 2016
Merged
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
32 changes: 30 additions & 2 deletions docs/rules/arrow-body-style.md
@@ -1,17 +1,18 @@
# Require braces in arrow function body (arrow-body-style)

Arrow functions can omit braces when there is a single statement in the body. This rule enforces the consistent use of braces in arrow functions.
Arrow functions have two syntactic forms for their function bodies. They may be defined with a *block* body (denoted by curly braces) `() => { ... }` or with a single expression `() => ...`, whose value is implicitly returned.

## Rule Details

This rule can enforce the use of braces around arrow function body.
This rule can enforce or disallow the use of braces around arrow function body.

## Options

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)
* `"never"` enforces no braces around the function body (constrains arrow functions to the role of returning an expression)

### "always"

Expand Down Expand Up @@ -84,3 +85,30 @@ let foo = () => {
// do nothing.
};
```

### "never"

When the rule is set to `"never"` the following patterns are considered problems:

```js
/*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:

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

let foo = () => 0;
let foo = () => ({ foo: 0 });
```
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
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 = () => 0;", parserOptions: { ecmaVersion: 6 }, options: ["never"] },
{ code: "var foo = () => ({ foo: 0 });", parserOptions: { ecmaVersion: 6 }, options: ["never"] }
],
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 = () => {\nreturn 0;\n};",
parserOptions: { ecmaVersion: 6 },
options: ["never"],
errors: [
{ line: 1, column: 17, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
},
{
code: "var foo = (retv, name) => {\nretv[name] = true;\nreturn retv;\n};",
parserOptions: { ecmaVersion: 6 },
options: ["never"],
errors: [
{ line: 1, column: 27, type: "ArrowFunctionExpression", message: "Unexpected block statement surrounding arrow body." }
]
}
]
});