Skip to content

Conversation

@cexoso
Copy link

@cexoso cexoso commented Aug 28, 2023

egg-ts-helper doesn't support code like this:

export const plus = (a: number, b: number) => a + b;

because egg-ts-helper generator d.ts file with content import xx from "./file" and xx mead defaultExport.
In this case, What we need is import * as xx from "./file".

this PR detect code, and generate import * as xx from "./file" when code just has export(not export defualt)

Summary by CodeRabbit

  • New Features

    • Generator now detects whether target files use default or named exports and emits the appropriate import style (default import or import *) in generated output, improving compatibility and reducing manual adjustments.
  • Tests

    • Updated expectations to check for explicit import statements instead of raw module paths.
    • Added coverage for both named and default export scenarios.
    • Revised fixtures to use named exports in helper examples, aligning with the new import behavior.

we detect ts file content,and generate import * as xx from "file" when
module just has export.
@fengmk2 fengmk2 changed the title support ESM export for extend feat: support ESM export for extend Sep 28, 2025
@fengmk2 fengmk2 requested a review from Copilot September 28, 2025 15:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for ESM exports in the extend generator by detecting when a file contains only named exports (no default export) and generating appropriate import statements.

  • Detects files with only named exports using a new hasExportWithoutDefault helper function
  • Updates import statement generation to use namespace imports (import * as) when appropriate
  • Modifies test fixture to use named exports instead of default export

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/generators/extend.ts Adds export detection logic and updates import generation to handle named exports
test/fixtures/app/app/extend/helper.ts Converts from default export to named exports for testing
test/generators/extend.test.ts Updates test assertions to match new import patterns

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@coderabbitai
Copy link

coderabbitai bot commented Sep 28, 2025

Walkthrough

Introduces per-file import construction in ExtendGenerator by detecting default vs. named exports via a new helper. Updates test fixture helper to use named exports. Adjusts generator tests to assert on generated import statements (star vs. default) rather than raw paths.

Changes

Cohort / File(s) Summary of changes
Import construction logic in generator
src/generators/extend.ts
Added private helper hasExportWithoutDefault(filePath) to detect absence of default export. ExtendGenerator now computes useImportStar per file and passes it as a fourth argument to utils.getImportStr, altering import strings based on export style.
Test fixture: helper exports switched to named
test/fixtures/app/app/extend/helper.ts
Replaced default-exported helper object with two named functions: isCool and isNotCool. Removed export default helper.
Generator tests updated for import assertions
test/generators/extend.test.ts
Updated expectations to check for generated import statements: import * as ExtendIHelper from '../../../app/extend/helper' and import ExtendUnittestApplication from '../../../app/extend/application.unittest' instead of raw module paths.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant EG as ExtendGenerator
  participant FS as File System
  participant U as utils.getImportStr
  participant Out as Generated Declarations

  EG->>FS: Read target file
  EG->>EG: hasExportWithoutDefault(filePath)
  alt No default export detected
    EG->>U: getImportStr(path, alias, type, useImportStar=true)
  else Default export present
    EG->>U: getImportStr(path, alias, type, useImportStar=false)
  end
  U-->>EG: Import statement string
  EG->>Out: Write import + context
  note over Out: Output varies between<br/>import * as X ... vs. import X ...
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

A hare with specs and tidy paws,
Sniffs exports, learns the laws—
“Star or default? I’ll know per file!”
It twitches code with careful style.
Tests now cheer, imports spar—
Hop, hop, shipped! import * as star ✨🐇

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title “feat: support ESM export for extend” succinctly captures the primary enhancement of detecting and supporting named ESM exports in the extend generator, directly reflecting the core change without extraneous details.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d55a1c0 and 0325854.

📒 Files selected for processing (3)
  • src/generators/extend.ts (2 hunks)
  • test/fixtures/app/app/extend/helper.ts (1 hunks)
  • test/generators/extend.test.ts (2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
test/fixtures/app/app/extend/helper.ts (1)
test/fixtures/app/app/extend/application.ts (2)
  • isCool (2-4)
  • isNotCool (6-8)
🔇 Additional comments (4)
test/fixtures/app/app/extend/helper.ts (1)

1-7: Named helper exports align with new generator logic.

Switching the fixture to named exports mirrors the ESM scenario we want to support and keeps the helper surface straightforward for the tests.

test/generators/extend.test.ts (2)

52-54: Great coverage for the star-import path.

This assertion will flag any regression that accidentally falls back to default imports for named-export helpers.


61-63: Default-import regression guard still in place.

Thanks for keeping a check that the default-export path continues to emit the expected default import.

src/generators/extend.ts (1)

9-12: Import strategy helper looks good.

Centralizing the export detection and passing the flag into getImportStr keeps the generator tidy while enabling the ESM case.

Also applies to: 50-51


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.

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.

2 participants