-
Notifications
You must be signed in to change notification settings - Fork 889
only-arrow-functions rule should allow named functions #1449
Comments
I can take a look at this within a day or two. |
LGTM =) |
Is there a benefit here to use a named function as opposed to |
Hmm. I'm not sure. 😟 |
For example, we'd want the rule to prevent the error case below:
I'm inclined not to add the extra complexity of an |
@jkillian The very-popular AirBnb style uses a setting like this: |
You can get some performance benefits to not having to create a new lambda to preserve scope. Consider the case of a library that allows execution of callbacks with scope: class Executor {
private callback: Function;
private scope: any;
private args: any[];
public setCallback(callback: Function, args?: any[]): void {
this.callback = callback;
this.args = args;
}
public setCallbackBound(callback: Function, scope?: any, args?: any[]): void {
this.callback = callback;
this.scope = scope;
this.args = args;
}
public execute(): void {
this.callback.apply(this.scope, this.args);
}
} In using executor.setCallback(() => this.doStuff());
// ...
executor.execute(); In using executor.setCallbackBound(this.doStuff, this);
// ...
executor.execute(); Because arrow lambdas capture scope you can't use them for places like this where scope is flexible. |
@JoshuaKGoldberg you don't have to turn the rule on if it doesn't make sense for your application! In general, if you're using a library (e.g. jQuery) that sets |
@danvk true! I see libraries like that as a reason why it could be useful to add flexibility to the rule. |
Indeed! Naming the callback seems like a reasonable enough escape hatch from this rule. |
Okay, I'm good with this, full-steam ahead on the PR 🚢 |
Bug Report
3.14.0
1.8.10
TypeScript code being linted
with
tslint.json
configuration:Actual behavior
non-arrow functions are forbidden
Expected behavior
Named functions should be allowed.
The text was updated successfully, but these errors were encountered: