repo-tools/type-deps: detect ambient jest usage in types

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2026-02-09 20:58:08 +01:00
parent a7d4a3109c
commit df59ee6a82
2 changed files with 20 additions and 1 deletions
@@ -0,0 +1,5 @@
---
'@backstage/repo-tools': patch
---
The `type-deps` command now detects ambient global types from the `jest` namespace in declaration files, rather than only looking for explicit imports and reference directives.
@@ -89,7 +89,21 @@ function findAllDeps(declSrc: string) {
.filter(n => !n.startsWith('.'))
// We allow references to these without an explicit dependency.
.filter(n => !['node', 'react'].includes(n));
return Array.from(new Set([...importedDeps, ...referencedDeps]));
// Detect ambient global type namespaces (e.g. jest.Mocked, jest.MockInstance)
// that are used in type positions but don't appear as explicit imports.
// Strip comments first to avoid false positives from JSDoc examples.
const strippedSrc = declSrc
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/\/\/.*$/gm, '');
const ambientDeps: string[] = [];
if (/\bjest\.\w/.test(strippedSrc)) {
ambientDeps.push('jest');
}
return Array.from(
new Set([...importedDeps, ...referencedDeps, ...ambientDeps]),
);
}
/**