Skip to content

fix: resolve 4 bugs in termui - #3418

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

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

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • Simplified empty-string validation: comparing trim() to '' misses whitespace-only input; .trim().length === 0 is explicit.
  • Removed redundant boolean comparison: x === true is equivalent to x (and x === false to !x), and shorter to read.
  • Replaced global isNaN with Number.isNaN: the global version coerces its argument, so isNaN('1') returns false while Number.isNaN is strict.
  • Simplified empty-string validation: comparing trim() to '' misses whitespace-only input; .trim().length === 0 is explicit.

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: #3417

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of blank input in calculator expressions and chat message parsing.
    • Strengthened validation of numeric selections in prompts for more reliable input handling.

@github-actions github-actions Bot added area:examples Example apps. area:ui @termuijs/ui labels Aug 3, 2026
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The changes replace two string emptiness comparisons with trimmed-length checks and replace one global isNaN call with Number.isNaN. Evaluation, paragraph handling, and selection behavior remain otherwise unchanged.

Changes

Validation checks

Layer / File(s) Summary
Explicit validation conditions
examples/calculator/src/index.tsx, examples/chat-app/src/index.tsx, packages/ui/src/prompts.ts
The calculator and chat app use trimmed string lengths to detect blank input. promptSelect uses Number.isNaN for parsed choice validation.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Possibly related PRs

Suggested reviewers: karanjot786

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the changes and testing, but it omits required package, full checklist, GSSoC, and linked-issue details. Add all template sections, identify affected packages, complete the required checklist, provide GSSoC details, and change the issue reference to a closing link such as Closes #3417``.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies a bug-fix pull request and summarizes its main purpose.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

@github-actions github-actions Bot added the type:bug +10 pts. Bug fix. label Aug 3, 2026

@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

🤖 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 `@packages/ui/src/prompts.ts`:
- Around line 115-116: The choice parser in the prompt handling logic currently
accepts partial numeric inputs; update the parsing around parseInt to use Number
on the trimmed value and require Number.isInteger(n) before indexing choices,
while preserving the existing range validation. Add regression cases for "2abc"
and "2.5" in the prompt tests to verify both are rejected.
🪄 Autofix (Beta)

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: 8e8c13fd-60e1-4748-a9a9-c9fd1e3d98d5

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and 928f172.

📒 Files selected for processing (3)
  • examples/calculator/src/index.tsx
  • examples/chat-app/src/index.tsx
  • packages/ui/src/prompts.ts

Comment on lines 115 to +116
const n = parseInt(trimmed, 10);
if (!isNaN(n) && n >= 1 && n <= choices.length) {
if (!Number.isNaN(n) && n >= 1 && n <= choices.length) {

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 | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== locate prompts files =="
fd -a 'prompts\.ts|prompts\.test\.ts' . | sed 's#^\./##'

echo "== relevant sections =="
for f in $(fd 'prompts\.ts$|prompts\.test\.ts$' .); do
  echo "--- $f ($(wc -l < "$f") lines) ---"
  nl -ba "$f" | sed -n '1,180p'
done

echo "== deterministic parseInt behavior =="
node - <<'JS'
const inputs = ["2abc", "2.5", "09", "  2  ", "abc", "", "1"];
for (const s of inputs) {
  const int = parseInt(s, 10);
  const num = Number(s);
  const trimmed = s.trim();
  const asInt = parseInt(trimmed, 10);
  console.log(JSON.stringify(s), {parseInt: asInt, Number: num, isInteger: Number.isInteger(num), isNaN:Number.isNaN(num)})
}
JS

echo "== tests around prompt selection =="
nl -ba packages/ui/src/prompts.test.ts | sed -n '45,75p' 2>/dev/null || true

Repository: Karanjot786/TermUI

Length of output: 541


🏁 Script executed:

#!/bin/bash
set -euo pipefail

for f in packages/ui/src/prompts.ts packages/ui/src/prompts.test.ts; do
  echo "--- $f ($(wc -l < "$f") lines) ---"
  grep -n . "$f" | sed -n '1,190p'
done

echo "== deterministic parseInt vs Number behavior =="
node - <<'JS'
const inputs = ["2abc", "2.5", "09", "  2  ", "abc", "", "1"];
for (const s of inputs) {
  const trimmed = s.trim();
  const asInt = parseInt(trimmed, 10);
  const asNum = Number(trimmed);
  console.log(JSON.stringify(s), {
    parseInt: asInt,
    Number: asNum,
    isInteger: Number.isInteger(asNum),
    isNaN: Number.isNaN(asNum),
  })
}
JS

Repository: Karanjot786/TermUI

Length of output: 11499


Reject partial numeric inputs for choices.

parseInt(trimmed, 10) accepts inputs like "2abc" and "2.5" and maps them to choice 2. Use Number.trimmed) and require Number.isInteger(n) before indexing choices. Add regression cases for "2abc" and "2.5" in packages/ui/src/prompts.test.ts.

Proposed fix
-                const n = parseInt(trimmed, 10);
-                if (!Number.isNaN(n) && n >= 1 && n <= choices.length) {
+                const n = Number(trimmed);
+                if (Number.isInteger(n) && n >= 1 && n <= choices.length) {
📝 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 n = parseInt(trimmed, 10);
if (!isNaN(n) && n >= 1 && n <= choices.length) {
if (!Number.isNaN(n) && n >= 1 && n <= choices.length) {
const n = Number(trimmed);
if (Number.isInteger(n) && n >= 1 && n <= choices.length) {
🤖 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/prompts.ts` around lines 115 - 116, The choice parser in the
prompt handling logic currently accepts partial numeric inputs; update the
parsing around parseInt to use Number on the trimmed value and require
Number.isInteger(n) before indexing choices, while preserving the existing range
validation. Add regression cases for "2abc" and "2.5" in the prompt tests to
verify both are rejected.

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