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
55 preferReflect: "Avoid using {{existing}}, instead use {{substitute}}."
60 const existingNames = {
61 apply: "Function.prototype.apply",
62 call: "Function.prototype.call",
63 defineProperty: "Object.defineProperty",
64 getOwnPropertyDescriptor: "Object.getOwnPropertyDescriptor",
65 getPrototypeOf: "Object.getPrototypeOf",
66 setPrototypeOf: "Object.setPrototypeOf",
67 isExtensible: "Object.isExtensible",
68 getOwnPropertyNames: "Object.getOwnPropertyNames",
69 preventExtensions: "Object.preventExtensions"
72 const reflectSubstitutes = {
73 apply: "Reflect.apply",
74 call: "Reflect.apply",
75 defineProperty: "Reflect.defineProperty",
76 getOwnPropertyDescriptor: "Reflect.getOwnPropertyDescriptor",
77 getPrototypeOf: "Reflect.getPrototypeOf",
78 setPrototypeOf: "Reflect.setPrototypeOf",
79 isExtensible: "Reflect.isExtensible",
80 getOwnPropertyNames: "Reflect.getOwnPropertyNames",
81 preventExtensions: "Reflect.preventExtensions"
84 const exceptions = (context.options[0] || {}).exceptions || [];
87 * Reports the Reflect violation based on the `existing` and `substitute`
88 * @param {Object} node The node that violates the rule.
89 * @param {string} existing The existing method name that has been used.
90 * @param {string} substitute The Reflect substitute that should be used.
93 function report(node, existing, substitute) {
96 messageId: "preferReflect",
105 CallExpression(node) {
106 const methodName = (node.callee.property || {}).name;
107 const isReflectCall = (node.callee.object || {}).name === "Reflect";
108 const hasReflectSubstitute = Object.prototype.hasOwnProperty.call(reflectSubstitutes, methodName);
109 const userConfiguredException = exceptions.indexOf(methodName) !== -1;
111 if (hasReflectSubstitute && !isReflectCall && !userConfiguredException) {
112 report(node, existingNames[methodName], reflectSubstitutes[methodName]);
115 UnaryExpression(node) {
116 const isDeleteOperator = node.operator === "delete";
117 const targetsIdentifier = node.argument.type === "Identifier";
118 const userConfiguredException = exceptions.indexOf("delete") !== -1;
120 if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) {
121 report(node, "the delete keyword", "Reflect.deleteProperty");