Skip to content

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Aug 9, 2025

This PR fixes the issue where module.exports = { ... } object literals were being stripped entirely instead of being transformed to proper ESM exports.

Problem

Previously, when transforming CommonJS to ESM, the following pattern would be incorrectly handled:

const c0 = 0;
const c1 = 1;

module.exports = {
  c0,
  c1,
};

Before (incorrect): The entire module.exports statement was stripped, leaving only:

const c0 = 0;
const c1 = 1;

After (correct): Now properly transforms to ESM exports:

const c0 = 0;
const c1 = 1;

export { c0, c1 };

Solution

Enhanced the replaceModuleExports function in src/converter/replacer/replaceModuleExports.ts to handle object literal expressions by:

  1. Detecting object literals: Added support for SyntaxKind.ObjectLiteralExpression alongside existing identifier and function expression handling
  2. Shorthand properties: Transforms { c0, c1 } to export { c0, c1 }
  3. Aliased properties: Transforms { a: valueA, b: valueB } to export { valueA as a, valueB as b }
  4. Mixed properties: Handles combinations like { x, z: y }export { x, y as z }

The implementation uses ts-morph's AST parsing to properly extract property names and values from object literals, generating the appropriate ESM export declarations.

Testing

Added comprehensive test coverage with three new test cases covering all scenarios:

  • Basic shorthand properties
  • Aliased properties
  • Mixed shorthand and aliased properties

All existing tests continue to pass, ensuring backward compatibility.

Fixes #125.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] module.exports = { ... } are stripped instead of being transformed Fix module.exports object literal transformation to ESM exports Aug 9, 2025
@Copilot Copilot AI requested a review from bennycode August 9, 2025 16:38
Copilot finished work on behalf of bennycode August 9, 2025 16:38
@raprocks
Copy link

how do i try this locally?

i have a repo that i want to transform, but #125 makes the entire project unusable, how do i try this fix on my project to test

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.

module.exports = { ... } are stripped instead of being transformed

3 participants