Visitar URL original
fix: import `export default class` declarations correctly by haigou-web · Pull Request #574 · unjs/unimport · GitHub
Skip to content

fix: import export default class declarations correctly - #574

Open
haigou-web wants to merge 1 commit into
unjs:mainfrom
haigou-web:fix/default-class-import
Open

haigou-web wants to merge 1 commit into
unjs:mainfrom
haigou-web:fix/default-class-import

Conversation

@haigou-web

@haigou-web haigou-web commented Oct 7, 2026 •

Copy link
Copy Markdown

Fixes #541

Problem

export default class Foo {} was always imported as a named import:

import { MyClass } from './foo'   // should be: import MyClass from './foo'

mlly's findExports() returns two entries for that source — a default one and a declaration one, both starting at the same offset. scanExports() added a default import plus a named import plus a type import, and then dedupeImports() kept the wrong one: the correct {name: "default", as: "MyClass"} was dropped in favour of {name: "MyClass", as: "MyClass"}.

Note this is specific to class: export default function foo() {} yields only the default entry, which is why the two behave differently.

Fix

The declaration branch now skips entries that came from an export default statement, so only the default import survives. The type import is kept — MyClass is valid as a type, which is what mlly's change was for.

Verification

  • New test/default-class.test.ts
  • Before the fix the new test fails with the exact output from the issue (expected 'import { MyClass } from ...' to contain 'import MyClass from')
  • Full suite compared against a stashed baseline: the only difference is the new test flipping; the 11 pre-existing Windows snapshot failures are identical
  • tsc --noEmit: exit 0; eslint: exit 0

Summary by CodeRabbit

  • Bug Fixes
    • Default-exported classes are now imported correctly when used without an explicit import.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a28f30c8-25b6-479f-9d30-957d6a89cc43
📥 Commits

Reviewing files that changed from the base of the PR and between bbd0776 and 355aa0e.

📒 Files selected for processing (2)
  • src/node/scan-dirs.ts
  • test/default-class.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.


📝 Walkthrough

Walkthrough

scanExports no longer adds a named value import for declarations reported from export default code. A regression test verifies that import injection uses a default import for a default-exported class.

Changes

Default class import correction

Layer / File(s) Summary
Handle default-exported declarations
src/node/scan-dirs.ts, test/default-class.test.ts
scanExports omits named value imports for declarations whose code starts with export default. The test checks that injection uses import MyClass from and not a named import.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: flo0806

Merge Risk: ⚪ Minimal · up to 355aa

The default-exported class is now injected with default-import syntax. No actionable merge risk remains in this change.

Architecture Summary

Architecture risk: 🔵 Low · up to 355aa

The change affects 2 systems.

Changed systems: src, test

Architecture concerns
No architecture-level concerns identified.

Review details

Systems and components

  • observed — src (service) was modified; 1 changed file maps to changed impact.
  • observed — test (service) was modified; 1 changed file maps to changed impact.

Before / after behavior

  • observed — Modified behavior in src/node/scan-dirs.ts: toImport now detects declarations whose code starts with export default and omits their named value imports, while retaining reserved-word filtering and subsequent type-import handling. Other declarations continue to produce named value imports.
  • observed — Modified behavior in test/default-class.test.ts: Adds imports and test setup that creates a temporary directory and default-exported class file before the test, then removes the directory afterward.
  • observed — Modified behavior in test/default-class.test.ts: Adds a test that scans the class file and verifies import injection uses import MyClass from and does not use a named import for MyClass.
🚥 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%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Issue #541 requires export default class MyClass to produce a default import, not a named value import. In src/node/scan-dirs.ts, scanExports() now skips the value import for declaration entries…
Out of Scope Changes check ✅ Passed The reported changes are limited to scanExports() behavior and a regression test for the export default class case in issue #541. Both changes directly support the linked issue. The whole diff cou…
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix for importing export default class declarations correctly.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@haigou-web haigou-web changed the title Fix export default class being imported as a named import fix: import export default class declarations correctly Oct 8, 2026
@haigou-web

Copy link
Copy Markdown
Author

Friendly note: the Unit Test workflow on this PR is sitting in �ction_required, so no checks have actually executed yet. Could a maintainer approve the run when convenient? I'll address anything it surfaces.

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.

export default class is always imported wrong

2 participants