Skip to content

fix: prevent TypeError in countMarkdownHeadings for non-string input#11606

Draft
roomote[bot] wants to merge 1 commit intomainfrom
fix/11605-markdown-type-guard
Draft

fix: prevent TypeError in countMarkdownHeadings for non-string input#11606
roomote[bot] wants to merge 1 commit intomainfrom
fix/11605-markdown-type-guard

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Feb 19, 2026

This PR attempts to address Issue #11605. Feedback and guidance are welcome.

Problem

countMarkdownHeadings() in webview-ui/src/utils/markdown.ts crashes with TypeError: n.replace is not a function when a non-string truthy value (e.g. a number or object from an API response) is passed through OpenMarkdownPreviewButton. The existing guard if (!text) only catches falsy values, so truthy non-strings slip through.

Fix

  • Changed the type guard from if (!text) to if (typeof text !== "string" || !text) so any non-string value returns 0 early
  • Added test cases covering non-string inputs (number, object, array, boolean, null)

Testing

  • All 6 tests pass in webview-ui/src/utils/__tests__/markdown.spec.ts
  • Lint and type-check pass across the full monorepo

Start a new Roo Code Cloud session on this branch

…ror on non-string input

Fixes #11605. The existing guard `if (!text)` only catches falsy values.
When a truthy non-string value (e.g. number or object from an API response)
is passed through OpenMarkdownPreviewButton, it passes the guard and
text.replace() throws "TypeError: n.replace is not a function".

Changed the guard to `typeof text !== "string"` so any non-string value
returns 0 early. Added test cases for non-string inputs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments