Skip to content

Commit 87e0a08

Browse files
authored
fix: prefer-object-has-own autofix breaks when Object is shadowed (#21282)
1 parent 3c753f1 commit 87e0a08

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎lib/rules/prefer-object-has-own.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ module.exports = {
9999
objectPropertyName === "hasOwnProperty" &&
100100
isObject &&
101101
variable &&
102-
variable.scope.type === "global"
102+
variable.scope.type === "global" &&
103+
variable.defs.length === 0
103104
) {
104105
context.report({
105106
node,

‎tests/lib/rules/prefer-object-has-own.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ ruleTester.run("prefer-object-has-own", rule, {
8888
"const hasProperty = Object.hasOwn(object, property);",
8989
`/* global Object: off */
9090
({}).hasOwnProperty.call(a, b);`,
91+
{
92+
// Object is shadowed
93+
code: "var Object = foo;\nObject.prototype.hasOwnProperty.call(obj, prop);",
94+
languageOptions: { sourceType: "script" },
95+
},
96+
{
97+
// Object is shadowed
98+
code: "var Object = foo;\n({}).hasOwnProperty.call(obj, prop);",
99+
languageOptions: { sourceType: "script" },
100+
},
91101
],
92102
invalid: [
93103
{

0 commit comments

Comments
 (0)