oxc-project/oxc · warning · OxcDiagnostic
Do not assign a property immediately after initializing an o
Error message
Do not assign a property immediately after initializing an object literal.
What it means
Diagnostic from the oxlint rule `unicorn/no-immediate-mutation` (property arm, category: pedantic). `const state = {}; state.loading = true;` splits an object literal across two statements; the property belongs in the initializer. Only plain `=` assignments to a static (or non-self-referencing computed) member of the same identifier that was declared as an object literal in the immediately preceding statement are flagged; compound assigns (`+=`), chained assignments through the object, and other receivers pass.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs:31
use crate::{AstNode, context::LintContext, rule::Rule};
fn array_mutation_diagnostic(span: Span, method: &str) -> OxcDiagnostic {
OxcDiagnostic::warn(format!(
"Do not call `{method}()` immediately after initializing an array."
))
.with_help(format!("Move the elements from `{method}()` into the array initializer."))
.with_label(span)
}
fn object_assign_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not call `Object.assign()` immediately after initializing an object.")
.with_help("Move the properties from `Object.assign()` into the object initializer.")
.with_label(span)
}
fn object_property_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Do not assign a property immediately after initializing an object literal.",
)
.with_help("Move the property into the object initializer.")
.with_label(span)
}
fn set_add_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not call `.add()` immediately after initializing a Set.")
.with_help("Add the element to the Set initializer array.")
.with_label(span)
}
fn map_set_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not call `.set()` immediately after initializing a Map.")
.with_help("Add the entry to the Map initializer array.")
.with_label(span)
}
View on GitHub (pinned to e1e7af627c)
Solutions
- Move the property into the literal: `const state = { loading: true };`
- For conditional properties use spread: `const state = { ...(active ? { active } : {}) }`
- Suppress with `// oxlint-disable-next-line unicorn/no-immediate-mutation` when the split form is intentional
Example fix
// before
const state = {};
state.loading = true;
// after
const state = { loading: true }; Defensive patterns
Strategy: validation
Prevention
- Write complete object literals; add conditional properties with spread
- Avoid the declare-empty-then-fill habit for objects with known shape
- When a property truly cannot be in the literal, add a comment saying why
When it happens
Trigger: `const state = {}; state.loading = true;`, `const obj = { foo: 1 }; obj[key] = value;` — a simple member assignment whose object is the just-declared object literal.
Common situations: 'Create empty object, then fill it in' habits; partial refactors from classes/functions to object literals; state objects built field by field.
Related errors
- Do not call `Object.assign()` immediately after initializing
- Object keys should be sorted
- Do not call `{method}()` immediately after initializing an a
- `VirtualFree` failed during cleanup: {err}
- Tried to get an allocator from an empty `FixedSizeAllocatorP
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/50783ec9b5de6fc5.
Report an issue: GitHub.