{"record":{"id":"063effe773018763","repo":"oxc-project/oxc","slug":"do-not-call-set-immediately-after-initializin","errorCode":null,"errorMessage":"Do not call `.set()` immediately after initializing a Map.","messagePattern":"Do not call `\\.set\\(\\)` immediately after initializing a Map\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs","lineNumber":45,"sourceCode":"        .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)\n}\n\nfn set_add_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not call `.add()` immediately after initializing a Set.\")\n        .with_help(\"Add the element to the Set initializer array.\")\n        .with_label(span)\n}\n\nfn map_set_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not call `.set()` immediately after initializing a Map.\")\n        .with_help(\"Add the entry to the Map initializer array.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoImmediateMutation;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallows mutating a variable immediately after initialization.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// When you initialize a variable and immediately mutate it, it's cleaner to include\n    /// the mutation in the initialization. This makes the code more readable and reduces\n    /// the number of statements.\n    ///","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs#L27-L63","documentation":"Diagnostic from the oxlint rule `unicorn/no-immediate-mutation` (Map arm, category: pedantic). `const map = new Map(); map.set('a', 1);` is a two-statement initialization; the entry belongs in the constructor's array-of-pairs initializer. Fires for the global `Map` and `WeakMap` when `set()` with exactly two non-spread arguments (neither referencing the map) directly follows the declaration of the same variable.","triggerScenarios":"`const ages = new Map(); ages.set('alice', 30);` or `const weak = new WeakMap(); weak.set(obj, meta);` — `new Map`/`new WeakMap` declaration immediately followed by `<sameVar>.set(key, value)`. `set(k)` alone, spread arguments, or `map.set('k', map.size)` pass. TS generic Maps (`new Map<K, V>()`) are matched too.","commonSituations":"Lookup/registry tables built entry by entry (e.g. TypeScript mapper maps); converting record objects to Maps; WeakMaps holding private state.","solutions":["Initialize with pairs: `const ages = new Map([['alice', 30]]);`","For many entries: `new Map([...existing, ['key', value]])`","Keep `set()` and suppress inline when entries depend on runtime state"],"exampleFix":"// before\nconst ages = new Map();\nages.set('alice', 30);\n\n// after\nconst ages = new Map([['alice', 30]]);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Initialize Maps with arrays of [key, value] pairs","Convert records with Object.entries before the constructor: `new Map(Object.entries(rec))`","Keep `.set()` only when keys are computed at runtime, and say so in review"],"tags":["map","collections","style","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-14T05:17:10.506Z"}