Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't error on missing re-exported declarations #332

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions src/types-usage-evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
isNodeNamedDeclaration,
splitTransientSymbol,
} from './helpers/typescript';
import { warnLog } from './logger';

export class TypesUsageEvaluator {
private readonly typeChecker: ts.TypeChecker;
Expand Down Expand Up @@ -141,20 +142,24 @@ export class TypesUsageEvaluator {
// `export {}` or `export {} from 'mod'`
if (ts.isExportDeclaration(node) && node.exportClause !== undefined && ts.isNamedExports(node.exportClause)) {
for (const exportElement of node.exportClause.elements) {
const exportElementSymbol = getImportExportReferencedSymbol(exportElement, this.typeChecker);

// i.e. `import * as NS from './local-module'`
const namespaceImportForElement = getDeclarationsForSymbol(exportElementSymbol).find(ts.isNamespaceImport);
if (namespaceImportForElement !== undefined) {
// the namespaced import itself doesn't add a "usage", but re-export of that imported namespace does
// so here we're handling the case where previously imported namespace import has been re-exported from a module
this.addUsagesForNamespacedModule(namespaceImportForElement, namespaceImportForElement.parent.parent.moduleSpecifier as ts.StringLiteral);
}
try {
const exportElementSymbol = getImportExportReferencedSymbol(exportElement, this.typeChecker);

// "link" referenced symbol with its import
const exportElementOwnSymbol = this.getNodeOwnSymbol(exportElement.name);
this.addUsages(exportElementSymbol, exportElementOwnSymbol);
this.addUsages(this.getActualSymbol(exportElementSymbol), exportElementOwnSymbol);
// i.e. `import * as NS from './local-module'`
const namespaceImportForElement = getDeclarationsForSymbol(exportElementSymbol).find(ts.isNamespaceImport);
if (namespaceImportForElement !== undefined) {
// the namespaced import itself doesn't add a "usage", but re-export of that imported namespace does
// so here we're handling the case where previously imported namespace import has been re-exported from a module
this.addUsagesForNamespacedModule(namespaceImportForElement, namespaceImportForElement.parent.parent.moduleSpecifier as ts.StringLiteral);
}

// "link" referenced symbol with its import
const exportElementOwnSymbol = this.getNodeOwnSymbol(exportElement.name);
this.addUsages(exportElementSymbol, exportElementOwnSymbol);
this.addUsages(this.getActualSymbol(exportElementSymbol), exportElementOwnSymbol);
} catch (error) {
warnLog(`Could not resolve declaration. Are types installed for module: ${node.moduleSpecifier?.getText()}?`);
}
}
}

Expand Down