oxc-project/oxc · error · OxcDiagnostic

Prefer `Object.fromEntries` over manual object construction

Error message

Prefer `Object.fromEntries` over manual object construction from entries.

What it means

Diagnostic from the unicorn/prefer-object-from-entries lint rule. It fires when code builds an object manually from a key/value collection — typically `entries.reduce((acc, [k, v]) => { acc[k] = v; return acc; }, {})` or an equivalent loop — where `Object.fromEntries(entries)` produces the same object declaratively. The flagged input is the manual construction expression. It is a lint suggestion, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_object_from_entries.rs:21

    ast::{Expression, MemberExpression, ObjectPropertyKind, PropertyKind},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_str::CompactStr;
use schemars::JsonSchema;
use serde::Deserialize;

use crate::{
    AstNode,
    ast_util::is_method_call,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
    utils::{call_expr_member_expr_property_span, does_expr_match_any_path},
};

fn prefer_object_from_entries_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer `Object.fromEntries` over manual object construction from entries.")
        .with_help("Use `Object.fromEntries(pairs)` instead of manually building objects with `reduce` or `forEach`.")
        .with_label(span)
}

#[derive(Debug, Default, Clone, Deserialize)]
pub struct PreferObjectFromEntries(Box<PreferObjectFromEntriesConfig>);

#[derive(Debug, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct PreferObjectFromEntriesConfig {
    /// Additional functions to treat as equivalents to `Object.fromEntries`.
    functions: Vec<CompactStr>,
}

impl Default for PreferObjectFromEntriesConfig {
    fn default() -> Self {
        Self { functions: vec!["_.fromPairs".into(), "lodash.fromPairs".into()] }
    }

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the reduce/loop accumulation with `Object.fromEntries(entries)`
  2. Use `Object.fromEntries(map)` directly for Maps
  3. Apply the rule's auto-fix where the pattern is recognized
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_object_from_entries.rs:21 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/608b33702b669e60. Report an issue: GitHub.