Make knip compatible with erasableSyntaxOnly#1541
Conversation
By making knip compatible with erasableSyntaxOnly, we're making knip compatible with other systems that could potentially parse or transpile knip codebase. For example, we could: - replace `tsc` with another compiler like swc - drop `tsx` dependency from knip repository (Node.js supports TypeScript natively, but with erasable syntax only) This change also eliminates a whole class of potential bugs related to enums, namespaces legacy decorators and other syntax this setting is forbidding.
commit: |
There was a problem hiding this comment.
Pull request overview
This PR enables the erasableSyntaxOnly TypeScript compiler option for the knip package, making it compatible with alternative TypeScript parsers/transpilers like swc and Node.js native TypeScript support. This change restricts the codebase to use only "erasable" TypeScript syntax that doesn't require JavaScript emit transformations.
Changes:
- Enabled
erasableSyntaxOnlyin tsconfig.json compiler options - Converted enum
CharacterCodesto a const object in ast-helpers.ts - Refactored constructor parameter properties to explicit private field declarations in three Peeker classes (YamlCatalogPeeker, PackagePeeker, JsonCatalogPeeker)
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/knip/tsconfig.json | Adds erasableSyntaxOnly: true to compiler options |
| packages/knip/src/typescript/ast-helpers.ts | Converts CharacterCodes enum to const object with property syntax |
| packages/knip/src/YamlCatalogPeeker.ts | Removes constructor parameter property, adds explicit private field |
| packages/knip/src/PackagePeeker.ts | Removes constructor parameter property, adds explicit private field |
| packages/knip/src/JsonCatalogPeeker.ts | Removes constructor parameter property, adds explicit private field |
…peScript version This setting is just a safeguard, so removing it does not change the output in any way and is safe for testing purposes. Without this change, older TypeScript versions throw with "Unknown compiler option 'erasableSyntaxOnly'.".
8f107bd to
ca30458
Compare
This draft PR is to showcase the potential behind webpro-nl#1541. Local results are promising: Old test results: ``` ℹ tests 245 ℹ suites 0 ℹ pass 245 ℹ fail 0 ℹ cancelled 0 ℹ skipped 0 ℹ todo 0 ℹ duration_ms 43892.244416 ``` ``` ℹ tests 324 ℹ suites 1 ℹ pass 323 ℹ fail 1 ℹ cancelled 0 ℹ skipped 0 ℹ todo 0 ℹ duration_ms 48137.8335 ``` Old log output (fragment): ``` wojciech.maj@MacBook-Pro knip % pnpm test:smoke > knip@5.83.1 test:smoke /Users/wojciech.maj/Projekty/Open source projects/knip/packages/knip > glob-bin -c "tsx --test" "test/*.test.ts" && glob-bin -c "tsx --test" "test/{plugins,util}/*.test.ts" ✔ Should handle empty named catalog entries without null pointer error (344.005667ms) ✔ Should track referenced catalog entries (package.json) (128.936166ms) ✔ Should track referenced catalog entries (package.json root) (69.343ms) ✔ Should track referenced default catalog entries (177.375916ms) ✔ Should track referenced named catalog entries (37.006291ms) ✔ Should track referenced default catalog entries (171.729875ms) ``` New test results: ``` ℹ tests 245 ℹ suites 0 ℹ pass 245 ℹ fail 0 ℹ cancelled 0 ℹ skipped 0 ℹ todo 0 ℹ duration_ms 39849.829583 ``` ``` ℹ tests 324 ℹ suites 1 ℹ pass 322 ℹ fail 2 ℹ cancelled 0 ℹ skipped 0 ℹ todo 0 ℹ duration_ms 37563.619541 ``` New log output (fragment): ``` wojciech.maj@MacBook-Pro knip % pnpm test:smoke-fast > knip@5.83.1 test:smoke-fast /Users/wojciech.maj/Projekty/Open source projects/knip/packages/knip > glob-bin -c "node --import=./register.ts --test" "test/*.test.ts" && glob-bin -c "node --import=./register.ts --test" "test/{plugins,util}/*.test.ts" ✔ Should handle empty named catalog entries without null pointer error (96.482917ms) ✔ Should track referenced catalog entries (package.json) (74.104167ms) ✔ Should track referenced catalog entries (package.json root) (57.632125ms) ✔ Should track referenced default catalog entries (126.710542ms) ✔ Should track referenced named catalog entries (60.290042ms) ✔ Should track referenced default catalog entries (126.315625ms) ```
|
Great! I was also thinking of going |
Yes, I think that's a fantastic direction. Only it was too disruptive to just throw a PR at you without discussing first :D |
|
🚀 This pull request is included in v5.85.0. See Release 5.85.0 for release notes. Using Knip in a commercial project? Please consider becoming a sponsor. |
By making knip compatible with erasableSyntaxOnly, we're making knip compatible with other systems that could potentially parse or transpile knip codebase, but are unable (or rather: unwilling) to parse TS features that require JS emit changes.
For example, we could:
tscwith another compiler like swctsxdependency from knip repository (Node.js supports TypeScript natively, but with erasable syntax only)This change also eliminates a whole class of potential bugs related to enums, namespaces legacy decorators and other syntax this setting is forbidding.