Skip to content

Commit

Permalink
feat: Add prefer-object-has-own rule.
Browse files Browse the repository at this point in the history
Fixes #14939
  • Loading branch information
Gautam-Arora24 committed Oct 24, 2021
1 parent 2774043 commit daf7f3f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/rules/prefer-object-has-own.md
@@ -0,0 +1,23 @@
# Prefer use of Object.hasOwn over `Object.prototype.hasOwnPrototype` (prefer-object-has-own)

When Object.prototype.hasOwnPrototype.call is used, this rule requires using the `Object.hasOwn` instead. `Object.hasOwn` is a syntactic sugar and makes the code cleaner.

## Rule Details

Examples of **incorrect** code for this rule:

```js
/*eslint prefer-object-has-own: "error"*/
Object.prototype.hasOwnProperty.call(obj, "a");

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

Examples of **correct** code for this rule:

```js
/*eslint prefer-object-has-own: "error"*/

Object.hasOwn(obj, "a");
```
43 changes: 43 additions & 0 deletions lib/rules/prefer-object-has-own.js
@@ -0,0 +1,43 @@
/**
* @fileoverview Prefers Object.hasOwn instead of Object.prototype.hasOwnProperty
* @author Gautam Arora <gautamarora6248@gmail.com>
* See LICENSE file in root directory for full license.
*/

"use strict";

module.exports = {
meta: {
type: "suggestion",
docs: {
description:
"disallow use of Object.prototype.hasOwnProperty and prefer use of Object.hasOwn",
recommended: "false",
url: "https://eslint.org/docs/rules/prefer-object-has-own"
},
schema: [],
messages: {
useHasOwnMessage:
"Use Object.hasOwn instead of Object.prototype.hasOwnProperty."
}
},
create(context) {

// declare the state of the rule
return {
MemberExpression(node) {
if (
node.property.name === "hasOwnProperty" &&
node.object.object.name === "Object"
) {
const messageId = "useHasOwnMessage";

context.report({
messageId,
node
});
}
}
};
}
};
43 changes: 43 additions & 0 deletions tests/lib/rules/prefer-object-has-own.js
@@ -0,0 +1,43 @@
/**
* @fileoverview Prefers Object.hasOwn instead of Object.prototype.hasOwnProperty
* @author Gautam Arora
* See LICENSE file in root directory for full license.
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/prefer-object-has-own");
const { RuleTester } = require("../../../lib/rule-tester");

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

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

const ruleTester = new RuleTester({ parserOptions });

ruleTester.run("prefer-object-has-own", rule, {
valid: [
`
let obj = {};
Object.hasOwn(obj,"");
`
],
invalid: [
`
let a = Object.prototype.hasOwnProperty();
obj.call();
`,
`
let a = Object.prototype.hasOwnProperty.call();
`
]
});

0 comments on commit daf7f3f

Please sign in to comment.