UNPKG

1.65 kBJavaScriptView Raw
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
11module.exports = {
12 meta: {
13 docs: {
14 description: "disallow calling some `Object.prototype` methods directly on objects",
15 category: "Possible Errors",
16 recommended: false,
17 url: "https://eslint.org/docs/rules/no-prototype-builtins"
18 },
19
20 schema: []
21 },
22
23 create(context) {
24 const DISALLOWED_PROPS = [
25 "hasOwnProperty",
26 "isPrototypeOf",
27 "propertyIsEnumerable"
28 ];
29
30 /**
31 * Reports if a disallowed property is used in a CallExpression
32 * @param {ASTNode} node The CallExpression node.
33 * @returns {void}
34 */
35 function disallowBuiltIns(node) {
36 if (node.callee.type !== "MemberExpression" || node.callee.computed) {
37 return;
38 }
39 const propName = node.callee.property.name;
40
41 if (DISALLOWED_PROPS.indexOf(propName) > -1) {
42 context.report({
43 message: "Do not access Object.prototype method '{{prop}}' from target object.",
44 loc: node.callee.property.loc.start,
45 data: { prop: propName },
46 node
47 });
48 }
49 }
50
51 return {
52 CallExpression: disallowBuiltIns
53 };
54 }
55};