Skip to content

Commit

Permalink
Add missing test & widen scope for implicit enumerations (resolves #927)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jan 27, 2025
1 parent 79392e2 commit d02db68
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
22 changes: 10 additions & 12 deletions packages/knip/src/typescript/get-imports-and-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,18 +353,16 @@ const getImportsAndExports = (
} else {
imports.refs.add(id);
}
} else if (imports.importedNs.has(id)) {
if (isConsiderReferencedNS(node)) {
// Pattern: fn(NS), { ...NS } etc. (https://knip.dev/guides/namespace-imports)
imports.refs.add(id);
} else if (isObjectEnumerationCallExpressionArgument(node)) {
// Pattern: Object.keys(NS)
imports.refs.add(id);
} else if (isInForIteration(node)) {
// Pattern: for (const x in NS) { }
// Pattern: for (const x of NS) { }
imports.refs.add(id);
}
} else if (imports.importedNs.has(id) && isConsiderReferencedNS(node)) {
// Pattern: fn(NS), { ...NS } etc. (https://knip.dev/guides/namespace-imports)
imports.refs.add(id);
} else if (isObjectEnumerationCallExpressionArgument(node)) {
// Pattern: Object.keys(NS)
imports.refs.add(id);
} else if (isInForIteration(node)) {
// Pattern: for (const x in NS) { }
// Pattern: for (const x of NS) { }
imports.refs.add(id);
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions packages/knip/test/enum-members-enumerated.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from 'bun:test';
import assert from 'node:assert/strict';
import { main } from '../src/index.js';
import { resolve } from '../src/util/path.js';
import baseArguments from './helpers/baseArguments.js';
import baseCounters from './helpers/baseCounters.js';

const cwd = resolve('fixtures/enum-members-enumerated');

test('Consider enum enumerated enum members used', async () => {
const { counters } = await main({
...baseArguments,
cwd,
});

assert.deepEqual(counters, {
...baseCounters,
processed: 3,
total: 3,
});
});

0 comments on commit d02db68

Please sign in to comment.