fix(dedupeImports): respect negative and zero import priorities by 8ctavio · Pull Request #527 · unjs/unimport · GitHub
Skip to content

fix(dedupeImports): respect negative and zero import priorities#527

Merged
antfu merged 3 commits intounjs:mainfrom
8ctavio:fix/dedupe-negative-priority
Apr 22, 2026
Merged

fix(dedupeImports): respect negative and zero import priorities#527
antfu merged 3 commits intounjs:mainfrom
8ctavio:fix/dedupe-negative-priority

Conversation

@8ctavio
Copy link
Copy Markdown
Contributor

@8ctavio 8ctavio commented Apr 14, 2026

Resolves #524.

The current implementation of dedupeImports ignores negative priorities in some cases. This PR implements proper support for imports' negative priorities.

In the linked issue, there's a warning for Nuxt's useAppConfig import, which uses a negative priority of -1, so I think not properly handling negative priorities is causing that issue.

Summary by CodeRabbit

  • Bug Fixes

    • Refined duplicate-import resolution so negative and zero priorities are handled correctly; default-priority imports are no longer erroneously replaced by lower-priority duplicates.
  • Tests

    • Added tests to verify behavior for negative and zero priority duplicates and ensure no spurious warnings are emitted.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 14, 2026

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 14, 2026

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.60%. Comparing base (a4a5e0a) to head (94bce12).
⚠️ Report is 162 commits behind head on main.

Files with missing lines Patch % Lines
src/utils.ts 66.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #527      +/-   ##
==========================================
- Coverage   98.73%   94.60%   -4.13%     
==========================================
  Files          14       13       -1     
  Lines        1817     1001     -816     
  Branches      374      332      -42     
==========================================
- Hits         1794      947     -847     
- Misses         23       50      +27     
- Partials        0        4       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
test/dedupe.test.ts (1)

70-93: Consider asserting no warning for the negative-priority case.

This test verifies winner selection, but not warning suppression. Adding an empty-warning assertion would harden the regression coverage.

Suggested test addition
   it('should respect negative priority', () => {
     expect(dedupeImports(
       [
         {
           name: 'foo',
           from: 'module1',
         },
         {
           name: 'foo',
           from: 'module2',
           priority: -1,
         },
       ],
       warnFn,
     )).toMatchInlineSnapshot(`
       [
         {
           "from": "module1",
           "name": "foo",
         },
       ]
     `)
+    expect(warnMsg).toMatchInlineSnapshot('""')
   })
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/dedupe.test.ts` around lines 70 - 93, The test for negative priority
should also assert that no warning was emitted: after calling dedupeImports in
the 'should respect negative priority' test, add an assertion that warnFn was
not called (e.g., expect(warnFn).not.toHaveBeenCalled() or
expect(warnFn.mock.calls.length).toBe(0)) so the test verifies both winner
selection and warning suppression for the negative-priority case; locate the
call to dedupeImports and the warnFn mock in this test to place the assertion.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@test/dedupe.test.ts`:
- Around line 70-93: The test for negative priority should also assert that no
warning was emitted: after calling dedupeImports in the 'should respect negative
priority' test, add an assertion that warnFn was not called (e.g.,
expect(warnFn).not.toHaveBeenCalled() or
expect(warnFn.mock.calls.length).toBe(0)) so the test verifies both winner
selection and warning suppression for the negative-priority case; locate the
call to dedupeImports and the warnFn mock in this test to place the assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7eb1b1b2-0ccc-43e6-b5ac-369b4f2977fd

📥 Commits

Reviewing files that changed from the base of the PR and between 10a9ef3 and 2164c86.

📒 Files selected for processing (2)
  • src/utils.ts
  • test/dedupe.test.ts

Comment thread src/utils.ts Outdated
Copy link
Copy Markdown
Member

@danielroe danielroe Apr 21, 2026

Choose a reason for hiding this comment

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

we should use ?? rather than || to respect priority of 0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should we do that now? I kept || for compatibility, but I can change to ?? if that's not an issue.

@antfu antfu changed the title fix(dedupeImports): support negative import priorities fix(dedupeImports): respect negative and zero import priorities Apr 22, 2026
@antfu antfu merged commit 4f462cb into unjs:main Apr 22, 2026
4 of 6 checks passed
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.

warn: duplicate "useAppConfig" surfaced in Nuxt 4.4.2 / Nitro after unimport 6.1.0

3 participants