]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/prefer-object-has-own.js
2 * @fileoverview Prefers Object.hasOwn() instead of Object.prototype.hasOwnProperty.call()
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 const astUtils
= require("./utils/ast-utils");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
20 * Checks if the given node is considered to be an access to a property of `Object.prototype`.
21 * @param {ASTNode} node `MemberExpression` node to evaluate.
22 * @returns {boolean} `true` if `node.object` is `Object`, `Object.prototype`, or `{}` (empty 'ObjectExpression' node).
24 function hasLeftHandObject(node
) {
27 * ({}).hasOwnProperty.call(obj, prop) - `true`
28 * ({ foo }.hasOwnProperty.call(obj, prop)) - `false`, object literal should be empty
30 if (node
.object
.type
=== "ObjectExpression" && node
.object
.properties
.length
=== 0) {
34 const objectNodeToCheck
= node
.object
.type
=== "MemberExpression" && astUtils
.getStaticPropertyName(node
.object
) === "prototype" ? node
.object
.object
: node
.object
;
36 if (objectNodeToCheck
.type
=== "Identifier" && objectNodeToCheck
.name
=== "Object") {
43 //------------------------------------------------------------------------------
45 //------------------------------------------------------------------------------
47 /** @type {import('../shared/types').Rule} */
53 "Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`",
55 url
: "https://eslint.org/docs/latest/rules/prefer-object-has-own"
59 useHasOwn
: "Use 'Object.hasOwn()' instead of 'Object.prototype.hasOwnProperty.call()'."
65 const sourceCode
= context
.sourceCode
;
68 CallExpression(node
) {
69 if (!(node
.callee
.type
=== "MemberExpression" && node
.callee
.object
.type
=== "MemberExpression")) {
73 const calleePropertyName
= astUtils
.getStaticPropertyName(node
.callee
);
74 const objectPropertyName
= astUtils
.getStaticPropertyName(node
.callee
.object
);
75 const isObject
= hasLeftHandObject(node
.callee
.object
);
77 // check `Object` scope
78 const scope
= sourceCode
.getScope(node
);
79 const variable
= astUtils
.getVariableByName(scope
, "Object");
82 calleePropertyName
=== "call" &&
83 objectPropertyName
=== "hasOwnProperty" &&
85 variable
&& variable
.scope
.type
=== "global"
89 messageId
: "useHasOwn",
92 if (sourceCode
.getCommentsInside(node
.callee
).length
> 0) {
96 const tokenJustBeforeNode
= sourceCode
.getTokenBefore(node
.callee
, { includeComments
: true });
98 // for https://github.com/eslint/eslint/pull/15346#issuecomment-991417335
100 tokenJustBeforeNode
&&
101 tokenJustBeforeNode
.range
[1] === node
.callee
.range
[0] &&
102 !astUtils
.canTokensBeAdjacent(tokenJustBeforeNode
, "Object.hasOwn")
104 return fixer
.replaceText(node
.callee
, " Object.hasOwn");
107 return fixer
.replaceText(node
.callee
, "Object.hasOwn");