Strict types for src/_lib/eleventy/pdf.js#1327
Open
stefan-burke wants to merge 5 commits into
Open
Conversation
Adds JSDoc type annotations so pdf.js passes tsc --strict. Ratchet baseline drops from 395 to 374 errors and pdf.js joins STRICT_CLEAN_FILES. Supporting changes: - Add `DietaryKey` type and a non-optional `dietaryKeys` field on `MenuItemData` (the 11tydata boundary already guarantees `[]`), so the PDF builder no longer needs `?? []` fallbacks. - Rewrite pdf.test.js as 20 focused, behaviour-driven tests with shared fixture factories; this also removed the test file from `ALLOWED_MUTABLE_CONST`. The cast that turns `json-to-pdf`'s `unknown`-returning `renderPdfTemplate` into the typed `RenderPdfFn` is the only type assertion in the file and is expressed at module level (where `@type` is permitted) via a double cast through `unknown`, since the package types the return as `unknown`.
Resolves conflicts: - scripts/strict-typecheck-ratchet.js: pdf.js entry was auto-merged into the updated STRICT_CLEAN_FILES list; CURRENT_ERROR_COUNT recomputed to 336 against the merged tree. - test/unit/build/pdf.test.js: kept main's rewrite (more thorough, includes generateMenuPdf integration tests). All 25 tests pass against the strictly-typed pdf.js.
Main's pdf.test.js (taken in the prior merge) uses imperative log/error capture arrays — restoring the allowlist entry that the earlier branch had dropped because its alternative test rewrite didn't need it.
The remote auto-merge (0923e32) left unresolved <<<<<<< / >>>>>>> markers in scripts/strict-typecheck-ratchet.js and test/unit/build/pdf.test.js, which is why CI biome was failing. Resolves both: - ratchet: keep the recomputed CURRENT_ERROR_COUNT = 336 - pdf.test.js: keep the version from this branch (main's rewrite, matched up with the strictly-typed pdf.js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds JSDoc type annotations to
src/_lib/eleventy/pdf.jsso it passestsc --strict. The ratchet baseline drops from 395 → 374 errors andpdf.jsjoinsSTRICT_CLEAN_FILES.Changes
src/_lib/eleventy/pdf.js— Strict types via JSDoc, no inline@typeannotations inside function bodies, noany. DefinesPdfState,PdfData,PdfMenuItem,PdfCategory,PdfDoc, andRenderPdfFntypedefs at module level. A single genericgetTaggedAs<T>(api, tag)helper replaces two duplicate per-tag casthelpers.
src/_lib/types/eleventy.d.ts— AddsDietaryKeytype and anon-optional
dietaryKeys: DietaryKey[]field onMenuItemData. The11tydata boundary already guarantees
[]here, so the PDF builder nolonger needs
?? []fallbacks.src/_lib/types/index.d.ts— Re-exportsDietaryKey.scripts/strict-typecheck-ratchet.js— LowersCURRENT_ERROR_COUNTfrom 395 to 374 and addspdf.jstoSTRICT_CLEAN_FILES.test/unit/build/pdf.test.js— Rewritten as 20 focused,behaviour-driven tests with shared fixture factories
(
menu(),category(),menuItem()). Removes a previousALLOWED_MUTABLE_CONSTexception that the old test file required.Compromises
The
json-to-pdfpackage typesrenderPdfTemplateas(...) => unknown. To call.pipe()and.end()on the result withstrict typing, a cast to
RenderPdfFnis required. That cast lives atmodule level (where
@typeis permitted) and goes throughunknowntosatisfy TypeScript's structural subtyping rules — it's the only type
assertion in the file.
pdfStateis a module-levellet(the same lazy-init pattern usedelsewhere in
_lib/eleventy/), reset on eachconfigurePdfcall sotest ordering stays deterministic.
configurePdf's registrationhelpers (
writePdfs,captureState,onAfter) are nested inside thefunction so they pass single-use checks while keeping
configurePdfitself well under the 30-line limit.
Test plan
bun run precommit— all checks pass (lint, typecheck, stricttypecheck, cpd, tests)
bun test test/unit/build/pdf.test.js— 20/20 passhttps://claude.ai/code/session_01AiUpnX7LR6e5KGmH2Ukoq6