2 * @fileoverview Rule to suggest using "Reflect" api over Function/Object methods
3 * @author Keith Cirkel <http://keithcirkel.co.uk>
4 * @deprecated in ESLint v3.9.0
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
17 description: "require `Reflect` methods where applicable",
18 category: "ECMAScript 6",
20 url: "https://eslint.org/docs/rules/prefer-reflect"
39 "getOwnPropertyDescriptor",
43 "getOwnPropertyNames",
50 additionalProperties: false
56 const existingNames = {
57 apply: "Function.prototype.apply",
58 call: "Function.prototype.call",
59 defineProperty: "Object.defineProperty",
60 getOwnPropertyDescriptor: "Object.getOwnPropertyDescriptor",
61 getPrototypeOf: "Object.getPrototypeOf",
62 setPrototypeOf: "Object.setPrototypeOf",
63 isExtensible: "Object.isExtensible",
64 getOwnPropertyNames: "Object.getOwnPropertyNames",
65 preventExtensions: "Object.preventExtensions"
68 const reflectSubsitutes = {
69 apply: "Reflect.apply",
70 call: "Reflect.apply",
71 defineProperty: "Reflect.defineProperty",
72 getOwnPropertyDescriptor: "Reflect.getOwnPropertyDescriptor",
73 getPrototypeOf: "Reflect.getPrototypeOf",
74 setPrototypeOf: "Reflect.setPrototypeOf",
75 isExtensible: "Reflect.isExtensible",
76 getOwnPropertyNames: "Reflect.getOwnPropertyNames",
77 preventExtensions: "Reflect.preventExtensions"
80 const exceptions = (context.options[0] || {}).exceptions || [];
83 * Reports the Reflect violation based on the `existing` and `substitute`
84 * @param {Object} node The node that violates the rule.
85 * @param {string} existing The existing method name that has been used.
86 * @param {string} substitute The Reflect substitute that should be used.
89 function report(node, existing, substitute) {
92 message: "Avoid using {{existing}}, instead use {{substitute}}.",
101 CallExpression(node) {
102 const methodName = (node.callee.property || {}).name;
103 const isReflectCall = (node.callee.object || {}).name === "Reflect";
104 const hasReflectSubsitute = Object.prototype.hasOwnProperty.call(reflectSubsitutes, methodName);
105 const userConfiguredException = exceptions.indexOf(methodName) !== -1;
107 if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
108 report(node, existingNames[methodName], reflectSubsitutes[methodName]);
111 UnaryExpression(node) {
112 const isDeleteOperator = node.operator === "delete";
113 const targetsIdentifier = node.argument.type === "Identifier";
114 const userConfiguredException = exceptions.indexOf("delete") !== -1;
116 if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) {
117 report(node, "the delete keyword", "Reflect.deleteProperty");