eslint: Unexpected block statement surrounding arrow body arrow-body-style
What version of ESLint are you using? 2.3.0
What configuration and parser (Espree, Babel-ESLint, etc.) are you using? Babel-Eslint version babel/babel-eslint#d2f90c7239e152b8994f250c69c83604cde93292 (from 07 march 2016, need this commit for fix ‘estraverse-fb’)
What did you do? Please include the actual source code causing the issue.
this.post('/post', () => {
return {
post: {
foo: 1,
bar: 2,
},
};
});
Error:
1:28 error Unexpected block statement surrounding arrow body arrow-body-style
I cant remove the { } after removing the ‘return’ because I want to response a object: { }
What did you expect to happen?
No error message with the default setting ‘as-needed’
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 3
- Comments: 27 (10 by maintainers)
Commits related to this issue
- Update: added example for #5498 — committed to teameh/eslint by teameh 8 years ago
- Update: added example for #5498 (arrow-body-style) — committed to teameh/eslint by teameh 8 years ago
- Docs: added examples for arrow-body-style (refs #5498) — committed to teameh/eslint by teameh 8 years ago
- Docs: added examples for arrow-body-style (refs #5498) (#6242) — committed to eslint/eslint by teameh 8 years ago
- Chore: improve arrow-body-style error message (refs #5498) https://github.com/eslint/eslint/issues/5498 gets more than twice as much traffic as any of our other issues. My interpretation of this fact... — committed to eslint/eslint by not-an-aardvark 7 years ago
- Chore: improve arrow-body-style error message (refs #5498) (#9718) https://github.com/eslint/eslint/issues/5498 gets more than twice as much traffic as any of our other issues. My interpretation of ... — committed to eslint/eslint by not-an-aardvark 7 years ago
If you wrap the object in parentheses, it will parse correctly and the rule won’t error:
@michaelficarra is right. This is intended behavior. If
return
is the only statement inside the body of an arrow function, you do not need block statement. @platinumazure provided an example of valid code for this case. Closing, because this is working as intended.@lucasbento
@JeffBaumgardt if that’s still an issue for you. Change the curly braces to parentheses. i.e
I had a bunch of these in my express app when returning function calls. Hope this clarifies for some people.
As the rule states:
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.Keep in mind that a function can be this “value” that’s implicitly returned. In general, you can just nuke the braces and the
return
.Ex:
becomes
For what it’s worth,
arrow-body-style
is autofixable as of a couple months ago, so if you runeslint --fix
then ESLint will fix this issue for you.@Badbreaddead this should work:
@albert-olive try
Sorry to bump this again, but after trying the multiple suggestion here, i’m still unable to make the error go away, this is my code:
EDIT: Figured out i had to remove the ‘return’ as im returning a single expression
I feel like the earliest comments (i.e. the ones people see when the first show up at this page looking for help) should be edited or something to be clearer: the issue is that an anonymous function containing only a
return
can *remove the{ return
…}
portion. There’s like 7 other people asking for help because they don’t get it because it isn’t clear (early on this thread) that the advice is to remove the wordreturn
.What about
arr.reduce()
? How can we fix this so ESLint does not claim it’s wrong?@ilyavolodin No, it was the right button. This is not a bug.
I see you edited your question @Badbreaddead so I’ve edited my answer to reflect how you’d write that to prevent being stopped by the rule
so, if return is just an object, you do not need block statement. and if return is just a statement, you do not need ‘return’
@alexanderkartavtsev The fixer was added in ESLint 3.9.0.
I found this by opening the rule file, using “Blame” to find the commit which introduced the
fixable: "code"
line, and then GitHub told me that the nearest tag after that commit was for 3.9.0. I confirmed this by reading the release notes for 3.9.0.I know this is closed but I was hoping someone could help with my issue. I don’t know what to do to fix it. As soon as I try one thing I get a different error.