Skip to content

Commit

Permalink
Simplify isReferencedInExport and report types-in-types (resolves #830
Browse files Browse the repository at this point in the history
)
  • Loading branch information
webpro committed Nov 11, 2024
1 parent f01a9fc commit 80537c5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 21 deletions.
4 changes: 2 additions & 2 deletions packages/knip/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type IgnorePatterns = (string | RegExp)[];

type IgnorableExport = 'class' | 'enum' | 'function' | 'interface' | 'member' | 'type';

export type IgnoreExportsUsedInFile = boolean | Partial<Record<IgnorableExport, boolean>>;
type IgnoreExportsUsedInFile = boolean | Partial<Record<IgnorableExport, boolean>>;

export type GetImportsAndExportsOptions = {
skipTypeOnly: boolean;
Expand Down Expand Up @@ -68,7 +68,7 @@ export interface Configuration {
rootPluginConfigs: Partial<PluginsConfiguration>;
}

export type NormalizedGlob = string[];
type NormalizedGlob = string[];

export type EnsuredPluginConfiguration = {
config: NormalizedGlob | null;
Expand Down
21 changes: 5 additions & 16 deletions packages/knip/src/typescript/ast-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,26 +201,15 @@ export const isImportSpecifier = (node: ts.Node) =>
ts.isImportClause(node.parent) ||
ts.isNamespaceImport(node.parent);

const isExported = (node: ts.Node): boolean => {
const isInExportedNode = (node: ts.Node): boolean => {
if (getExportKeywordNode(node)) return true;
return node.parent ? isExported(node.parent) : false;
};

const isTypeDeclaration = (node: ts.Node) =>
ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isEnumDeclaration(node);

const getAncestorTypeDeclaration = (node: ts.Node) => {
while (node) {
if (isTypeDeclaration(node)) return node;
node = node.parent;
}
return node.parent ? isInExportedNode(node.parent) : false;
};

export const isReferencedInExport = (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));
if (ts.isTypeQueryNode(node.parent) && isInExportedNode(node.parent.parent)) return true;
if (ts.isTypeReferenceNode(node.parent) && isInExportedNode(node.parent.parent)) return true;
return false;
};

export const getExportKeywordNode = (node: ts.Node) =>
Expand Down
1 change: 0 additions & 1 deletion packages/knip/src/typescript/find-internal-references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const findInternalReferences = (

if (item.symbol === symbol) {
refCount++;
if (isInExport || isType(item)) return [refCount, isSymbolInExport];
if (isBindingElement) return [refCount, true];
}

Expand Down
3 changes: 1 addition & 2 deletions packages/knip/src/typescript/get-imports-and-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ const getImportsAndExports = (
// and whether it's referenced in an exported type and should be exported with it (*)
for (const item of exports.values()) {
// TODO Reconsider this messy logic in AST visitors + `isReferencedInExport` + `findInternalReferences`
if (item.symbol && referencedSymbolsInExport.has(item.symbol)) {
if (!isType(item) && item.symbol && referencedSymbolsInExport.has(item.symbol)) {
item.refs = [1, true];
} else {
const isBindingElement = item.symbol?.valueDeclaration && ts.isBindingElement(item.symbol.valueDeclaration);
Expand All @@ -408,7 +408,6 @@ const getImportsAndExports = (
(typeof ignoreExportsUsedInFile === 'object' &&
item.type !== 'unknown' &&
ignoreExportsUsedInFile[item.type]) ||
isType(item) ||
isBindingElement
) {
item.refs = findInternalReferences(item, sourceFile, typeChecker, referencedSymbolsInExport, isBindingElement);
Expand Down
2 changes: 2 additions & 0 deletions packages/knip/test/exports-value-refs-default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ test('Find unused exports in exported types (default)', async () => {
});

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

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

0 comments on commit 80537c5

Please sign in to comment.