fix: resolve 4 bugs in termui - #3418
Conversation
📝 WalkthroughWalkthroughThe changes replace two string emptiness comparisons with trimmed-length checks and replace one global ChangesValidation checks
Estimated code review effort: 1 (Trivial) | ~5 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
examples/calculator/src/index.tsxexamples/chat-app/src/index.tsxpackages/ui/src/prompts.ts
| const n = parseInt(trimmed, 10); | ||
| if (!isNaN(n) && n >= 1 && n <= choices.length) { | ||
| if (!Number.isNaN(n) && n >= 1 && n <= choices.length) { |
There was a problem hiding this comment.
🎯 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 || trueRepository: 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),
})
}
JSRepository: 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.
| 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.
Description
This PR fixes real bugs found in the codebase:
trim()to''misses whitespace-only input;.trim().length === 0is explicit.x === trueis equivalent tox(andx === falseto!x), and shorter to read.isNaNwithNumber.isNaN: the global version coerces its argument, soisNaN('1')returns false whileNumber.isNaNis strict.trim()to''misses whitespace-only input;.trim().length === 0is explicit.Type of Change
How Has This Been Tested?
Checklist
Related Issue
Ref: #3417
Summary by CodeRabbit