UNPKG

4.56 kBJavaScriptView Raw
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 docs: {
15 description: "require `Reflect` methods where applicable",
16 category: "ECMAScript 6",
17 recommended: false,
18 replacedBy: [],
19 url: "https://eslint.org/docs/rules/prefer-reflect"
20 },
21
22 deprecated: true,
23
24 schema: [
25 {
26 type: "object",
27 properties: {
28 exceptions: {
29 type: "array",
30 items: {
31 enum: [
32 "apply",
33 "call",
34 "delete",
35 "defineProperty",
36 "getOwnPropertyDescriptor",
37 "getPrototypeOf",
38 "setPrototypeOf",
39 "isExtensible",
40 "getOwnPropertyNames",
41 "preventExtensions"
42 ]
43 },
44 uniqueItems: true
45 }
46 },
47 additionalProperties: false
48 }
49 ]
50 },
51
52 create(context) {
53 const existingNames = {
54 apply: "Function.prototype.apply",
55 call: "Function.prototype.call",
56 defineProperty: "Object.defineProperty",
57 getOwnPropertyDescriptor: "Object.getOwnPropertyDescriptor",
58 getPrototypeOf: "Object.getPrototypeOf",
59 setPrototypeOf: "Object.setPrototypeOf",
60 isExtensible: "Object.isExtensible",
61 getOwnPropertyNames: "Object.getOwnPropertyNames",
62 preventExtensions: "Object.preventExtensions"
63 };
64
65 const reflectSubsitutes = {
66 apply: "Reflect.apply",
67 call: "Reflect.apply",
68 defineProperty: "Reflect.defineProperty",
69 getOwnPropertyDescriptor: "Reflect.getOwnPropertyDescriptor",
70 getPrototypeOf: "Reflect.getPrototypeOf",
71 setPrototypeOf: "Reflect.setPrototypeOf",
72 isExtensible: "Reflect.isExtensible",
73 getOwnPropertyNames: "Reflect.getOwnPropertyNames",
74 preventExtensions: "Reflect.preventExtensions"
75 };
76
77 const exceptions = (context.options[0] || {}).exceptions || [];
78
79 /**
80 * Reports the Reflect violation based on the `existing` and `substitute`
81 * @param {Object} node The node that violates the rule.
82 * @param {string} existing The existing method name that has been used.
83 * @param {string} substitute The Reflect substitute that should be used.
84 * @returns {void}
85 */
86 function report(node, existing, substitute) {
87 context.report({
88 node,
89 message: "Avoid using {{existing}}, instead use {{substitute}}.",
90 data: {
91 existing,
92 substitute
93 }
94 });
95 }
96
97 return {
98 CallExpression(node) {
99 const methodName = (node.callee.property || {}).name;
100 const isReflectCall = (node.callee.object || {}).name === "Reflect";
101 const hasReflectSubsitute = reflectSubsitutes.hasOwnProperty(methodName);
102 const userConfiguredException = exceptions.indexOf(methodName) !== -1;
103
104 if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
105 report(node, existingNames[methodName], reflectSubsitutes[methodName]);
106 }
107 },
108 UnaryExpression(node) {
109 const isDeleteOperator = node.operator === "delete";
110 const targetsIdentifier = node.argument.type === "Identifier";
111 const userConfiguredException = exceptions.indexOf("delete") !== -1;
112
113 if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) {
114 report(node, "the delete keyword", "Reflect.deleteProperty");
115 }
116 }
117 };
118
119 }
120};