1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 |
|
9 |
|
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 | 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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
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 hasReflectSubstitute = Object.prototype.hasOwnProperty.call(reflectSubstitutes, methodName);
|
109 | const userConfiguredException = exceptions.indexOf(methodName) !== -1;
|
110 |
|
111 | if (hasReflectSubstitute && !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 | };
|