Skip to content

Commit

Permalink
Do not emit duplicate exports
Browse files Browse the repository at this point in the history
  • Loading branch information
rix0rrr committed Oct 2, 2023
1 parent b034f08 commit 857ae0a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions tools/@aws-cdk/lazify/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export function transformFileContents(filename: string, contents: string, progre

file = ts.transform(file, [(ctx: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {
const factory = ctx.factory;
const alreadyEmittedExports = new Set<string>();

const visit: ts.Visitor = node => {
if (node.parent && ts.isSourceFile(node.parent)
&& ts.isExpressionStatement(node)
Expand All @@ -160,7 +162,7 @@ export function transformFileContents(filename: string, contents: string, progre
const entries = Object.keys(module);

return entries.flatMap((entry) =>
createModuleGetter(factory, entry, requiredModule, (mod) =>
createModuleGetterOnce(alreadyEmittedExports)(factory, entry, requiredModule, (mod) =>
factory.createPropertyAccessExpression(mod, entry))
);
}
Expand All @@ -180,7 +182,7 @@ export function transformFileContents(filename: string, contents: string, progre

const exportName = node.expression.left.name.text;
const moduleName = node.expression.right.arguments[0].text;
return createModuleGetter(factory, exportName, moduleName, (x) => x);
return createModuleGetterOnce(alreadyEmittedExports)(factory, exportName, moduleName, (x) => x);
}

return ts.visitEachChild(node, child => visit(child), ctx);
Expand Down Expand Up @@ -259,4 +261,20 @@ function createModuleGetter(
]
)
)];
}

/**
* Prevent emitting an export if it has already been emitted before
*
* This assumes that the symbols have the same definition, and are only duplicated because of
* accidental multiple `export *`s.
*/
function createModuleGetterOnce(alreadyEmittedExports: Set<string>): typeof createModuleGetter {
return (factory, exportName, moduleName, moduleFormatter) => {
if (alreadyEmittedExports.has(exportName)) {
return [];
}
alreadyEmittedExports.add(exportName);
return createModuleGetter(factory, exportName, moduleName, moduleFormatter);
};
}

0 comments on commit 857ae0a

Please sign in to comment.