prefer-reflect
Require Reflect methods instead of their older Object and Function forms
deprecated
Reflect puts the low-level object operations in one place, as plain functions. For example, Reflect.defineProperty() returns false where Object.defineProperty() throws.
The rule reports delete on a property, and every call to a method with one of these names on anything but Reflect: apply, call, defineProperty, getOwnPropertyDescriptor, getPrototypeOf, setPrototypeOf, isExtensible, getOwnPropertyNames, preventExtensions.
Examples#
example.js4 problems
save.apply(user, args)Avoid using Function.prototype.apply, instead use Reflect.apply.;
save.call(user, "draft")Avoid using Function.prototype.call, instead use Reflect.apply.;
Object.defineProperty(user, "id", { value: 1 })Avoid using Object.defineProperty, instead use Reflect.defineProperty.;
delete user.emailAvoid using the delete keyword, instead use Reflect.deleteProperty.;example.jsNo problems
Reflect.apply(save, user, args);
Reflect.apply(save, user, ["draft"]);
Reflect.defineProperty(user, "id", { value: 1 });
Reflect.deleteProperty(user, "email");Options#
| Option | Default | What it does |
|---|---|---|
exceptions | [] | Names to allow: any of the method names above, or "delete". |
example.jswith
{ "exceptions": ["delete", "call"] }No problemsdelete user.email;
save.call(user, "draft");example.jswith
{ "exceptions": ["delete", "call"] }1 problemsave.apply(user, args)Avoid using Function.prototype.apply, instead use Reflect.apply.;When not to use it#
ESLint has deprecated this rule, and the older forms aren't going away. The rule only goes by the method's name, so phone.call(number) is reported. For Object.getOwnPropertyNames it points you to Reflect.getOwnPropertyNames, which doesn't exist.