Skip to content

Commit 80537c5

Browse files
committedNov 11, 2024·
Simplify isReferencedInExport and report types-in-types (resolves #830)
1 parent f01a9fc commit 80537c5

File tree

5 files changed

+10
-21
lines changed

5 files changed

+10
-21
lines changed
 

‎packages/knip/src/types/config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export type IgnorePatterns = (string | RegExp)[];
4040

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

43-
export type IgnoreExportsUsedInFile = boolean | Partial<Record<IgnorableExport, boolean>>;
43+
type IgnoreExportsUsedInFile = boolean | Partial<Record<IgnorableExport, boolean>>;
4444

4545
export type GetImportsAndExportsOptions = {
4646
skipTypeOnly: boolean;
@@ -68,7 +68,7 @@ export interface Configuration {
6868
rootPluginConfigs: Partial<PluginsConfiguration>;
6969
}
7070

71-
export type NormalizedGlob = string[];
71+
type NormalizedGlob = string[];
7272

7373
export type EnsuredPluginConfiguration = {
7474
config: NormalizedGlob | null;

‎packages/knip/src/typescript/ast-helpers.ts

+5-16
Original file line numberDiff line numberDiff line change
@@ -201,26 +201,15 @@ export const isImportSpecifier = (node: ts.Node) =>
201201
ts.isImportClause(node.parent) ||
202202
ts.isNamespaceImport(node.parent);
203203

204-
const isExported = (node: ts.Node): boolean => {
204+
const isInExportedNode = (node: ts.Node): boolean => {
205205
if (getExportKeywordNode(node)) return true;
206-
return node.parent ? isExported(node.parent) : false;
207-
};
208-
209-
const isTypeDeclaration = (node: ts.Node) =>
210-
ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node) || ts.isEnumDeclaration(node);
211-
212-
const getAncestorTypeDeclaration = (node: ts.Node) => {
213-
while (node) {
214-
if (isTypeDeclaration(node)) return node;
215-
node = node.parent;
216-
}
206+
return node.parent ? isInExportedNode(node.parent) : false;
217207
};
218208

219209
export const isReferencedInExport = (node: ts.Node) => {
220-
if (ts.isTypeQueryNode(node.parent) && isExported(node.parent.parent)) return true;
221-
if (ts.isTypeReferenceNode(node.parent) && isExported(node.parent.parent)) return true;
222-
const typeNode = getAncestorTypeDeclaration(node);
223-
return Boolean(typeNode && isExported(typeNode));
210+
if (ts.isTypeQueryNode(node.parent) && isInExportedNode(node.parent.parent)) return true;
211+
if (ts.isTypeReferenceNode(node.parent) && isInExportedNode(node.parent.parent)) return true;
212+
return false;
224213
};
225214

226215
export const getExportKeywordNode = (node: ts.Node) =>

‎packages/knip/src/typescript/find-internal-references.ts

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ export const findInternalReferences = (
4141

4242
if (item.symbol === symbol) {
4343
refCount++;
44-
if (isInExport || isType(item)) return [refCount, isSymbolInExport];
4544
if (isBindingElement) return [refCount, true];
4645
}
4746

‎packages/knip/src/typescript/get-imports-and-exports.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ const getImportsAndExports = (
399399
// and whether it's referenced in an exported type and should be exported with it (*)
400400
for (const item of exports.values()) {
401401
// TODO Reconsider this messy logic in AST visitors + `isReferencedInExport` + `findInternalReferences`
402-
if (item.symbol && referencedSymbolsInExport.has(item.symbol)) {
402+
if (!isType(item) && item.symbol && referencedSymbolsInExport.has(item.symbol)) {
403403
item.refs = [1, true];
404404
} else {
405405
const isBindingElement = item.symbol?.valueDeclaration && ts.isBindingElement(item.symbol.valueDeclaration);
@@ -408,7 +408,6 @@ const getImportsAndExports = (
408408
(typeof ignoreExportsUsedInFile === 'object' &&
409409
item.type !== 'unknown' &&
410410
ignoreExportsUsedInFile[item.type]) ||
411-
isType(item) ||
412411
isBindingElement
413412
) {
414413
item.refs = findInternalReferences(item, sourceFile, typeChecker, referencedSymbolsInExport, isBindingElement);

‎packages/knip/test/exports-value-refs-default.test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ test('Find unused exports in exported types (default)', async () => {
1414
});
1515

1616
assert(issues.exports['refs.ts']['logger']);
17+
assert(issues.types['refs.ts']['Lizard']);
1718

1819
assert.deepEqual(counters, {
1920
...baseCounters,
2021
exports: 1,
22+
types: 1,
2123
processed: 2,
2224
total: 2,
2325
});

0 commit comments

Comments
 (0)
Please sign in to comment.