• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
12module.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        messages: {
55            preferReflect: "Avoid using {{existing}}, instead use {{substitute}}."
56        }
57    },
58
59    create(context) {
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"
70        };
71
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"
82        };
83
84        const exceptions = (context.options[0] || {}).exceptions || [];
85
86        /**
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.
91         * @returns {void}
92         */
93        function report(node, existing, substitute) {
94            context.report({
95                node,
96                messageId: "preferReflect",
97                data: {
98                    existing,
99                    substitute
100                }
101            });
102        }
103
104        return {
105            CallExpression(node) {
106                const methodName = (node.callee.property || {}).name;
107                const isReflectCall = (node.callee.object || {}).name === "Reflect";
108                const hasReflectSubsitute = Object.prototype.hasOwnProperty.call(reflectSubstitutes, methodName);
109                const userConfiguredException = exceptions.indexOf(methodName) !== -1;
110
111                if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
112                    report(node, existingNames[methodName], reflectSubstitutes[methodName]);
113                }
114            },
115            UnaryExpression(node) {
116                const isDeleteOperator = node.operator === "delete";
117                const targetsIdentifier = node.argument.type === "Identifier";
118                const userConfiguredException = exceptions.indexOf("delete") !== -1;
119
120                if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) {
121                    report(node, "the delete keyword", "Reflect.deleteProperty");
122                }
123            }
124        };
125
126    }
127};
128