Skip to content
Closed
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
2 changes: 1 addition & 1 deletion examples/calculator/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class CalculatorApp extends Widget {
}

private evaluate() {
if (this.expression.trim() === "") return;
if (this.expression.trim().length === 0) return;
const oldExpr = this.expression;
this.result = safeEval(this.expression);
if (this.result !== null && !this.result.startsWith("Error")) {
Expand Down
2 changes: 1 addition & 1 deletion examples/chat-app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function parseBlocks(text: string): Block[] {
}

// ── Handle Paragraphs ────────────────────────
if (line.trim() === '') {
if (line.trim().length === 0) {
blocks.push({
type: 'paragraph',
text: '',
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ async function promptSelect<T = string>(options: SelectPromptOptions<T>): Promis
return;
}
const n = parseInt(trimmed, 10);
if (!isNaN(n) && n >= 1 && n <= choices.length) {
if (!Number.isNaN(n) && n >= 1 && n <= choices.length) {
Comment on lines 115 to +116

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.

rl.close();
resolve(choices[n - 1].value);
return;
Expand Down
Loading