oxc-project/oxc · error · OxcDiagnostic

Prefer using `{obj_name}.prototype.{method_name}`.

Error message

Prefer using `{obj_name}.prototype.{method_name}`.

What it means

Diagnostic from the unicorn/prefer-prototype-methods lint rule. It fires when a method is invoked on a value via a construct that skips the prototype chain unnecessarily — typically calling `{obj_name}.{method_name}` through a throwaway instance (an empty array/object literal) instead of `{obj_name}.prototype.{method_name}`; the prototype form is reliable even when the receiver may be null/undefined or from another realm. It is a lint suggestion, not a runtime error.

Source

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

use oxc_ast::{AstKind, ast::Expression};
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?
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Call the method as `{obj_name}.prototype.{method_name}.call(receiver, ...)` or on the known prototype
  2. Avoid creating throwaway instances (`[]`, `{}`) just to reach a method
  3. Apply the rule's auto-fix
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_prototype_methods.rs:15 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/fc8f5c854568ca74. Report an issue: GitHub.