Skip to content

Commit

Permalink
Add array expression to isConsiderReferencedNS (resolves #844)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Nov 22, 2024
1 parent 4657618 commit 1baa1d4
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/docs/src/content/docs/guides/namespace-imports.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ const shorthand = { NS };

const assignment = NS;

const item = [NS];

type TypeOf = typeof NS;

export { NS };
Expand Down
6 changes: 6 additions & 0 deletions packages/knip/fixtures/imports-namespace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as NS3 from './namespace3';
import * as NS4 from './namespace4';
import * as NS5 from './namespace5';
import * as NS6 from './namespace6';
import * as NS7 from './namespace7';
import * as NS8 from './namespace8';
import fn from 'external';

NS.identifier15;
Expand All @@ -24,3 +26,7 @@ fn(NS4);
const spread = { ...NS5 };

const assign = NS6;

fn([NS7]);

fn({ NS8 });
2 changes: 2 additions & 0 deletions packages/knip/fixtures/imports-namespace/namespace7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const identifier39 = 39;
export const identifier40 = 40;
2 changes: 2 additions & 0 deletions packages/knip/fixtures/imports-namespace/namespace8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const identifier41 = 41;
export const identifier42 = 42;
1 change: 1 addition & 0 deletions packages/knip/src/typescript/ast-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export const isConsiderReferencedNS = (node: ts.Identifier) =>
ts.isShorthandPropertyAssignment(node.parent) ||
(ts.isCallExpression(node.parent) && node.parent.arguments.includes(node)) ||
ts.isSpreadAssignment(node.parent) ||
ts.isArrayLiteralExpression(node.parent) ||
ts.isExportAssignment(node.parent) ||
(ts.isVariableDeclaration(node.parent) && node.parent.initializer === node) ||
ts.isTypeQueryNode(node.parent);
Expand Down
4 changes: 2 additions & 2 deletions packages/knip/test/imports-namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test('Ignore namespace re-export by entry file', async () => {
assert.deepEqual(counters, {
...baseCounters,
unlisted: 1,
processed: 8,
total: 8,
processed: 10,
total: 10,
});
});

0 comments on commit 1baa1d4

Please sign in to comment.