From 6ff5f7d1506bd3908f722657bcdc3070a1087eec Mon Sep 17 00:00:00 2001 From: Gautam Arora Date: Sun, 24 Oct 2021 11:54:11 +0530 Subject: [PATCH] New: Add prefer-object-has-own rule. Fixes #14939 --- docs/rules/prefer-object-has-own.md | 23 +++++++++++++ lib/rules/prefer-object-has-own.js | 43 ++++++++++++++++++++++++ tests/lib/rules/prefer-object-has-own.js | 43 ++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 docs/rules/prefer-object-has-own.md create mode 100644 lib/rules/prefer-object-has-own.js create mode 100644 tests/lib/rules/prefer-object-has-own.js diff --git a/docs/rules/prefer-object-has-own.md b/docs/rules/prefer-object-has-own.md new file mode 100644 index 00000000000..f8a6f3dd957 --- /dev/null +++ b/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"); +``` diff --git a/lib/rules/prefer-object-has-own.js b/lib/rules/prefer-object-has-own.js new file mode 100644 index 00000000000..437c64603e0 --- /dev/null +++ b/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"; + +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 + }); + } + } + }; + } +}; diff --git a/tests/lib/rules/prefer-object-has-own.js b/tests/lib/rules/prefer-object-has-own.js new file mode 100644 index 00000000000..8b18e1c200a --- /dev/null +++ b/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(); + ` + ] +});