Blame view

node_modules/eslint/lib/rules/prefer-reflect.js 4.32 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  /**
   * @fileoverview Rule to suggest using "Reflect" api over Function/Object methods
   * @author Keith Cirkel <http://keithcirkel.co.uk>
   * @deprecated in ESLint v3.9.0
   */
  "use strict";
  
  //------------------------------------------------------------------------------
  // Rule Definition
  //------------------------------------------------------------------------------
  
  module.exports = {
      meta: {
          docs: {
              description: "require `Reflect` methods where applicable",
              category: "ECMAScript 6",
              recommended: false,
              replacedBy: []
          },
  
          deprecated: true,
  
          schema: [
              {
                  type: "object",
                  properties: {
                      exceptions: {
                          type: "array",
                          items: {
                              enum: [
                                  "apply",
                                  "call",
                                  "delete",
                                  "defineProperty",
                                  "getOwnPropertyDescriptor",
                                  "getPrototypeOf",
                                  "setPrototypeOf",
                                  "isExtensible",
                                  "getOwnPropertyNames",
                                  "preventExtensions"
                              ]
                          },
                          uniqueItems: true
                      }
                  },
                  additionalProperties: false
              }
          ]
      },
  
      create(context) {
          const existingNames = {
              apply: "Function.prototype.apply",
              call: "Function.prototype.call",
              defineProperty: "Object.defineProperty",
              getOwnPropertyDescriptor: "Object.getOwnPropertyDescriptor",
              getPrototypeOf: "Object.getPrototypeOf",
              setPrototypeOf: "Object.setPrototypeOf",
              isExtensible: "Object.isExtensible",
              getOwnPropertyNames: "Object.getOwnPropertyNames",
              preventExtensions: "Object.preventExtensions"
          };
  
          const reflectSubsitutes = {
              apply: "Reflect.apply",
              call: "Reflect.apply",
              defineProperty: "Reflect.defineProperty",
              getOwnPropertyDescriptor: "Reflect.getOwnPropertyDescriptor",
              getPrototypeOf: "Reflect.getPrototypeOf",
              setPrototypeOf: "Reflect.setPrototypeOf",
              isExtensible: "Reflect.isExtensible",
              getOwnPropertyNames: "Reflect.getOwnPropertyNames",
              preventExtensions: "Reflect.preventExtensions"
          };
  
          const exceptions = (context.options[0] || {}).exceptions || [];
  
          /**
           * Reports the Reflect violation based on the `existing` and `substitute`
           * @param {Object} node The node that violates the rule.
           * @param {string} existing The existing method name that has been used.
           * @param {string} substitute The Reflect substitute that should be used.
           * @returns {void}
           */
          function report(node, existing, substitute) {
              context.report({ node, message: "Avoid using {{existing}}, instead use {{substitute}}.", data: {
                  existing,
                  substitute
              } });
          }
  
          return {
              CallExpression(node) {
                  const methodName = (node.callee.property || {}).name;
                  const isReflectCall = (node.callee.object || {}).name === "Reflect";
                  const hasReflectSubsitute = reflectSubsitutes.hasOwnProperty(methodName);
                  const userConfiguredException = exceptions.indexOf(methodName) !== -1;
  
                  if (hasReflectSubsitute && !isReflectCall && !userConfiguredException) {
                      report(node, existingNames[methodName], reflectSubsitutes[methodName]);
                  }
              },
              UnaryExpression(node) {
                  const isDeleteOperator = node.operator === "delete";
                  const targetsIdentifier = node.argument.type === "Identifier";
                  const userConfiguredException = exceptions.indexOf("delete") !== -1;
  
                  if (isDeleteOperator && !targetsIdentifier && !userConfiguredException) {
                      report(node, "the delete keyword", "Reflect.deleteProperty");
                  }
              }
          };
  
      }
  };