Implements the gate-consolidation Agent Note from the NIH dependency audit: - Shared markdownFences helper in scripts/markdown.ts (mdast code-node visit); doc-typecheck and verify-type-equiv extract fences through it; md-fences.ts and the duplicated extractEquivBlocks regex scanner are deleted; markdownProseLines derives fenced lines from parsed code-node positions instead of a second fence regex. - publint-all.ts and verify-built-package-invariants.mjs parse argv with node:util parseArgs instead of hand-stepped parseOptions copies. - Five straggler readdirSync walks become globSync: verify-runtime-closure, dev-web discoverPluginDirs, verify-package-paths realPackageNames, verify-client-domain-graph listSources, publint-all addPath. The dirent-diagnostic walks in check-workspace-constraints.ts and clean.ts stay. Behavior parity verified: pnpm run doc-sync and every rewritten gate produce byte-identical output before and after on this tree. Moves the owning Agent Note proposed -> implemented and re-records its pair.
4.7 KiB
4.7 KiB
Agent Note: Consolidate gate scripts on already-present deps and builtins
Status: implemented
English | 中文
Problem
The scripts/ gates mostly used the right tools (node:fs globSync in 15+ gates, mdast/micromark in the markdown gates), but a handful of stragglers hand-rolled what a sibling gate already did with an existing dependency or builtin:
- Duplicated fence scanners.
scripts/md-fences.ts(~55 lines, consumed bydoc-typecheck.ts) andextractEquivBlocksinscripts/verify-type-equiv.ts(~39 lines) were two copies of the same regex line-scanner for fenced code blocks, whilescripts/verify-mermaid.tsalready extracted fences by visiting mdastcodenodes — andmarkdownProseLinesinscripts/markdown.tsitself parsed to mdast but then hand-tracked fence state with a second regex. The regex scanners only recognized backtick fences at column 0, so they silently disagreed with the mdast-based gates on tilde and indented fences. - Hand-rolled argv parsing.
parseOptionsinscripts/publint-all.tsand its near-identical copy inscripts/verify-built-package-invariants.mjs(~26 lines) stepped argv indexes manually, while sibling scripts (verify-runtime-closure.ts,build-exe-for-python-sdk.ts,packages/sdk/scripts/src/args.ts) already used thenode:utilparseArgsbuiltin. - Hand-rolled directory walks. Five sites re-derived nested
readdirSyncwalks thatglobSynccovers:verify-runtime-closure.ts(packages + vendor manifests),dev-web.tsdiscoverPluginDirs,verify-package-paths.tsrealPackageNames,verify-client-domain-graph.tslistSources, andpublint-all.tsaddPath(~55–65 lines total).scripts/package-invariants.tsshows the one-lineglobSynctemplate.
No new dependency was needed anywhere; every replacement is an existing devDep or a Node builtin.
Decision
- A shared mdast fence helper,
markdownFencesinscripts/markdown.ts, visitscodenodes for the language, full info string, body, and 1-based opening-fence line;doc-typecheck.tsandverify-type-equiv.tsextract fences through it.md-fences.tsand the duplicatedextractEquivBlocksscanner are deleted, andmarkdownProseLinesderives fenced lines from the parsedcodenodes' positions instead of a second regex. - Both CLIs parse argv via
parseArgs; unknown options and missing values still fail loud, withparseArgs's own error text instead of the bespoke usage strings. - The five straggler walks use
globSync. The walks incheck-workspace-constraints.tsandclean.tsstay: they need dirent-level detail to diagnose malformed trees, which glob-by-pattern cannot report.
Alternatives considered
- A new glob/walking dependency (
tinyglobby,fdir). Rejected: the builtin already won repo-wide; these were stragglers, not a gap. p-mapforpublint-all.ts's ~19-line ordered worker pool. Deliberately left out: one new devDep for one small deletion is at the edge of the dependency policy bar, and the pool's requirements (bounded workers, deterministic order, env override) are documented in the parallel-gates note. Fold it in only ifp-mapearns a second consumer.- Leaving the fence scanners. Rejected: two drifting copies of a parser beside a third correct implementation is exactly the duplication the shared
markdown.tshelper exists to prevent, and the column-0-backtick-only limitation was a latent inconsistency between sibling gates.
Consequences
- One fence parser: every markdown gate now classifies fences through mdast, so tilde, indented, and 4-backtick container fences behave identically everywhere. The docs tree contained no fence shape the regex scanners mishandled, so gate results are unchanged on the tree that landed the swap:
pnpm run doc-syncand each rewritten gate ran before and after with byte-identical output (doc-typecheckblock/opt-out counts,verify-type-equivmatch counts,publint,verify-built-package-invariants,verify-runtime-closure,verify-package-paths,verify-client-domain-graph, and both package-README prose gates). verify-type-equivno longer errors on an unterminated fence: mdast closes an unterminated block at end-of-file, so such a block reaches the manifest checks and still fails there as an orphan or drift rather than as a dedicated scanner error. Thedoc-typecheckscanner never had that error path.parseArgskeeps the last value of a duplicated option instead of erroring and consumes a---prefixed next token as a value; both are dev-tool edge cases the tests don't pin, accepted in exchange for deleting the two bespoke parsers.