Skip to content

Commit

Permalink
Update: Follow up commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gautam-Arora24 committed Oct 28, 2021
1 parent 47795fc commit 7f15375
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
7 changes: 6 additions & 1 deletion docs/rules/prefer-object-has-own.md
Expand Up @@ -10,8 +10,9 @@ Examples of **incorrect** code for this rule:
/*eslint prefer-object-has-own: "error"*/
Object.prototype.hasOwnProperty.call(obj, "a");

({}).hasOwnProperty(obj,"a");

let a = Object.prototype.hasOwnProperty;
a.call(obj, "a");
```

Examples of **correct** code for this rule:
Expand All @@ -21,3 +22,7 @@ Examples of **correct** code for this rule:

Object.hasOwn(obj, "a");
```

## Related Material

[MDN Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwn)
1 change: 1 addition & 0 deletions lib/rules/index.js
Expand Up @@ -255,6 +255,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
"prefer-exponentiation-operator": () => require("./prefer-exponentiation-operator"),
"prefer-named-capture-group": () => require("./prefer-named-capture-group"),
"prefer-numeric-literals": () => require("./prefer-numeric-literals"),
"prefer-object-has-own": () => require("./prefer-object-has-own"),
"prefer-object-spread": () => require("./prefer-object-spread"),
"prefer-promise-reject-errors": () => require("./prefer-promise-reject-errors"),
"prefer-reflect": () => require("./prefer-reflect"),
Expand Down
36 changes: 26 additions & 10 deletions lib/rules/prefer-object-has-own.js
Expand Up @@ -6,6 +6,22 @@

"use strict";

/**
* Checks to see if a property name object exists in the subtree recursively.
* @param {node} node to evalutate.
* @returns {boolean} `True` if object property exists, false otherwise.
*/
function checkForObject(node) {
if (!node.object) {
return false;
}
if (node.object.name === "Object") {
return true;
}
return checkForObject(node.object);
}


module.exports = {
meta: {
type: "suggestion",
Expand All @@ -17,24 +33,24 @@ module.exports = {
},
schema: [],
messages: {
useHasOwnMessage:
"Use Object.hasOwn instead of Object.prototype.hasOwnProperty."
useHasOwn: "Prefer using hasOwn property instead of hasOwnProperty."
}
},
create(context) {

// declare the state of the rule
return {
MemberExpression(node) {
const propertyName = node.property.name;
const isObject = checkForObject(node);
const isObjectExpression =
node.object.type === "ObjectExpression";

if (
node.property.name === "hasOwnProperty" &&
node.object.object.name === "Object"
propertyName === "hasOwnProperty" &&
(isObject || isObjectExpression)
) {
const messageId = "useHasOwnMessage";

context.report({
messageId,
node
node,
messageId: "useHasOwn"
});
}
}
Expand Down
27 changes: 18 additions & 9 deletions tests/lib/rules/prefer-object-has-own.js
Expand Up @@ -18,11 +18,11 @@ const { RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------

const parserOptions = {
ecmaVersion: 2018,
sourceType: "module"
ecmaVersion: 2022
};

const ruleTester = new RuleTester({ parserOptions });
const error = { messageId: "useHasOwn" };

ruleTester.run("prefer-object-has-own", rule, {
valid: [
Expand All @@ -32,12 +32,21 @@ ruleTester.run("prefer-object-has-own", rule, {
`
],
invalid: [
`
let a = Object.prototype.hasOwnProperty();
obj.call();
`,
`
let a = Object.prototype.hasOwnProperty.call();
`
{
code: "Object.prototype.hasOwnProperty",
errors: [error]
},
{
code: "Object.hasOwnProperty.call(obj, 'foo')",
errors: [error]
},
{
code: "Object.prototype.hasOwnProperty.call(obj, 'foo')",
errors: [error]
},
{
code: "({}).hasOwnProperty.call(obj, 'foo')",
errors: [error]
}
]
});
1 change: 1 addition & 0 deletions tools/rule-types.json
Expand Up @@ -242,6 +242,7 @@
"prefer-exponentiation-operator": "suggestion",
"prefer-named-capture-group": "suggestion",
"prefer-numeric-literals": "suggestion",
"prefer-object-has-own":"suggestion",
"prefer-object-spread": "suggestion",
"prefer-promise-reject-errors": "suggestion",
"prefer-reflect": "suggestion",
Expand Down

0 comments on commit 7f15375

Please sign in to comment.