|
| 1 | +# Type Dependencies Test |
| 2 | + |
| 3 | +This test suite validates that packages in the HPCC Visualization Framework monorepo do not reference types from third-party libraries unless those libraries are explicitly declared as dependencies or peerDependencies in their `package.json`. |
| 4 | + |
| 5 | +## Purpose |
| 6 | + |
| 7 | +- **Prevents runtime errors**: Ensures that all external type dependencies are properly declared |
| 8 | +- **Improves maintainability**: Makes it clear which external libraries each package depends on |
| 9 | +- **Enforces consistency**: Ensures all packages follow the same dependency declaration patterns |
| 10 | + |
| 11 | +## What it checks |
| 12 | + |
| 13 | +1. **TypeScript type files**: Scan `index.d.ts`, `index.node.d.ts`, `index.browser.d.ts` files in each package's `types/` directory |
| 14 | +2. **Import statements**: Analyzes `import` and `from` statements to find external dependencies |
| 15 | +3. **Type imports**: Specifically looks for type-only imports and type references |
| 16 | +4. **Dependency validation**: Checks that referenced packages are in the `dependencies` section of `package.json` |
| 17 | + |
| 18 | +## Exclusions |
| 19 | + |
| 20 | +The test ignores: |
| 21 | +- **Built-in Node.js modules**: `fs`, `path`, `util`, etc. |
| 22 | +- **TypeScript built-ins**: `typescript`, `@types/node` |
| 23 | +- **Internal packages**: All `@hpcc-js/*` packages |
| 24 | +- **Relative imports**: Local file imports starting with `./` or `../` |
| 25 | +- **Dev dependencies**: Types only used in tests or build scripts |
| 26 | + |
| 27 | +## Running the tests |
| 28 | + |
| 29 | +```bash |
| 30 | +# From the root directory |
| 31 | +npm run test --workspace tests/type-leaks |
| 32 | + |
| 33 | +# Or directly in the types directory |
| 34 | +cd tests/type-leaks |
| 35 | +npm test |
| 36 | +``` |
| 37 | + |
| 38 | +## Example violations |
| 39 | + |
| 40 | +If a package imports from `lodash` but doesn't have it in dependencies: |
| 41 | + |
| 42 | +```typescript |
| 43 | +// ❌ Violation - lodash not in dependencies |
| 44 | +import { debounce } from 'lodash'; |
| 45 | + |
| 46 | +// ✅ Correct - lodash in dependencies section of package.json |
| 47 | +{ |
| 48 | + "dependencies": { |
| 49 | + "lodash": "^4.17.21" |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +## Type-only imports |
| 55 | + |
| 56 | +For type-only dependencies, you can use: |
| 57 | + |
| 58 | +```typescript |
| 59 | +// Type-only import (still requires dependency declaration) |
| 60 | +import type { SomeType } from 'external-library'; |
| 61 | + |
| 62 | +// Or in dependencies as a peer dependency for types |
| 63 | +{ |
| 64 | + "peerDependencies": { |
| 65 | + "external-library": "^1.0.0" |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
0 commit comments