]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-prototype-builtins.js
first commit
[pve-eslint.git] / eslint / lib / rules / 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 messages: {
25 prototypeBuildIn: "Do not access Object.prototype method '{{prop}}' from target object."
26 }
27 },
28
29 create(context) {
30 const DISALLOWED_PROPS = [
31 "hasOwnProperty",
32 "isPrototypeOf",
33 "propertyIsEnumerable"
34 ];
35
36 /**
37 * Reports if a disallowed property is used in a CallExpression
38 * @param {ASTNode} node The CallExpression node.
39 * @returns {void}
40 */
41 function disallowBuiltIns(node) {
42 if (node.callee.type !== "MemberExpression" || node.callee.computed) {
43 return;
44 }
45 const propName = node.callee.property.name;
46
47 if (DISALLOWED_PROPS.indexOf(propName) > -1) {
48 context.report({
49 messageId: "prototypeBuildIn",
50 loc: node.callee.property.loc,
51 data: { prop: propName },
52 node
53 });
54 }
55 }
56
57 return {
58 CallExpression: disallowBuiltIns
59 };
60 }
61 };