eslint-plugin-lodash
eslint-plugin-lodash copied to clipboard
feature: prefer-invoke
In many places there are usages of _.isFunction before running a certain function. _.invoke does exactly that. The rule will prefer using _.invoke instead of checking if something is a function before executing it
Examples
Bad
// This is bad
if (_.isFunction(obj.foo)) {
obj.foo(a, b, c)
}
Good
// This is good
_.invoke(obj, 'foo', a, b, c)
When using !_isFunction in order to raise an exception, the rule should pass
// this is alright
if (!_.isFunction(obj, 'foo') {
throw new Error('foo is not a function')
}
@nirnaor looks OK, feel free to open a PR. Things to make sure:
- The function/method call is the only statement in the
ifblock - This should warn both on function calls and method calls (e.g. both
f(a, b, c)andx.f(a, b, c)
Hey @ganimomer Just making sure that I understood your notes:
First note:
Good
// This is good.
if(_.isFunction(obj.foo) {
obj.foo();
console.log('calling foo')
}
Second note
Bad
var f = () => console.log('i am a function')
// This is bad.
if(_.isFunction(f)){
f()
}
Exactly. (Also, you can use these examples in the docs)
Also
// This is bad.
if(shouldRender && _.isFunction(obj.render){
obj.render()
}
// While this is good.
if(shouldRender{
_.invoke(obj, 'render');
}
Yeah, I agree. However, the order here matters. This:
if (_.isFunction(f) && someHeavyCalculationThatTakesAWhile(...someArguments)) {
f()
}
Is not the same as
if (someHeavyCalculationThatTakesAWhile(...someArguments)) {
_.invoke(f);
}
Especially if this code runs very often.
Also, make sure to check this bad case as well:
if (shouldRender && !shouldSkip && _.isFunction(f)) {
f();
}