Skip to content

feat(linter): implement unicorn/no-immediate-mutation rule#17118

Merged
camc314 merged 3 commits intooxc-project:mainfrom
tt-a1i:feat/no-immediate-mutation
Dec 19, 2025
Merged

feat(linter): implement unicorn/no-immediate-mutation rule#17118
camc314 merged 3 commits intooxc-project:mainfrom
tt-a1i:feat/no-immediate-mutation

Conversation

@tt-a1i
Copy link
Contributor

@tt-a1i tt-a1i commented Dec 19, 2025

Summary

Implements the unicorn/no-immediate-mutation rule that disallows mutating a variable immediately after initialization.

Closes #9534

What it detects

  • Array mutations: array.push(), array.unshift()
  • Object property assignments: obj.foo = bar
  • Object.assign() calls
  • Set mutations: set.add()
  • Map mutations: map.set()

Features

  • Handles both variable declarations and assignment expressions
  • Correctly skips self-referencing mutations (e.g., array.push(array[0]))
  • Supports all statement containers including SwitchCase
  • Validates global references for Object/Set/Map to avoid false positives when these identifiers are shadowed

Examples

// Bad
const array = [1, 2];
array.push(3);

const obj = { foo: 1 };
obj.bar = 2;

const set = new Set([1]);
set.add(2);

// Good
const array = [1, 2, 3];

const obj = { foo: 1, bar: 2 };

const set = new Set([1, 2]);

Test plan

  • All 94 pass cases and 118 fail cases from eslint-plugin-unicorn pass
  • Clippy passes with no warnings

AI Assistance Disclosure

I used Codex to review the changes, sanity-check the implementation against existing patterns, and help spot potential edge cases.

@github-actions github-actions bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Dec 19, 2025
@tt-a1i tt-a1i marked this pull request as ready for review December 19, 2025 06:40
Implements the `unicorn/no-immediate-mutation` rule that disallows mutating
a variable immediately after initialization.

Detects:
- Array mutations: `array.push()`, `array.unshift()`
- Object property assignments: `obj.foo = bar`
- Object.assign(): `Object.assign(obj, source)`
- Set mutations: `set.add()`
- Map mutations: `map.set()`

Features:
- Handles both variable declarations and assignment expressions
- Correctly skips self-referencing mutations (e.g., `array.push(array[0])`)
- Supports SwitchCase statement containers
- Validates global references for Object/Set/Map to avoid false positives
  when these identifiers are shadowed
@tt-a1i tt-a1i force-pushed the feat/no-immediate-mutation branch from ad16a0b to cc46b6f Compare December 19, 2025 06:42
@codspeed-hq
Copy link

codspeed-hq bot commented Dec 19, 2025

CodSpeed Performance Report

Merging #17118 will not alter performance

Comparing tt-a1i:feat/no-immediate-mutation (e8258f2) with main (b87600a)

Summary

✅ 4 untouched
⏩ 41 skipped1

Footnotes

  1. 41 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Contributor

@camc314 camc314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@camc314 camc314 merged commit 09e8e31 into oxc-project:main Dec 19, 2025
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants