796bbdf05fd4464323b0555ad8549512fc5b6a6a
[dotfiles/.git] / prefer-reflect.js
1 /**
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
5  */
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = {
13     meta: {
14         type: "suggestion",
15
16         docs: {
17             description: "require `Reflect` methods where applicable",
18             category: "ECMAScript 6",
19             recommended: false,
20             url: "https://eslint.org/docs/rules/prefer-reflect"
21         },
22
23         deprecated: true,
24
25         replacedBy: [],
26
27         schema: [
28             {
29                 type: "object",
30                 properties: {
31                     exceptions: {
32                         type: "array",
33                         items: {
34                             enum: [
35                                 "apply",
36                                 "call",
37                                 "delete",
38                                 "defineProperty",
39                                 "getOwnPropertyDescriptor",
40                                 "getPrototypeOf",
41                                 "setPrototypeOf",
42                                 "isExtensible",
43                                 "getOwnPropertyNames",
44                                 "preventExtensions"
45                             ]
46                         },
47                         uniqueItems: true
48                     }
49                 },
50                 additionalProperties: false
51             }
52         ]
53     },
54
55     create(context) {
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"
66         };
67
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"
78         };
79
80         const exceptions = (context.options[0] || {}).exceptions || [];
81
82         /**
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.
87          * @returns {void}
88          */
89         function report(node, existing, substitute) {
90             context.report({
91                 node,
92                 message: "Avoid using {{existing}}, instead use {{substitute}}.",
93                 data: {
94                     existing,
95                     substitute
96                 }
97             });
98         }
99
100         return {
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;
106
107                 if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
108                     report(node, existingNames[methodName], reflectSubsitutes[methodName]);
109                 }
110             },
111             UnaryExpression(node) {
112                 const isDeleteOperator = node.operator === "delete";
113                 const targetsIdentifier = node.argument.type === "Identifier";
114                 const userConfiguredException = exceptions.indexOf("delete") !== -1;
115
116                 if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) {
117                     report(node, "the delete keyword", "Reflect.deleteProperty");
118                 }
119             }
120         };
121
122     }
123 };