Suggest using Reflect methods where applicable (prefer-reflect)

The ES6 Reflect API comes with a handful of methods which somewhat deprecate methods on old constructors:

The prefer-reflect rule will flag usage of any older method, suggesting to instead use the newer Reflect version.

Rule Details

Reflect.apply (Function.prototype.apply/Function.prototype.call)

The following patterns are considered warnings:

config: prefer-reflect: [2]

foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);
obj.foo.apply(other, args);

foo.call(undefined, arg);
foo.call(null, arg);
obj.foo.call(obj, arg);
obj.foo.call(other, arg);

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.apply(undefined, args);
Reflect.apply(null, args);
Reflect.apply(obj.foo, obj, args);
Reflect.apply(obj.foo, other, args);
Reflect.apply(undefined, [arg]);
Reflect.apply(null, [arg]);
Reflect.apply(obj.foo, obj, [arg]);
Reflect.apply(obj.foo, other, [arg]);

config: prefer-reflect: [2, { exceptions: ["apply"] }]

foo.apply(undefined, args);
foo.apply(null, args);
obj.foo.apply(obj, args);
obj.foo.apply(other, args);
Reflect.apply(undefined, args);
Reflect.apply(null, args);
Reflect.apply(obj.foo, obj, args);
Reflect.apply(obj.foo, other, args);

config: prefer-reflect: [2, { exceptions: ["call"] }]

foo.call(undefined, arg);
foo.call(null, arg);
obj.foo.call(obj, arg);
obj.foo.call(other, arg);
Reflect.apply(undefined, [arg]);
Reflect.apply(null, [arg]);
Reflect.apply(obj.foo, obj, [arg]);
Reflect.apply(obj.foo, other, [arg]);

Reflect.defineProperty (Object.defineProperty)

The following patterns are considered warnings:

config: prefer-reflect: [2]

Object.defineProperty({}, 'foo', {value: 1})

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.defineProperty({}, 'foo', {value: 1})

config: prefer-reflect: [2, { exceptions: ["defineProperty"] }]

Object.defineProperty({}, 'foo', {value: 1})
Reflect.defineProperty({}, 'foo', {value: 1})

Reflect.getOwnPropertyDescriptor (Object.getOwnPropertyDescriptor)

The following patterns are considered warnings:

config: prefer-reflect: [2]

Object.getOwnPropertyDescriptor({}, 'foo')

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.getOwnPropertyDescriptor({}, 'foo')

config: prefer-reflect: [2, { exceptions: ["getOwnPropertyDescriptor"] }]

Object.getOwnPropertyDescriptor({}, 'foo')
Reflect.getOwnPropertyDescriptor({}, 'foo')

Reflect.getPrototypeOf (Object.getPrototypeOf)

The following patterns are considered warnings:

config: prefer-reflect: [2]

Object.getPrototypeOf({}, 'foo')

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.getPrototypeOf({}, 'foo')

config: prefer-reflect: [2, { exceptions: ["getPrototypeOf"] }]

Object.getPrototypeOf({}, 'foo')
Reflect.getPrototypeOf({}, 'foo')

Reflect.setPrototypeOf (Object.setPrototypeOf)

The following patterns are considered warnings:

config: prefer-reflect: [2]

Object.setPrototypeOf({}, Object.prototype)

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.setPrototypeOf({}, Object.prototype)

config: prefer-reflect: [2, { exceptions: ["setPrototypeOf"] }]

Object.setPrototypeOf({}, Object.prototype)
Reflect.setPrototypeOf({}, Object.prototype)

Reflect.isExtensible (Object.isExtensible)

The following patterns are considered warnings:

config: prefer-reflect: [2]

Object.isExtensible({})

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.isExtensible({})

config: prefer-reflect: [2, { exceptions: ["isExtensible"] }]

Object.isExtensible({})
Reflect.isExtensible({})

Reflect.getOwnPropertyNames (Object.getOwnPropertyNames)

The following patterns are considered warnings:

config: prefer-reflect: [2]

Object.getOwnPropertyNames({})

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.getOwnPropertyNames({})

config: prefer-reflect: [2, { exceptions: ["getOwnPropertyNames"] }]

Object.getOwnPropertyNames({})
Reflect.getOwnPropertyNames({})

Reflect.preventExtensions (Object.preventExtensions)

The following patterns are considered warnings:

config: prefer-reflect: [2]

Object.preventExtensions({})

The following patterns are not considered warnings:

config: prefer-reflect: [2]

Reflect.preventExtensions({})

config: prefer-reflect: [2, { exceptions: ["preventExtensions"] }]

Object.preventExtensions({})
Reflect.preventExtensions({})

Reflect.deleteProperty (The delete keyword)

The following patterns are considered warnings:

config: prefer-reflect: [2]

delete foo.bar;

The following patterns are not considered warnings:

config: prefer-reflect: [2]

delete bar; // Does not reference an object, just a var
Reflect.deleteProperty(foo, 'bar');

(Note: For a rule preventing deletion of variables, see no-delete-var instead)

config: prefer-reflect: [2, { exceptions: ["delete"] }]

delete bar
delete foo.bar
Reflect.deleteProperty(foo, 'bar');

Options

Exceptions

"prefer-reflect": [<enabled>, { exceptions: [<...exceptions>] }]

The exceptions option allows you to pass an array of methods names you'd like to continue to use in the old style.

For example if you wish to use all Reflect methods, except for Function.prototype.apply then your config would look like prefer-reflect: [2, { exceptions: ["apply"] }].

If you want to use Reflect methods, but keep using the delete keyword, then your config would look like prefer-reflect: [2, { exceptions: ["delete"] }].

These can be combined as much as you like. To make all methods exceptions (thereby rendering this rule useless), use prefer-reflect: [2, { exceptions: ["apply", "call", "defineProperty", "getOwnPropertyDescriptor", "getPrototypeOf", "setPrototypeOf", "isExtensible", "getOwnPropertyNames", "preventExtensions", "delete"] }]

When Not to Use It

This rule should not be used in ES3/5 environments.

In ES2015 (ES6) or later, if you don't want to be notified about places where Reflect could be used, you can safely disable this rule.

Version

This rule was introduced in ESLint 1.0.0-rc-2.

Resources