Skip to content

Commit

Permalink
Merge pull request #278 from timocov/external-inlines-with-re-exports
Browse files Browse the repository at this point in the history
Fixed accidentally inlining export statements while inlining a library with namespaced imports/exports
  • Loading branch information
timocov authored Nov 26, 2023
2 parents e1fd042 + 7336366 commit f31f373
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/bundle-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,18 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
for (const declaration of getDeclarationsForExportedValues(exportAssignment)) {
let exportedDeclarations: readonly ts.Statement[] = [];

if (ts.isModuleDeclaration(declaration)) {
if (ts.isExportDeclaration(exportAssignment) && ts.isSourceFile(declaration)) {
const referencedModule = getReferencedModuleInfo(exportAssignment, criteria, typeChecker);
if (referencedModule !== null) {
if (visitedModules.has(referencedModule.fileName)) {
continue;
}

visitedModules.add(referencedModule.fileName);
}

exportedDeclarations = declaration.statements;
} else if (ts.isModuleDeclaration(declaration)) {
if (declaration.body !== undefined && ts.isModuleBlock(declaration.body)) {
const referencedModule = getReferencedModuleInfo(declaration, criteria, typeChecker);
if (referencedModule !== null) {
Expand Down Expand Up @@ -258,7 +269,7 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
}

if (ts.isExportDeclaration(statement)) {
if (!currentModule.isExternal) {
if (currentModule.type === ModuleType.ShouldBeInlined) {
continue;
}

Expand Down
8 changes: 7 additions & 1 deletion src/helpers/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,13 @@ export function getClosestSourceFileLikeNode(node: ts.Node): ts.SourceFile | ts.
return ts.isSourceFile(node) ? node : node.parent;
}

export type NodeWithReferencedModule = ts.ExportDeclaration | ts.ModuleDeclaration | ts.ImportTypeNode | ts.ImportEqualsDeclaration | ts.ImportDeclaration;
export type NodeWithReferencedModule =
| ts.ExportDeclaration
| ts.ImportDeclaration
| ts.ImportEqualsDeclaration
| ts.ImportTypeNode
| ts.ModuleDeclaration
;

export function resolveReferencedModule(node: NodeWithReferencedModule, typeChecker: ts.TypeChecker): ts.SourceFile | ts.ModuleDeclaration | null {
let moduleName: ts.Expression | ts.LiteralTypeNode | undefined;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/e2e/test-cases/re-export-in-node_modules/config.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/e2e/test-cases/re-export-in-node_modules/input.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/e2e/test-cases/re-export-in-node_modules/output.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f31f373

Please sign in to comment.