oxc-project/oxc · warning · OxcDiagnostic

Disallow use of `Object.prototype.hasOwnProperty.call()` and

Error message

Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`.

What it means

Diagnostic from the oxlint `prefer-object-has-own` rule. It flags the legacy pattern `Object.prototype.hasOwnProperty.call(obj, key)` and recommends the ES2022 static `Object.hasOwn(obj, key)` (prefer_object_has_own.rs:13-18). The rule matches method calls via is_method_call (imported in the file), covering `{}.hasOwnProperty.call(...)` shapes reached through member expressions.

Source

Thrown at crates/oxc_linter/src/rules/eslint/prefer_object_has_own.rs:13

use oxc_ast::{
    AstKind,
    ast::{Expression, MemberExpression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_str::static_ident;

use crate::{AstNode, ast_util::is_method_call, context::LintContext, rule::Rule};

fn prefer_object_has_own_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(
        "Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`."
    ).with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferObjectHasOwn;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow use of `Object.prototype.hasOwnProperty.call()` and prefer use of `Object.hasOwn()`
    ///
    /// ### Why is this bad?
    ///
    /// It is very common to write code like:
    /// ```javascript
    /// if (Object.prototype.hasOwnProperty.call(object, "foo")) {
    ///     console.log("has property foo");

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace with `Object.hasOwn(obj, key)` (behaviorally equivalent for this use).
  2. If older runtimes must be supported, add a tiny polyfill for Object.hasOwn and still migrate the call sites, or disable the rule until support drops.
  3. For plain own-property checks in modern code only, direct migration is safe; verify with your browserslist/engines field.
  4. Inline-disable the rare case that intentionally tests the legacy pattern.

Example fix

// before
if (Object.prototype.hasOwnProperty.call(config, "debug")) { ... }

// after
if (Object.hasOwn(config, "debug")) { ... }
Defensive patterns

Strategy: validation

Validate before calling

// feature-detect before migrating if you support old runtimes
const hasOwn = Object.hasOwn ?? ((o, k) => Object.prototype.hasOwnProperty.call(o, k));

Prevention

When it happens

Trigger: Enable the rule and write `Object.prototype.hasOwnProperty.call(config, "key")`, a pattern widely used because plain `obj.hasOwnProperty(key)` is unsafe (prototype pollution / inherited props).

Common situations: Utility belt code predating ES2022; configs enabling the rule while the code must also run on Node < 16.9 / old browsers without Object.hasOwn; polyfill-less projects; code copied from MDN's older examples. If your support matrix lacks Object.hasOwn, keep the rule off or polyfill.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/cdc61191816245d8. Report an issue: GitHub.