|
| 1 | +/** |
| 2 | + * @fileoverview Prefers Object.hasOwn() instead of Object.prototype.hasOwnProperty.call() |
| 3 | + * @author Nitin Kumar |
| 4 | + * @author Gautam Arora |
| 5 | + */ |
| 6 | + |
| 7 | +"use strict"; |
| 8 | + |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | +// Requirements |
| 11 | +//------------------------------------------------------------------------------ |
| 12 | + |
| 13 | +const astUtils = require("./utils/ast-utils"); |
| 14 | + |
| 15 | +/** |
| 16 | + * Checks if the given node is considered to be an access to a property of `Object.prototype`. |
| 17 | + * @param {ASTNode} node `MemberExpression` node to evaluate. |
| 18 | + * @returns {boolean} `true` if `node.object` is `Object`, `Object.prototype`, or `{}` (empty 'ObjectExpression' node). |
| 19 | + */ |
| 20 | +function hasLeftHandObject(node) { |
| 21 | + |
| 22 | + /* |
| 23 | + * ({}).hasOwnProperty.call(obj, prop) - `true` |
| 24 | + * ({ foo }.hasOwnProperty.call(obj, prop)) - `false`, object literal should be empty |
| 25 | + */ |
| 26 | + if (node.object.type === "ObjectExpression" && node.object.properties.length === 0) { |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + const objectNodeToCheck = node.object.type === "MemberExpression" && astUtils.getStaticPropertyName(node.object) === "prototype" ? node.object.object : node.object; |
| 31 | + |
| 32 | + if (objectNodeToCheck.type === "Identifier" && objectNodeToCheck.name === "Object") { |
| 33 | + return true; |
| 34 | + } |
| 35 | + |
| 36 | + return false; |
| 37 | +} |
| 38 | + |
| 39 | +//------------------------------------------------------------------------------ |
| 40 | +// Rule Definition |
| 41 | +//------------------------------------------------------------------------------ |
| 42 | + |
| 43 | +/** @type {import('../shared/types').Rule} */ |
| 44 | +module.exports = { |
| 45 | + meta: { |
| 46 | + type: "suggestion", |
| 47 | + docs: { |
| 48 | + description: |
| 49 | + "disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`", |
| 50 | + recommended: false, |
| 51 | + url: "https://eslint.org/docs/rules/prefer-object-has-own" |
| 52 | + }, |
| 53 | + schema: [], |
| 54 | + messages: { |
| 55 | + useHasOwn: "Use 'Object.hasOwn()' instead of 'Object.prototype.hasOwnProperty.call()'." |
| 56 | + }, |
| 57 | + fixable: "code" |
| 58 | + }, |
| 59 | + create(context) { |
| 60 | + return { |
| 61 | + CallExpression(node) { |
| 62 | + if (!(node.callee.type === "MemberExpression" && node.callee.object.type === "MemberExpression")) { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + const calleePropertyName = astUtils.getStaticPropertyName(node.callee); |
| 67 | + const objectPropertyName = astUtils.getStaticPropertyName(node.callee.object); |
| 68 | + const isObject = hasLeftHandObject(node.callee.object); |
| 69 | + |
| 70 | + // check `Object` scope |
| 71 | + const scope = context.getScope(); |
| 72 | + const variable = astUtils.getVariableByName(scope, "Object"); |
| 73 | + |
| 74 | + if ( |
| 75 | + calleePropertyName === "call" && |
| 76 | + objectPropertyName === "hasOwnProperty" && |
| 77 | + isObject && |
| 78 | + variable && variable.scope.type === "global" |
| 79 | + ) { |
| 80 | + context.report({ |
| 81 | + node, |
| 82 | + messageId: "useHasOwn", |
| 83 | + fix(fixer) { |
| 84 | + const sourceCode = context.getSourceCode(); |
| 85 | + |
| 86 | + if (sourceCode.getCommentsInside(node.callee).length > 0) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + return fixer.replaceText(node.callee, "Object.hasOwn"); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | + } |
| 95 | + }; |
| 96 | + } |
| 97 | +}; |
0 commit comments