Skip to content

Commit 3d76e51

Browse files
committed
Cover more cases for symbol refs finder (resolves #1273)
1 parent 087a98e commit 3d76e51

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function fn() {}
2+
3+
const obj = { fn };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import './antecedents';
2+
import './fn';
23
import { slice } from './slice';
34

45
slice;

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export const findInternalReferences = (
5454
if (declaration) {
5555
// @ts-expect-error Keep it cheap
5656
if (findInFlow(declaration.name?.flowNode, item.symbol)) {
57-
return [++refCount, isSymbolInExport];
57+
refCount++;
58+
return [refCount, isSymbolInExport];
5859
}
5960

6061
if (ts.isImportSpecifier(declaration) && symbols.has(symbol)) {
@@ -63,6 +64,14 @@ export const findInternalReferences = (
6364
}
6465
}
6566

67+
if (symbol && symbol.flags & ts.SymbolFlags.Property) {
68+
const type = typeChecker.getTypeOfSymbol(symbol);
69+
if (type?.symbol && item.symbol === type.symbol) {
70+
refCount++;
71+
if (isBindingElement) return [refCount, isSymbolInExport];
72+
}
73+
}
74+
6675
symbols.add(symbol);
6776
}
6877
}

packages/knip/test/ignore-exports-used-in-file-shorthand.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ test('Find unused exports respecting an ignoreExportsUsedInFile (shorthand)', as
1313

1414
assert.deepEqual(counters, {
1515
...baseCounters,
16-
processed: 3,
17-
total: 3,
16+
processed: 4,
17+
total: 4,
1818
});
1919
});

0 commit comments

Comments
 (0)