Skip to content

Commit

Permalink
fix(linter): false positive in eslint/prefer-object-has-own (#7463)
Browse files Browse the repository at this point in the history
closes #7450
  • Loading branch information
shulaoda authored Nov 25, 2024
1 parent a3ecbde commit db6558f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
25 changes: 10 additions & 15 deletions crates/oxc_linter/src/rules/eslint/prefer_object_has_own.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,12 @@ impl Rule for PreferObjectHasOwn {
ctx.diagnostic(diagnostic);
} else {
ctx.diagnostic_with_fix(diagnostic, |fixer| {
let before_token_of_replace_span = if replace_target_span.start > 1 {
Some(ctx.source_range(Span::new(
replace_target_span.start - 1,
replace_target_span.start,
)))
} else {
None
};
let replacement = match before_token_of_replace_span {
Some(token) => match token {
" " | "=" | "/" | "(" => "Object.hasOwn",
_ => " Object.hasOwn",
},
_ => "Object.hasOwn",
};
let needs_space = replace_target_span.start > 1
&& !ctx
.source_range(Span::new(0, replace_target_span.start))
.ends_with(&[' ', '=', '/', '(']);

let replacement = if needs_space { " Object.hasOwn" } else { "Object.hasOwn" };
fixer.replace(replace_target_span, replacement)
});
}
Expand Down Expand Up @@ -238,6 +229,8 @@ fn test() {
"Object[`hasOwnProperty`][`call`](object, property);",
"({})['hasOwnProperty']['call'](object, property);",
"({})[`hasOwnProperty`][`call`](object, property);",
// Issue: <https://github.com/oxc-project/oxc/issues/7450>
"…Object.prototype.hasOwnProperty.call(C,x);",
];

let fix = vec![
Expand Down Expand Up @@ -385,6 +378,8 @@ fn test() {
"Object.hasOwn(object, property);",
None,
),
// Issue: <https://github.com/oxc-project/oxc/issues/7450>
("…Object.prototype.hasOwnProperty.call(C,x);", "… Object.hasOwn(C,x);", None),
];
Tester::new(PreferObjectHasOwn::NAME, pass, fail).expect_fix(fix).test_and_snapshot();
}
7 changes: 7 additions & 0 deletions crates/oxc_linter/src/snapshots/prefer_object_has_own.snap
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,10 @@ snapshot_kind: text
· ────────────────────────────────────────────────
╰────
help: Replace `({})[`hasOwnProperty`][`call`]` with `Object.hasOwn`.

eslint(prefer-object-has-own): Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`.
╭─[prefer_object_has_own.tsx:1:3]
1 │ …Object.prototype.hasOwnProperty.call(C,x);
· ─────────────────────────────────────────
╰────
help: Replace `Object.prototype.hasOwnProperty.call` with ` Object.hasOwn`.

0 comments on commit db6558f

Please sign in to comment.