fix(dedupeImports): use original array index to avoid offset with disabled imports by guoyangzhen · Pull Request #510 · unjs/unimport · GitHub
Skip to content

fix(dedupeImports): use original array index to avoid offset with disabled imports#510

Open
guoyangzhen wants to merge 1 commit intounjs:mainfrom
guoyangzhen:main
Open

fix(dedupeImports): use original array index to avoid offset with disabled imports#510
guoyangzhen wants to merge 1 commit intounjs:mainfrom
guoyangzhen:main

Conversation

@guoyangzhen
Copy link
Copy Markdown

@guoyangzhen guoyangzhen commented Mar 16, 2026

Fixes #505

Problem

dedupeImports used imports.filter(i => !i.disabled).forEach((i, idx) => ...) where idx is the index in the filtered array. But indexToRemove stores these filtered indexes and applies them to the original array via imports.filter((_, idx) => !indexToRemove.has(idx)).

When disabled imports exist, all indexes are offset. For example:

[{ name: 'disabled', disabled: true }, { name: 'foo' }, { name: 'foo' }]
// idx 0 in filtered = idx 1 in original, but idx 0 gets removed

Fix

Iterate the original array directly and skip disabled items with return. This ensures idx always matches the original array position.

Summary by CodeRabbit

  • Refactor
    • Optimized internal import handling logic to improve deduplication behavior when certain items are disabled. This change does not affect the public API or user-facing functionality.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 16, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deduplication offset when some imports are disabled

1 participant