Skip to content

Commit eb03676

Browse files
committed
Fixed inlining transitive-dependencies when using inlineDeclareGlobals
1 parent 1da6921 commit eb03676

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/bundle-generator.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,32 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
766766
});
767767
}
768768

769+
function isSymbolUsedByGlobalSymbols(symbol: ts.Symbol, visitedSymbols: Set<ts.Symbol> = new Set()): boolean {
770+
if (visitedSymbols.has(symbol)) {
771+
return false;
772+
}
773+
774+
visitedSymbols.add(symbol);
775+
776+
return Array.from(typesUsageEvaluator.getSymbolsUsingSymbol(symbol) ?? []).some((usedInSymbol: ts.Symbol) => {
777+
if (usedInSymbol.escapedName !== ts.InternalSymbolName.Global) {
778+
return isSymbolUsedByGlobalSymbols(usedInSymbol, visitedSymbols);
779+
}
780+
781+
const usedByThisSymbol = getDeclarationsForSymbol(usedInSymbol).some((decl: ts.Declaration) => {
782+
const closestModuleLike = getClosestSourceFileLikeNode(decl);
783+
const moduleInfo = getModuleLikeModuleInfo(closestModuleLike, criteria, typeChecker);
784+
return moduleInfo.type === ModuleType.ShouldBeInlined;
785+
});
786+
787+
if (usedByThisSymbol) {
788+
return true;
789+
}
790+
791+
return isSymbolUsedByGlobalSymbols(usedInSymbol, visitedSymbols);
792+
});
793+
}
794+
769795
function isNodeUsed(node: ts.Node): boolean {
770796
if (isNodeNamedDeclaration(node) || ts.isSourceFile(node)) {
771797
const nodeSymbol = getNodeSymbol(node, typeChecker);
@@ -778,7 +804,7 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
778804
return true;
779805
}
780806

781-
return inlineDeclareGlobals && getGlobalSymbolsUsingSymbol(nodeSymbol).length !== 0;
807+
return inlineDeclareGlobals && isSymbolUsedByGlobalSymbols(nodeSymbol);
782808
} else if (ts.isVariableStatement(node)) {
783809
return node.declarationList.declarations.some((declaration: ts.VariableDeclaration) => {
784810
return isNodeUsed(declaration);
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
export interface FooOptions {}
1+
export interface SubOptions {}
2+
3+
export interface FooOptions {
4+
field: SubOptions;
5+
}

tests/e2e/test-cases/export-via-global-declaration/output.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Interface } from 'fake-package';
22

3+
export interface SubOptions {
4+
}
35
export interface FooOptions {
6+
field: SubOptions;
47
}
58
declare global {
69
export namespace Cypress {

0 commit comments

Comments
 (0)