Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unnecessary-callback-wrapper: Allow x => x(x) #2524

Conversation

andy-hanson
Copy link
Contributor

@andy-hanson andy-hanson commented Apr 8, 2017

PR checklist

  • Addresses an existing issue: #0000
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

Overview of change:

#2510 is too simple; there are cases of non-identifier calls that are still an unnecessary callback, like x => f()(x). We can solve #2509 by looking for uses of the parameters in the called expression, so that x => x(x) is still not a failure.

CHANGELOG.md entry:

[enhancement] no-unnecessary-callback-wrapper: Allow x => x(x)

@ajafff
Copy link
Contributor

ajafff commented Apr 8, 2017

I disagree with some changes made here. (x) => foo()(x) is no unnecessary wrapper, because foo() could have side effects. It would change the semantics of the program if the callback is never called or called more than once.
Every CallExpression whose expression is not an identifier should be allowed.

+1 for allowing (x) => x(x)

@andy-hanson
Copy link
Contributor Author

That's technically true... but that would be a pretty cruel place to hide a side effect. Most of the time code is better off evaluating foo() only once, instead of once per callback.

@ajafff
Copy link
Contributor

ajafff commented Apr 8, 2017

but that would be a pretty cruel place to hide a side effect. Most of the time code is better off evaluating foo() only once, instead of once per callback.

Agreed, but that's definitely not the responsibility of this rule.

What if the arrow function is not used as callback. It's totally valid to write all functions as arrow function. If that's the case, side effects are expected and it's not just an "unnecessary wrapper"

@andy-hanson
Copy link
Contributor Author

OK, I've reverted to only check for identifiers.

return true;
return isIdentifier(name) && isIdentifier(arg) && name.text === arg.text
// If the invoked expression is one of the parameters, bail.
&& expression.text !== name.text;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the only line that really changed
please consider keeping the diff small and just add this line to the source as it is on master.
Btw. sorry for the rather big refactor and therefore unnecessary big diff in #2510 😕

@adidahiya adidahiya merged commit af624f8 into palantir:master Apr 12, 2017
@adidahiya adidahiya added this to the TSLint v5.2 milestone Apr 12, 2017
@andy-hanson andy-hanson deleted the no-unnecessary-callback-wrapper_identifier branch April 13, 2017 00:37
@andy-hanson andy-hanson changed the title no-unnecessary-callback-wrapper: Handle non-identifier functions no-unnecessary-callback-wrapper: Allow x => x(x) Apr 13, 2017
@andy-hanson
Copy link
Contributor Author

Updated changelog entry to reflect what this PR ended up actually doing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants