Skip to content

Commit

Permalink
Expand unused exports referenced in used exports (#808)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 9, 2024
1 parent 2f2a11e commit 1c04163
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
4 changes: 3 additions & 1 deletion packages/knip/src/typescript/ast-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ const getAncestorTypeDeclaration = (node: ts.Node) => {
}
};

export const isReferencedInExportedType = (node: ts.Node) => {
export const isReferencedInExported = (node: ts.Node) => {
if (ts.isTypeQueryNode(node.parent) && isExported(node.parent.parent)) return true;
if (ts.isTypeReferenceNode(node.parent) && isExported(node.parent.parent)) return true;
const typeNode = getAncestorTypeDeclaration(node);
return Boolean(typeNode && isExported(typeNode));
};
Expand Down
8 changes: 3 additions & 5 deletions packages/knip/src/typescript/get-imports-and-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
isDestructuring,
isImportSpecifier,
isObjectEnumerationCallExpressionArgument,
isReferencedInExportedType,
isReferencedInExported,
} from './ast-helpers.js';
import { findInternalReferences, isType } from './find-internal-references.js';
import getDynamicImportVisitors from './visitors/dynamic-imports/index.js';
Expand Down Expand Up @@ -373,10 +373,8 @@ const getImportsAndExports = (

// Store exports referenced in exported types, including `typeof` values
// Simplifies and speeds up (*) below while we're still in the realm of bound AST
if (!isTopLevel && symbol.exportSymbol) {
if (ts.isTypeQueryNode(node.parent) || isReferencedInExportedType(node)) {
referencedSymbolsInExportedTypes.add(symbol.exportSymbol);
}
if (!isTopLevel && symbol.exportSymbol && isReferencedInExported(node)) {
referencedSymbolsInExportedTypes.add(symbol.exportSymbol);
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions packages/knip/test/exports-value-refs-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import baseCounters from './helpers/baseCounters.js';

const cwd = resolve('fixtures/exports-value-refs-default');

test('Find unused exports in exported types', async () => {
const { counters } = await main({
test('Find unused exports in exported types (default)', async () => {
const { issues, counters } = await main({
...baseArguments,
cwd,
});

assert(issues.exports['refs.ts']['logger']);

assert.deepEqual(counters, {
...baseCounters,
exports: 1,
processed: 2,
total: 2,
});
Expand Down

0 comments on commit 1c04163

Please sign in to comment.