{"record":{"id":"ca305bcb7c6a0e21","repo":"oxc-project/oxc","slug":"do-not-call-method-immediately-after-initial","errorCode":null,"errorMessage":"Do not call `{method}()` immediately after initializing an array.","messagePattern":"Do not call `(.+?)\\(\\)` immediately after initializing an array\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs","lineNumber":17,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{\n        Argument, ArrayExpressionElement, AssignmentExpression, AssignmentTarget, CallExpression,\n        Expression, NewExpression, ObjectPropertyKind, Statement, VariableDeclaration,\n        VariableDeclarator,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::IsGlobalReference;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn array_mutation_diagnostic(span: Span, method: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"Do not call `{method}()` immediately after initializing an array.\"\n    ))\n    .with_help(format!(\"Move the elements from `{method}()` into the array initializer.\"))\n    .with_label(span)\n}\n\nfn object_assign_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not call `Object.assign()` immediately after initializing an object.\")\n        .with_help(\"Move the properties from `Object.assign()` into the object initializer.\")\n        .with_label(span)\n}\n\nfn object_property_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Do not assign a property immediately after initializing an object literal.\",\n    )\n    .with_help(\"Move the property into the object initializer.\")\n    .with_label(span)","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs#L1-L35","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Leftover incremental construction; code generators emitting init-then-append; 'declare empty, fill later' habits that outlived the refactor that made them unnecessary.","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`"],"exampleFix":"// before\nconst queue = [1, 2];\nqueue.push(3);\n\n// after\nconst queue = [1, 2, 3];","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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"],"tags":["arrays","style","code-quality","oxlint"],"backgroundTag":"mutate-after-init","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}