Skip to content

fix: resolve 4 bugs in termui - #3634

Closed
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-19430
Closed

fix: resolve 4 bugs in termui#3634
saurabhhhcodes wants to merge 1 commit into
Karanjot786:mainfrom
saurabhhhcodes:fix/termui-19430

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Added explicit radix to parseInt: without 10, strings like '0x1F' or '08' parse in unintended bases.
  • Added Number.EPSILON to Math.round: prevents floating-point drift (e.g. 1.005 * 100 rounding to 100 instead of 101).
  • Prevented interval leak: repeated mounts now clear the previous interval before scheduling a new one.
  • Removed redundant boolean comparison: x === true is equivalent to x (and x === false to !x), and shorter to read.

Type of Change

  • Bug fix (non-breaking change fixing an issue)

How Has This Been Tested?

  • Local manual testing

Checklist

  • My code follows the style guidelines
  • I have performed a self-review

Related Issue

Ref: #3633

Summary by CodeRabbit

  • Bug Fixes
    • Improved numeric hexadecimal entity decoding for more predictable results.
    • Prevented duplicate weather refresh timers during updates.
    • Improved switch knob positioning during animation.
    • Preserved confirmation prompt hints while simplifying boolean handling.

@github-actions github-actions Bot added area:examples Example apps. area:ui @termuijs/ui type:bug +10 pts. Bug fix. labels Aug 6, 2026
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The pull request applies four targeted fixes: limits hexadecimal entity parsing, prevents duplicate weather refresh timers, corrects switch knob rounding, and simplifies confirmation prompt hint selection.

Changes

Correctness fixes

Layer / File(s) Summary
Numeric parsing and rounding corrections
examples/rss-reader/src/index.tsx, packages/ui/src/Switch.ts
Hexadecimal entity parsing now reads at most eight digits. Switch knob rounding adds Number.EPSILON.
Refresh interval lifecycle
examples/weather/src/index.tsx
The weather refresh interval is stored on window.__interval and cleared before a new five-second interval is scheduled.
Confirmation hint condition
packages/ui/src/prompts.ts
promptConfirm uses a truthy default-value check while preserving the existing boolean hint mappings.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the four fixes and testing, but it omits required package, GSSoC, and template checklist sections and does not use a closing issue link. Add the missing required sections, identify the affected packages, complete the checklist and GSSoC details, and change Ref: #3633 to `Closes `#3633 if applicable.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies four bug fixes and follows the required type: short description format.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/ui/src/Switch.ts (1)

117-117: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add a regression test for the rounding boundary.

The existing packages/ui/src/Switch.test.ts rendering tests do not cover _animProgress values just below the 0.5 and 1.5 rounding thresholds. Add a deterministic test that drives those values and verifies the rendered knob position.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/ui/src/Switch.ts` at line 117, Add a deterministic regression test
in the Switch rendering tests that drives _animProgress to values just below 0.5
and 1.5, then verifies the rendered knob position produced by the knobPos
calculation. Cover both rounding boundaries without changing the implementation
in Switch.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@examples/rss-reader/src/index.tsx`:
- Around line 30-31: Update the entity decoder around codePoint so it rejects
hexadecimal entities containing more than eight digits by returning match,
rather than truncating with slice(2, 10). Validate that the parsed value is
within the Unicode range 0x0000–0x10FFFF before calling String.fromCodePoint,
preserving match for invalid or out-of-range entities.

---

Nitpick comments:
In `@packages/ui/src/Switch.ts`:
- Line 117: Add a deterministic regression test in the Switch rendering tests
that drives _animProgress to values just below 0.5 and 1.5, then verifies the
rendered knob position produced by the knobPos calculation. Cover both rounding
boundaries without changing the implementation in Switch.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 403a1b16-9b6c-436d-b691-283138d95c72

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and 822c0a9.

📒 Files selected for processing (4)
  • examples/rss-reader/src/index.tsx
  • examples/weather/src/index.tsx
  • packages/ui/src/Switch.ts
  • packages/ui/src/prompts.ts

Comment on lines +30 to 31
const codePoint = Number.parseInt(entity.slice(2, 10), 16);
return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : match;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Reject overlong hexadecimal entities instead of truncating them.

The regular expression still captures all hexadecimal digits. entity.slice(2, 10) discards digits after the first eight. For К, the decoder returns A instead of preserving the invalid entity. A first-eight-digit value can also exceed 0x10FFFF, so String.fromCodePoint can still throw RangeError.

If the entity has more than eight digits, return match. Also validate the Unicode range before calling String.fromCodePoint.

Proposed fix
-      const codePoint = Number.parseInt(entity.slice(2, 10), 16);
-      return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : match;
+      const digits = entity.slice(2);
+      if (digits.length > 8) return match;
+      const codePoint = Number.parseInt(digits, 16);
+      return Number.isFinite(codePoint) && codePoint <= 0x10ffff
+        ? String.fromCodePoint(codePoint)
+        : match;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const codePoint = Number.parseInt(entity.slice(2, 10), 16);
return Number.isFinite(codePoint) ? String.fromCodePoint(codePoint) : match;
const digits = entity.slice(2);
if (digits.length > 8) return match;
const codePoint = Number.parseInt(digits, 16);
return Number.isFinite(codePoint) && codePoint <= 0x10ffff
? String.fromCodePoint(codePoint)
: match;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@examples/rss-reader/src/index.tsx` around lines 30 - 31, Update the entity
decoder around codePoint so it rejects hexadecimal entities containing more than
eight digits by returning match, rather than truncating with slice(2, 10).
Validate that the parsed value is within the Unicode range 0x0000–0x10FFFF
before calling String.fromCodePoint, preserving match for invalid or
out-of-range entities.

@coderabbitai coderabbitai Bot mentioned this pull request Aug 6, 2026
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:examples Example apps. area:ui @termuijs/ui type:bug +10 pts. Bug fix.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant