1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
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 |
|
34 |
|
35 |
|
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 | };
|