oxc-project/oxc · warning · OxcDiagnostic
Do not call `{method}()` immediately after initializing an a
Error message
Do not call `{method}()` immediately after initializing an array. What it means
Diagnostic from the oxlint rule `unicorn/no-immediate-mutation` (array arm, category: pedantic). When a variable is declared (or plainly reassigned) with an array literal and the very next statement mutates it via `push()` or `unshift()`, the initialization is split across two statements for no benefit. The rule asks you to fold the elements into the array initializer. It only fires when the call directly follows the declaration, targets the same identifier, and no argument references that variable.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs:17
use oxc_ast::{
AstKind,
ast::{
Argument, ArrayExpressionElement, AssignmentExpression, AssignmentTarget, CallExpression,
Expression, NewExpression, ObjectPropertyKind, Statement, VariableDeclaration,
VariableDeclarator,
},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::IsGlobalReference;
use oxc_span::{GetSpan, Span};
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)View on GitHub (pinned to e1e7af627c)
Solutions
- Fold the elements into the initializer: `const queue = [1, 2, 3];` (for unshift, prepend: `[1, 2, ...rest]`)
- For conditional elements use spread: `const arr = [1, ...(includeExtra ? [extra] : [])]`
- If the two-statement form is intentional, suppress with `// oxlint-disable-next-line unicorn/no-immediate-mutation`
Example fix
// before const queue = [1, 2]; queue.push(3); // after const queue = [1, 2, 3];
Defensive patterns
Strategy: validation
Prevention
- Build collection literals in one expression; prepend/append with spread when sources vary
- Watch for this pattern in generated code — fix the generator, not the output
- Keep declarations and their first mutation in the same statement whenever possible
When it happens
Trigger: `const array = [1, 2]; array.push(3);` and `let array; array = [3, 4]; array.unshift(1, 2);` — push/unshift with at least one argument immediately after an array-literal declaration/assignment. Self-referencing args (`array.push(array[0])`), optional chains (`array?.push(1)`), and other methods pass.
Common situations: Leftover incremental construction; code generators emitting init-then-append; 'declare empty, fill later' habits that outlived the refactor that made them unnecessary.
Related errors
- Avoid calls to the `Array` constructor
- Use Array destructuring.
- Assign array to a variable before exporting as module defaul
- Prefer `{} {}` over `{} {}` to check {}.
- Do not call `Object.assign()` immediately after initializing
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/ca305bcb7c6a0e21.
Report an issue: GitHub.