Skip to content

fix(toExports): dedupe duplicate identifiers for class/enum declarations - #529

Merged
antfu merged 1 commit into
unjs:mainfrom
MukundaKatta:fix/to-exports-dedupe-class
Apr 28, 2026
Merged

antfu merged 1 commit into
unjs:mainfrom
MukundaKatta:fix/to-exports-dedupe-class

Conversation

@MukundaKatta

@MukundaKatta MukundaKatta commented Apr 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When includeType is enabled, toExports could emit invalid code:

export { Foo, Foo } from 'src/Foo';

This happens because classes (and enums, const enums, interfaces) live in both the value and type spaces. Since #507, dedupeImports intentionally keeps both a value entry and a type-only entry for these declarations. toExports then stringified both, producing the duplicate.

A single export { Foo } already re-exports both the value and the type for class/enum/interface declarations, so this PR dedupes entries by their exported alias before stringifying, preferring the value import when both forms exist.

Before / After

Input:

[
  { from: 'src/Foo', name: 'Foo', as: 'Foo' },
  { from: 'src/Foo', name: 'Foo', as: 'Foo', type: true, declarationType: 'class' },
]

Before: export { Foo, Foo } from 'src/Foo'; ❌ (SyntaxError)
After: export { Foo } from 'src/Foo'; ✅

Test plan

  • pnpm vitest run — 203 tests pass (2 new: dedupe with value-then-type order, and type-then-value order)
  • pnpm build
  • pnpm lint

Fixes #498

Summary by CodeRabbit

  • Bug Fixes

    • Fixed generation of duplicate export statements when the same identifier appears multiple times.
    • When both value and type imports exist for the same identifier, the value import is now preferred in exports.
  • Tests

    • Added test cases for export deduplication behavior.

When `includeType` is enabled, classes/enums/const-enums/interfaces that
live in both the value and type spaces are kept as two separate entries
by `dedupeImports` (since unjs#507). `toExports` then emitted them both,
producing invalid output like:

    export { Foo, Foo } from 'src/Foo';

A single `export { Foo }` already re-exports both the value and the
type for these declaration kinds, so dedupe by exported alias before
stringifying, preferring the value import when both forms exist.

Fixes unjs#498
@coderabbitai

coderabbitai Bot commented Apr 14, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5525a7de-4607-4ae0-8472-8649604572cb

📥 Commits

Reviewing files that changed from the base of the PR and between 10a9ef3 and 197c39a.

📒 Files selected for processing (2)
  • src/utils.ts
  • test/to-export.test.ts

📝 Walkthrough

Walkthrough

Modified toExports function to deduplicate export statements by collapsing multiple specifiers for the same identifier, preferring value imports when both value and type-only imports exist. Test cases validate the deduplication behavior for classes.

Changes

Cohort / File(s) Summary
Deduplication Logic
src/utils.ts
Added deduplication logic to collapse duplicate export specifiers per source module and exported identifier. When both value and type-only imports exist for the same {from, name, as} tuple, only the value-style export is emitted.
Test Coverage
test/to-export.test.ts
Added two test cases validating deduplication behavior: verifying duplicate entries collapse into single re-exports, and confirming value imports are preferred over type-only imports regardless of input order.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 Hops with glee, the duplicates flee!
Foo no more appears twice, you see.
Value wins when both exist—
A dedup twist we could not resist!
Export once, export with grace,
Cleaner code in every case! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: deduplicating duplicate identifiers in toExports for classes and enums when includeType is enabled.
Linked Issues check ✅ Passed The code changes successfully address issue #498 by implementing deduplication logic in toExports to prevent duplicate identifiers in generated exports.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the duplicate identifier issue in toExports; no unrelated modifications were introduced.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.72%. Comparing base (a4a5e0a) to head (197c39a).
⚠️ Report is 164 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #529      +/-   ##
==========================================
- Coverage   98.73%   94.72%   -4.01%     
==========================================
  Files          14       13       -1     
  Lines        1817     1005     -812     
  Branches      374      331      -43     
==========================================
- Hits         1794      952     -842     
- Misses         23       49      +26     
- 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.

@antfu
antfu merged commit 28486d5 into unjs:main Apr 28, 2026
4 of 5 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.

toExports with includeType=true creates duplicate identifiers for classes

2 participants