/
no-prototype-builtins.js
91 lines (73 loc) · 2.72 KB
/
no-prototype-builtins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Copyright (c) 2013 ESLint Team
* Released under the MIT license
* https://github.com/eslint/eslint/blob/main/LICENSE
* Portions Copyright (c) 2022 Query Kuma
*
* @fileoverview Rule to disallow use of Object.prototype builtins on objects
* @author Andrew Levine
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
type: "problem",
docs: {
description: "disallow calling some `Object.prototype` methods directly on objects",
recommended: true,
url: "https://eslint.org/docs/rules/no-prototype-builtins"
},
schema: [],
messages: {
prototypeBuildIn: "Do not access Object.prototype method '{{prop}}' from target object."
},
fixable: "code"
},
create(context) {
const DISALLOWED_PROPS = new Set([
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable"
]);
const sourceCode = context.getSourceCode();
/**
* Reports if a disallowed property is used in a CallExpression
* @param {ASTNode} node The CallExpression node.
* @returns {void}
*/
function disallowBuiltIns(node) {
const callee = astUtils.skipChainExpression(node.callee);
if (callee.type !== "MemberExpression") {
return;
}
const propName = astUtils.getStaticPropertyName(callee);
const fix = (fixer) => {
if (propName === "hasOwnProperty") {
var s_arg = sourceCode.getText(node.arguments[0]);
var s_object = sourceCode.getText(node.callee).replace(/\.hasOwnProperty$/u, '');
var s_replace = `Object.prototype.hasOwnProperty.call(${s_object}, ${s_arg})`;
return fixer.replaceText(node, s_replace);
}
};
if (propName !== null && DISALLOWED_PROPS.has(propName)) {
context.report({
messageId: "prototypeBuildIn",
loc: callee.property.loc,
data: { prop: propName },
node,
fix
});
}
}
return {
CallExpression: disallowBuiltIns
};
}
};