1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | module.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 |
|
32 |
|
33 |
|
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 | };
|