Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/converter/convertFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ describe('convertFile', () => {
await testFileConversion('module-exports', 'multiple-named-exports');
});

it('converts object literal exports', async () => {
await testFileConversion('module-exports', 'object-literal-export');
});

it('converts object literal exports with aliases', async () => {
await testFileConversion('module-exports', 'object-literal-aliased');
});

it('converts object literal exports with mixed properties', async () => {
await testFileConversion('module-exports', 'object-literal-mixed');
});

it('handles functions exported as default from plain JavaScript files', async () => {
await testFileConversion('module-exports-function-js', 'build-example-index', 'js');
await testFileConversion('module-exports-function-js', 'build-example-index-markdown', 'js');
Expand Down
37 changes: 37 additions & 0 deletions src/converter/replacer/replaceModuleExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function replaceModuleExports(sourceFile: SourceFile) {
const isNamedExport = leftText.startsWith('module.exports.');
const isExportingFunction = right.getKind() === SyntaxKind.FunctionExpression;
const isExportingIdentifier = right.getKind() === SyntaxKind.Identifier;
const isExportingObjectLiteral = right.getKind() === SyntaxKind.ObjectLiteralExpression;

const {comment} = NodeUtil.extractComment(left);

Expand All @@ -52,6 +53,42 @@ export function replaceModuleExports(sourceFile: SourceFile) {
} else if (isExportingFunction) {
// @see https://github.com/dsherret/ts-morph/issues/1586
sourceFile.insertStatements(position, `${comment}export default ${rightText};`);
} else if (isExportingObjectLiteral) {
// Handle object literal expressions like module.exports = { c0, c1 }
foundNamedExport = true;
const objectLiteral = right.asKind(SyntaxKind.ObjectLiteralExpression);
if (objectLiteral) {
const exportNames: string[] = [];
const properties = objectLiteral.getProperties();

for (const property of properties) {
if (property.getKind() === SyntaxKind.ShorthandPropertyAssignment) {
// For shorthand properties like { c0, c1 }, export the name directly
const shorthandProperty = property.asKind(SyntaxKind.ShorthandPropertyAssignment);
if (shorthandProperty) {
const name = shorthandProperty.getName();
exportNames.push(name);
}
} else if (property.getKind() === SyntaxKind.PropertyAssignment) {
// For regular properties like { a: c0, b: c1 }, export with alias
const propAssignment = property.asKind(SyntaxKind.PropertyAssignment);
if (propAssignment) {
const name = propAssignment.getName();
const initializer = propAssignment.getInitializer();
if (initializer && initializer.getKind() === SyntaxKind.Identifier) {
const identifier = initializer.getText();
exportNames.push(`${identifier} as ${name}`);
}
}
}
}

if (exportNames.length > 0) {
sourceFile.insertExportDeclaration(position, {
namedExports: exportNames,
});
}
}
}

expressionStatement.remove();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const valueA = 0;
const valueB = 1;

export { valueA as a, valueB as b };
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const valueA = 0;
const valueB = 1;

module.exports = {
a: valueA,
b: valueB,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const c0 = 0;
const c1 = 1;

export { c0, c1 };
7 changes: 7 additions & 0 deletions src/test/fixtures/module-exports/src/object-literal-export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const c0 = 0;
const c1 = 1;

module.exports = {
c0,
c1,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const x = 1;
const y = 2;

export { x, y as z };
7 changes: 7 additions & 0 deletions src/test/fixtures/module-exports/src/object-literal-mixed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const x = 1;
const y = 2;

module.exports = {
x,
z: y,
};