87a760156e369337285d246e0c6f60a630524c91
[dotfiles/.git] / no-prototype-builtins.js
1 /**
2  * @fileoverview Rule to disallow use of Object.prototype builtins on objects
3  * @author Andrew Levine
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 module.exports = {
12     meta: {
13         type: "problem",
14
15         docs: {
16             description: "disallow calling some `Object.prototype` methods directly on objects",
17             category: "Possible Errors",
18             recommended: true,
19             url: "https://eslint.org/docs/rules/no-prototype-builtins"
20         },
21
22         schema: []
23     },
24
25     create(context) {
26         const DISALLOWED_PROPS = [
27             "hasOwnProperty",
28             "isPrototypeOf",
29             "propertyIsEnumerable"
30         ];
31
32         /**
33          * Reports if a disallowed property is used in a CallExpression
34          * @param {ASTNode} node The CallExpression node.
35          * @returns {void}
36          */
37         function disallowBuiltIns(node) {
38             if (node.callee.type !== "MemberExpression" || node.callee.computed) {
39                 return;
40             }
41             const propName = node.callee.property.name;
42
43             if (DISALLOWED_PROPS.indexOf(propName) > -1) {
44                 context.report({
45                     message: "Do not access Object.prototype method '{{prop}}' from target object.",
46                     loc: node.callee.property.loc.start,
47                     data: { prop: propName },
48                     node
49                 });
50             }
51         }
52
53         return {
54             CallExpression: disallowBuiltIns
55         };
56     }
57 };