Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/unix-manual-regex-lastindex.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@node-core/doc-kit': patch
---

Fix `isTextWithUnixManual` intermittently missing valid Unix manual page
references: the shared `unixManualPage` regex is global, so its `lastIndex`
persisted between `.test()` calls during tree traversal.
6 changes: 4 additions & 2 deletions src/utils/queries/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ export const UNIST = {
* @param {import('@types/mdast').Text} text
* @returns {boolean}
*/
isTextWithUnixManual: ({ type, value }) =>
type === 'text' && QUERIES.unixManualPage.test(value),
isTextWithUnixManual: ({ type, value }) => {
QUERIES.unixManualPage.lastIndex = 0; // Reset the lastIndex to ensure proper matching
return type === 'text' && QUERIES.unixManualPage.test(value);
},
Comment on lines +44 to +47

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example of something it would miss?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dug into this, and honestly: with the current call chain, nothing is observably missed today. isTextWithUnixManual is only used as a visit() predicate (src/generators/metadata/utils/parse.mjs:142), and every successful .test() is immediately followed by the visitor's node.value.replace(QUERIES.unixManualPage, ...) (src/generators/metadata/utils/visitors.mjs:33, same regex instance)

RegExp.prototype[@@replace], step 9, on a global regex resets lastIndex to 0. A failing .test() also resets it, so lastIndex is always 0 at the start of each test. Verified empirically: pre-fix vs post-fix predicates over all of node/doc/api/*.md produce identical matches.

So this is hardening against shared mutable regex state, not a fix for a currently visible bug. Happy to add a small unit test calling the predicate twice to lock it in, or close if you'd rather not carry the extra line.


/**
* @param {import('@types/mdast').Link} link
Expand Down
Loading