Skip to content

Commit db6558f

Browse files
authored
fix(linter): false positive in eslint/prefer-object-has-own (#7463)
closes #7450
1 parent a3ecbde commit db6558f

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

crates/oxc_linter/src/rules/eslint/prefer_object_has_own.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,12 @@ impl Rule for PreferObjectHasOwn {
8787
ctx.diagnostic(diagnostic);
8888
} else {
8989
ctx.diagnostic_with_fix(diagnostic, |fixer| {
90-
let before_token_of_replace_span = if replace_target_span.start > 1 {
91-
Some(ctx.source_range(Span::new(
92-
replace_target_span.start - 1,
93-
replace_target_span.start,
94-
)))
95-
} else {
96-
None
97-
};
98-
let replacement = match before_token_of_replace_span {
99-
Some(token) => match token {
100-
" " | "=" | "/" | "(" => "Object.hasOwn",
101-
_ => " Object.hasOwn",
102-
},
103-
_ => "Object.hasOwn",
104-
};
90+
let needs_space = replace_target_span.start > 1
91+
&& !ctx
92+
.source_range(Span::new(0, replace_target_span.start))
93+
.ends_with(&[' ', '=', '/', '(']);
94+
95+
let replacement = if needs_space { " Object.hasOwn" } else { "Object.hasOwn" };
10596
fixer.replace(replace_target_span, replacement)
10697
});
10798
}
@@ -238,6 +229,8 @@ fn test() {
238229
"Object[`hasOwnProperty`][`call`](object, property);",
239230
"({})['hasOwnProperty']['call'](object, property);",
240231
"({})[`hasOwnProperty`][`call`](object, property);",
232+
// Issue: <https://github.com/oxc-project/oxc/issues/7450>
233+
"…Object.prototype.hasOwnProperty.call(C,x);",
241234
];
242235

243236
let fix = vec![
@@ -385,6 +378,8 @@ fn test() {
385378
"Object.hasOwn(object, property);",
386379
None,
387380
),
381+
// Issue: <https://github.com/oxc-project/oxc/issues/7450>
382+
("…Object.prototype.hasOwnProperty.call(C,x);", "… Object.hasOwn(C,x);", None),
388383
];
389384
Tester::new(PreferObjectHasOwn::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
390385
}

crates/oxc_linter/src/snapshots/prefer_object_has_own.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,10 @@ snapshot_kind: text
231231
· ────────────────────────────────────────────────
232232
╰────
233233
help: Replace `({})[`hasOwnProperty`][`call`]` with `Object.hasOwn`.
234+
235+
eslint(prefer-object-has-own): Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`.
236+
╭─[prefer_object_has_own.tsx:1:3]
237+
1 │ …Object.prototype.hasOwnProperty.call(C,x);
238+
· ─────────────────────────────────────────
239+
╰────
240+
help: Replace `Object.prototype.hasOwnProperty.call` with ` Object.hasOwn`.

0 commit comments

Comments
 (0)