Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add prefer-object-has-own rule.
Fixes #14939
- Loading branch information
1 parent
2774043
commit beb55e3
Showing
3 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
` | ||
] | ||
}); |