Do not tree shake interfaces or classes that extend or inherit from symbols defined in the default library

This commit is contained in:
Alex Dima
2020-05-08 14:40:50 +02:00
parent 1c4b3759ff
commit 4578463f90
3 changed files with 62 additions and 3 deletions

View File

@@ -536,7 +536,7 @@ function markNodes(languageService: ts.LanguageService, options: ITreeShakingOpt
continue;
}
if (options.shakeLevel === ShakeLevel.ClassMembers && (ts.isClassDeclaration(declaration) || ts.isInterfaceDeclaration(declaration))) {
if (options.shakeLevel === ShakeLevel.ClassMembers && (ts.isClassDeclaration(declaration) || ts.isInterfaceDeclaration(declaration)) && !isLocalCodeExtendingOrInheritingFromDefaultLibSymbol(program, checker, declaration)) {
enqueue_black(declaration.name!);
for (let j = 0; j < declaration.members.length; j++) {
@@ -752,6 +752,36 @@ function generateResult(languageService: ts.LanguageService, shakeLevel: ShakeLe
//#region Utils
function isLocalCodeExtendingOrInheritingFromDefaultLibSymbol(program: ts.Program, checker: ts.TypeChecker, declaration: ts.ClassDeclaration | ts.InterfaceDeclaration): boolean {
if (!program.isSourceFileDefaultLibrary(declaration.getSourceFile()) && declaration.heritageClauses) {
for (const heritageClause of declaration.heritageClauses) {
for (const type of heritageClause.types) {
const symbol = findSymbolFromHeritageType(checker, type);
if (symbol) {
const decl = symbol.valueDeclaration || (symbol.declarations && symbol.declarations[0]);
if (decl && program.isSourceFileDefaultLibrary(decl.getSourceFile())) {
return true;
}
}
}
}
}
return false;
}
function findSymbolFromHeritageType(checker: ts.TypeChecker, type: ts.ExpressionWithTypeArguments | ts.Expression | ts.PrivateIdentifier): ts.Symbol | null {
if (ts.isExpressionWithTypeArguments(type)) {
return findSymbolFromHeritageType(checker, type.expression);
}
if (ts.isIdentifier(type)) {
return getRealNodeSymbol(checker, type)[0];
}
if (ts.isPropertyAccessExpression(type)) {
return findSymbolFromHeritageType(checker, type.name);
}
return null;
}
/**
* Returns the node's symbol and the `import` node (if the symbol resolved from a different module)
*/