Skip to content

Commit

Permalink
Fixed accidentally inlining export statements while inlining a librar…
Browse files Browse the repository at this point in the history
…y with namespaced imports/exports

Fixes #271
  • Loading branch information
timocov committed Nov 26, 2023
1 parent e1fd042 commit 7336366
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 7336366

Please sign in to comment.