oxc-project/oxc · error · OxcDiagnostic

Prefer using method from `{obj_name}.prototype`.

Error message

Prefer using method from `{obj_name}.prototype`.

What it means

Diagnostic from the unicorn/prefer-prototype-methods lint rule. It fires when a method is called on an instance of `{obj_name}` (named in the message) but the rule cannot verify the method exists on that constructor's prototype — for example calling a method on `new Array()`'s instance via an unknown shape — so it recommends referencing the method through `{obj_name}.prototype` to guarantee the call resolves even if the receiver is subclassed or cross-realm. It is a lint suggestion, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_prototype_methods.rs:20

use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

use crate::{
    AstNode,
    ast_util::is_method_call,
    context::LintContext,
    rule::Rule,
    utils::{is_empty_array_expression, is_empty_object_expression, pad_fix_with_token_boundary},
};

fn known_method(span: Span, obj_name: &str, method_name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Prefer using `{obj_name}.prototype.{method_name}`."))
        .with_label(span)
}

fn unknown_method(span: Span, obj_name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Prefer using method from `{obj_name}.prototype`."))
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// This rule prefers borrowing methods from the prototype instead of the instance.
    ///
    /// ### Why is this bad?
    ///
    /// “Borrowing” a method from an instance of `Array` or `Object` is less clear than getting it from the corresponding prototype.
    ///
    /// ### Examples
    ///
    /// Examples of **incorrect** code for this rule:

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Reference the method via `{obj_name}.prototype` instead of relying on the instance shape
  2. Use `.call()`/`.apply()` with an explicit receiver when needed
  3. Restructure the code so the receiver's type is statically clear
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_prototype_methods.rs:20 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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