question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add option to arrow-body-style to disallow braces for single-statement functions

See original GitHub issue

What rule do you want to change?

arrow-body-style

Does this change cause the rule to produce more or fewer warnings?

More

How will the change be implemented? (New option, new default behavior, etc.)?

I’d be happy if it was a new option or if as-needed changed its behavior.

Please provide some example code that this change will affect:

let foo = () => { bar(); };

I’d like to propose What does the rule currently do for this code?

Currently, that code is considered correct for as-needed.

What will the rule do after it’s changed?

If the proposed enhancement is a change to as-needed, that code would become incorrect.

If the proposed enhancement is a new option, the new option would function exactly like as-needed except that code would become incorrect.


While it is technically different from let foo = () => bar(); because the return value is undefined for the with-braces version but the brace-less version returns the return value of bar(), in day-to-day code it often doesn’t make a difference. It is often stylistically cleaner not to have the braces, especially when using an auto-formatter like Prettier.js that multi-lines every brace-including arrow function.

Examples of code that don’t care whether the braces are there (return undefined implicit) or not (return the statement’s value):

imgTag.onload = () => myApp.showSuccess();
Ember.run(() => this.set('foo', 'bar'));
QUnit.assert.throws(() => myApp.shouldThrow());

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:21 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
not-an-aardvarkcommented, Sep 11, 2018

Nope, the suggestion to use an extra return is only in cases where the implicit undefined return value of a single-statement arrow function with a block matters.

Yes, that’s what I meant – sorry, my wording was a bit unclear. Thanks for clarifying.

In the same vein, returning undefined by including a block because braces were typed (“because other functions have braces so why not this one”) seems like it could cause confusion due to a lack of explicitness; when reading and refactoring, it might be difficult to tell which undefined return values are actually used, and which undefined return values are only present because the developer “accidentally” used braces.

I’m not sure I follow. Could you elaborate on what you mean by “accidentally” using braces?


Here’s an example to demonstrate what I mean about readability:

// Sample A
doSomethingWithACallback(() => foo());
// Sample B
doSomethingWithACallback(() => {
    foo();
});
// Sample C
doSomethingWithACallback(() => {
    foo();
    bar();
});
// Sample D
doSomethingWithACallback(() => {
    bar();
    return foo();
});

Suppose my code looks like Sample A, and I want to update the callback so that bar() gets called in addition to foo(). From just looking at the code, it’s not clear whether the return value of foo() is used in doSomethingWithACallback. If the return value is unused, then the correct update would look like Sample C. If the return value is used, then the correct update would look like Sample D (to ensure that the same value is returned as before). In order to figure out the correct refactoring, I might have to look at the implementation of doSomethingWithACallback and/or foo.

On the other hand, if my code initially looks like Sample B, then it’s clear that the return value of foo is not being used, because it never leaves the callback. So I can immediately tell that Sample C would be the correct refactoring, without needing to look at the implementation of doSomethingWithACallback or foo.

In other words, in the case where a return value is unused, Sample B is more “explicit” than Sample A in that it provides more clarity about how the system behaves. The intent is clear based on the presence of the block, despite the fact that the return keyword doesn’t appear anywhere in the text.

1reaction
Kerrickcommented, Oct 3, 2018

@Kerrick the different is that == vs === is likely a bug; the likely bug is omitting the braces, unintentionally exposing a return value, which unintentionally becomes part of the contract of your function.

You raise a really good point here. I had considered it to be more about accidentally not returning a value, but you’re probably right that the more likely accident is to return a value you didn’t intend.

Read more comments on GitHub >

github_iconTop Results From Across the Web

arrow-body-style - ESLint - Pluggable JavaScript Linter
This rule can enforce or disallow the use of braces around arrow function body. Options. The rule takes one or two options. The...
Read more >
arrow-body-style error on react useEffect method
The function you pass to useEffect has curly braces, hence the warning based on your ESLint config. Adding curly braces to a different...
Read more >
The add or remove braces from arrow function refactoring
Today's VS Code tip: add or remove braces from arrow functionUse this refactoring in JavaScript and TypeScript to quickly add braces to an ......
Read more >
eslint/eslint - Gitter
Hey everyone, the following mocha test snippet is failing on arrow-body-style ("Unexpected block statement surrounding arrow body.") it('should do a thing', ...
Read more >
Integration with ESLint JavaScript Linter - Codacy | Blog
comma-dangle — disallow or enforce trailing commas (recommended); no-cond-assign ... one-var — require or disallow one variable declaration per function ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found