oxc-project/oxc · warning · OxcDiagnostic
Do not call `.set()` immediately after initializing a Map.
Error message
Do not call `.set()` immediately after initializing a Map.
What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_immediate_mutation.rs:45
.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)
}
#[derive(Debug, Default, Clone)]
pub struct NoImmediateMutation;
declare_oxc_lint!(
/// ### What it does
///
/// Disallows mutating a variable immediately after initialization.
///
/// ### Why is this bad?
///
/// When you initialize a variable and immediately mutate it, it's cleaner to include
/// the mutation in the initialization. This makes the code more readable and reduces
/// the number of statements.
///View on GitHub (pinned to e1e7af627c)
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
Example fix
// before
const ages = new Map();
ages.set('alice', 30);
// after
const ages = new Map([['alice', 30]]); Defensive patterns
Strategy: validation
Prevention
- 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
When it happens
Trigger: `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.
Common situations: Lookup/registry tables built entry by entry (e.g. TypeScript mapper maps); converting record objects to Maps; WeakMaps holding private state.
Related errors
- Do not call `.add()` immediately after initializing a Set.
- The {expr_type} is useless
- `VirtualFree` failed during cleanup: {err}
- Tried to get an allocator from an empty `FixedSizeAllocatorP
- Prefer `async`/`await` to the callback pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/063effe773018763.
Report an issue: GitHub.