Skip to content

Commit

Permalink
Fixed inlining transitive-dependencies when using inlineDeclareGlobals
Browse files Browse the repository at this point in the history
  • Loading branch information
timocov committed Jan 25, 2024
1 parent 1da6921 commit eb03676
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/bundle-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,32 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
});
}

function isSymbolUsedByGlobalSymbols(symbol: ts.Symbol, visitedSymbols: Set<ts.Symbol> = new Set()): boolean {
if (visitedSymbols.has(symbol)) {
return false;
}

visitedSymbols.add(symbol);

return Array.from(typesUsageEvaluator.getSymbolsUsingSymbol(symbol) ?? []).some((usedInSymbol: ts.Symbol) => {
if (usedInSymbol.escapedName !== ts.InternalSymbolName.Global) {
return isSymbolUsedByGlobalSymbols(usedInSymbol, visitedSymbols);
}

const usedByThisSymbol = getDeclarationsForSymbol(usedInSymbol).some((decl: ts.Declaration) => {
const closestModuleLike = getClosestSourceFileLikeNode(decl);
const moduleInfo = getModuleLikeModuleInfo(closestModuleLike, criteria, typeChecker);
return moduleInfo.type === ModuleType.ShouldBeInlined;
});

if (usedByThisSymbol) {
return true;
}

return isSymbolUsedByGlobalSymbols(usedInSymbol, visitedSymbols);
});
}

function isNodeUsed(node: ts.Node): boolean {
if (isNodeNamedDeclaration(node) || ts.isSourceFile(node)) {
const nodeSymbol = getNodeSymbol(node, typeChecker);
Expand All @@ -778,7 +804,7 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
return true;
}

return inlineDeclareGlobals && getGlobalSymbolsUsingSymbol(nodeSymbol).length !== 0;
return inlineDeclareGlobals && isSymbolUsedByGlobalSymbols(nodeSymbol);
} else if (ts.isVariableStatement(node)) {
return node.declarationList.declarations.some((declaration: ts.VariableDeclaration) => {
return isNodeUsed(declaration);
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export interface FooOptions {}
export interface SubOptions {}

export interface FooOptions {
field: SubOptions;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Interface } from 'fake-package';

export interface SubOptions {
}
export interface FooOptions {
field: SubOptions;
}
declare global {
export namespace Cypress {
Expand Down

0 comments on commit eb03676

Please sign in to comment.