Skip to content

Commit

Permalink
Filter out external refs to re-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Nov 10, 2024
1 parent 9a78e33 commit b9ffef4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/knip/src/ProjectPrincipal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,16 @@ export class ProjectPrincipal {

const externalRefs = referencedSymbols
.flatMap(refs => refs.references)
.filter(ref => !ref.isDefinition && ref.fileName !== filePath);
.filter(ref => !ref.isDefinition && ref.fileName !== filePath)
.filter(ref => {
// Filter out are re-exports
const sourceFile = this.backend.program?.getSourceFile(ref.fileName);
if (!sourceFile) return true;
// @ts-expect-error ts.getTokenAtPosition is internal fn
const node = ts.getTokenAtPosition(sourceFile, ref.textSpan.start);
if (!node?.parent?.parent?.parent) return true;
return !(ts.isExportSpecifier(node.parent) && node.parent.parent.parent.moduleSpecifier);
});

return externalRefs.length > 0;
}
Expand Down

0 comments on commit b9ffef4

Please sign in to comment.