unicorn/prefer-reflect-apply
Require Reflect.apply() instead of Function.prototype.apply()
has suggestions
handler.apply(this, arguments) looks up apply on handler, and any function can have its own apply property that does something else. The safe spelling is Function.prototype.apply.call(handler, this, arguments), which is hard to read. Reflect.apply(handler, this, arguments) always calls the function and is shorter.
The rule only reports a call when it can tell it's Function.prototype.apply. That means the first argument is this or null, and the second is an array literal or arguments.
--fix doesn't change the code. --fix-suggestions rewrites the call.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/prefer-reflect-apply" = "error" # or "warn"Examples#
example.js2 problems
function logged() {
console.log("called");
return handler.apply(this, arguments)Prefer Reflect.apply() over Function#apply().;
}
Function.prototype.apply.call(handler, null, [event])Prefer Reflect.apply() over Function#apply().;example.jsNo problems
function logged() {
console.log("called");
return Reflect.apply(handler, this, arguments);
}
Reflect.apply(handler, null, [event]);With other arguments, it could be a different method named apply, so these aren't reported:
example.jsNo problems
patch.apply(document, changes);
handler.apply(this, args);