Migrate Core Patches to TypeScript#2853
Conversation
created module augmentation in .d.ts files for components patches, with types inferred by studying the usage in the node_modules src files. added tsc compiled .js files to .gitignore and prevent .d.ts files from being ignored Ignore compiled js and patched ts from linting
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2853 +/- ##
==========================================
- Coverage 71.92% 71.89% -0.03%
==========================================
Files 132 132
Lines 7358 7359 +1
Branches 1516 1542 +26
==========================================
- Hits 5292 5291 -1
+ Misses 2021 1967 -54
- Partials 45 101 +56 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR migrates MarkBind’s internal “core patches” (monkey-patches applied to nunjucks, htmlparser2, and a markdown-it custom-component patch set) from CommonJS .js files to TypeScript sources, and adds local .d.ts files to type internal/unstable APIs that aren’t covered by upstream @types/* packages.
Changes:
- Converted patch entrypoints from
*.jsto*.tsand updated exports/imports accordingly. - Added manual TypeScript declaration files for patched/hidden internals (
nunjuckssubmodules + internals,htmlparser2internals). - Updated ignore lists to avoid committing compiled patch outputs while still tracking the “manual” declaration files.
Reviewed changes
Copilot reviewed 13 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/patches/nunjucks/nunjucks-submodules.d.ts | Adds ambient module declarations for untyped nunjucks internal submodules. |
| packages/core/src/patches/nunjucks/nunjucks-internals.d.ts | Adds module augmentations for nunjucks internals used by MarkBind patches. |
| packages/core/src/patches/nunjucks/load-event.ts | Migrates the nunjucks “load event” patch to TS + ES imports. |
| packages/core/src/patches/nunjucks/index.ts | TS patch entrypoint (side-effect imports). |
| packages/core/src/patches/nunjucks/index.js | Removes old CommonJS entrypoint. |
| packages/core/src/patches/nunjucks/context-overrides-frame.ts | Migrates and types the MarkBind context/frame priority patch. |
| packages/core/src/patches/index.ts | Migrates patch aggregator to TS and wires ignore-tag injections. |
| packages/core/src/patches/index.js | Removes old CommonJS patch aggregator. |
| packages/core/src/patches/htmlparser2.ts | Migrates htmlparser2 patch to TS + ES imports and exports. |
| packages/core/src/patches/htmlparser2-internal.d.ts | Adds declaration augmentation for htmlparser2 internals used by the patch. |
| packages/core/src/lib/markdown-it/patches/custom-component/inlineTags.ts | Converts export style to TS-friendly export =. |
| packages/core/src/lib/markdown-it/patches/custom-component/htmlRe.ts | Converts to named ESM exports. |
| packages/core/src/lib/markdown-it/patches/custom-component/htmlInlineRule.ts | Migrates to TS imports/exports and adds types. |
| packages/core/src/lib/markdown-it/patches/custom-component/htmlBlockRule.ts | Migrates to TS imports/exports and adds types. |
| packages/core/src/lib/markdown-it/patches/custom-component/customComponentPlugin.ts | Migrates plugin wiring to TS imports/exports and adds types. |
| .gitignore | Ignores compiled patch JS outputs; attempts to unignore manual .d.ts files. |
| .eslintignore | Ignores compiled patch JS outputs for linting. |
Comments suppressed due to low confidence (3)
packages/core/src/patches/htmlparser2.ts:465
injectIgnoreTagsis typed asstring[], but the caller path (PluginManager->patches.ignoreTags) passes aSetof tags. Since the implementation only spreads the input, it can support any iterable; consider changing the parameter type toIterable<string>(orreadonly string[]) and converting to an array before spreading if needed.
packages/core/src/lib/markdown-it/patches/custom-component/customComponentPlugin.ts:41injectTagsis typed astagsToIgnore: string[], but the upstream call path (PluginManager->patches.ignoreTags) passes aSetof tags. Consider widening this (and thecustomComponentPluginparameter) toIterable<string>/ReadonlySet<string> | readonly string[]so the existing call-site type-checks without forcing an array conversion.
packages/core/src/lib/markdown-it/patches/custom-component/htmlBlockRule.ts:6initCustomComponentHtmlBlockRuleis typed to takestring[], but the surrounding logic (Array.from(tagsToIgnore)) works with any iterable and the upstream caller provides aSetof tags. Widening this parameter toIterable<string>(or acceptingSet<string> | string[]) would align with actual usage and avoid type errors once the caller is updated.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
0aa1499 to
ea62ea3
Compare
gerteck
left a comment
There was a problem hiding this comment.
Highlighted some parts of the code that needs a further look and update, but generally looks good
packages/core/src/lib/markdown-it/patches/custom-component/customComponentPlugin.ts
Outdated
Show resolved
Hide resolved
packages/core/src/lib/markdown-it/patches/custom-component/htmlInlineRule.ts
Outdated
Show resolved
Hide resolved
| // --------------------------------------------------------------------------- | ||
| // Sub-module: nunjucks/src/runtime | ||
| // --------------------------------------------------------------------------- | ||
| declare module 'nunjucks/src/runtime' { |
There was a problem hiding this comment.
Duplicated nunjucks/src/runtime and nunjucks/src/object declarations in
internals.d.tsandsubmodule.d.ts
Any reason why it was duplicated? Should it be removed from internals.d.ts?
There was a problem hiding this comment.
nunjucks-submodules.d.tsexists solely to create the sub-modules (nunjucks/src/runtime, nunjucks/src/object) as ambient declarations.nunjucks-internals.d.tsexists to augment the main nunjucks module, big declare module 'nunjucks' block at the bottom is its unique, non-duplicated work.
The sub-module blocks in it are dead weight that snuck in.
So the rule is: whoever creates something owns it. nunjucks-submodules.d.ts created the sub-modules, so it should keep the duplicated code. Delete the duplicate sub-module blocks from nunjucks-internals.d.ts.
|
Todos:
👍 |
ea62ea3 to
2834c80
Compare
What is the purpose of this pull request?
Overview of changes:
Migrated
nunjucks,htmlparser2andmarkdown-itpatches to TypeScript and ESM.Anything you'd like to highlight/discuss:
As these patches are modifying the internals of the npm packages, the types
we worked on are not exposed in the
@typespackages. We have to do module augmentationto add the types that we are patching. Considering that the packages haven't been updated
in a while and will likely not be updated in the near future, we are fine with this approach.
Testing instructions:
Pull this branch and run
npm run setupto make sure exisitng .js files are cleanedand .ts files are compiled. Then run
npm testto make sure all tests are passing.Proposed commit message: (wrap lines at 72 characters)
Migrated
nunjucks,htmlparser2andmarkdown-itpatches to TypeScript and ESM.Checklist: ☑️
Reviewer checklist:
Indicate the SEMVER impact of the PR:
At the end of the review, please label the PR with the appropriate label:
r.Major,r.Minor,r.Patch.Breaking change release note preparation (if applicable):