Skip to content

fix: resolve 4 bugs in termui - #3653

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

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

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Description

This PR fixes real bugs found in the codebase:

  • 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.
  • Replaced global isNaN with Number.isNaN: the global version coerces its argument, so isNaN('1') returns false while Number.isNaN is strict.
  • Fixed default sort: .sort() coerces elements to strings, so [10, 9, 2] sorts as [10, 2, 9]; numeric comparator sorts correctly.

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

Summary by CodeRabbit

  • Bug Fixes
    • Prevented duplicate weather refresh timers, ensuring updates continue at the intended five-second interval.
    • Improved select prompt input validation for more reliable handling of invalid numeric entries.
    • Ensured collected package dependencies are ordered consistently during registry generation.

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

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The changes fix three independent behaviors: weather interval replacement, strict numeric validation in select prompts, and numeric ordering of collected registry dependencies.

Changes

Weather timer management

Layer / File(s) Summary
Clear the existing weather interval
examples/weather/src/index.tsx
The refresh setup clears the existing interval before creating a new five-second fetchWeather interval.

Prompt input validation

Layer / File(s) Summary
Use strict NaN validation
packages/ui/src/prompts.ts
promptSelect uses Number.isNaN to validate parsed input.

Registry dependency sorting

Layer / File(s) Summary
Sort dependency specifiers numerically
scripts/build-registry.ts
collectDeps sorts unique @termuijs/* dependency specifiers with a numeric comparator.

Estimated code review effort: 2 (Simple) | ~10 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 explains the bug fixes but omits the required package section and GSSoC profile and uses non-closing issue syntax. Add the package list and GSSoC profile, complete the required checklist, and change Ref: #3652 to `Closes `#3652.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title uses the required fix: prefix and clearly identifies a four-bug fix in TermUI.
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.

@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: 2

🤖 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`:
- Line 116: Update the prompt selection validation around the numeric parsing
condition to validate the complete raw input before accepting it, rather than
relying on parseInt’s partial parsing. Require a valid integer index within 1
through choices.length, rejecting values such as “2abc” and “2.5”; add
regression tests covering malformed-prefix and decimal inputs.

In `@scripts/build-registry.ts`:
- Line 47: Update the sorting comparator in collectDeps() to compare dependency
specifier strings lexically rather than subtracting them numerically. Preserve
the returned string[] and ensure the ordering matches the lexical order expected
by the existing tests.
🪄 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: c96cc369-8a35-4346-bce7-9efa57987373

📥 Commits

Reviewing files that changed from the base of the PR and between 6c7584e and 409a362.

📒 Files selected for processing (3)
  • examples/weather/src/index.tsx
  • packages/ui/src/prompts.ts
  • scripts/build-registry.ts

}
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 | 🔴 Critical | ⚡ Quick win

Validate the complete input before accepting a selection.

Number.isNaN(n) is equivalent to the previous check here because n is already a number from parseInt. It does not reject inputs with numeric prefixes. For example, 2abc and 2.5 both parse as 2, so the prompt can select the wrong option.

Parse the complete input and require an integer index. Add regression tests for malformed and decimal input.

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) {
🤖 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` at line 116, Update the prompt selection
validation around the numeric parsing condition to validate the complete raw
input before accepting it, rather than relying on parseInt’s partial parsing.
Require a valid integer index within 1 through choices.length, rejecting values
such as “2abc” and “2.5”; add regression tests covering malformed-prefix and
decimal inputs.

Comment thread scripts/build-registry.ts
let m: RegExpExecArray | null;
while ((m = re.exec(content)) !== null) deps.add(m[1]!);
return [...deps].sort();
return [...deps].sort((a, b) => a - b);

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 | 🔴 Critical | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== files =="
git ls-files | rg '(^|/)build-registry(\.test)?\.ts$' || true

echo "== build-registry.ts =="
if [ -f scripts/build-registry.ts ]; then
  nl -ba scripts/build-registry.ts | sed -n '1,90p'
fi

echo "== related tests =="
if [ -f scripts/build-registry.test.ts ]; then
  nl -ba scripts/build-registry.test.ts | sed -n '1,180p'
fi

echo "== tsconfig(s) =="
fd -a 'tsconfig.*\.json$' . -d 3 | sort | sed 's#^\./##' | while read -r f; do
  echo "--- $f"
  nl -ba "$f" | sed -n '1,140p'
done

echo "== TypeScript availability and arithmetic probe =="
if command -v tsc >/dev/null 2>&1; then
  echo "tsc version:"
  tsc --version
  tmp="$(mktemp -d)"
  cat > "$tmp/probe.ts" <<'TS'
const deps: string[] = ['apple', 'banana', 'apricot'];
deps.sort((a: string, b: string) => a - b);
TS
  (tsc --noEmit --strict "$tmp/probe.ts" 2>&1 | sed -n '1,120p') || true
else
  node - <<'JS'
const deps = ['apple', 'banana', 'apricot'];
const out = [...deps].sort((a, b) => a - b);
console.log(JSON.stringify(out));
JS
fi

Repository: Karanjot786/TermUI

Length of output: 289


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== build-registry.ts =="
if [ -f scripts/build-registry.ts ]; then
  cat -n scripts/build-registry.ts | sed -n '1,90p'
fi

echo "== related tests =="
if [ -f scripts/build-registry.test.ts ]; then
  cat -n scripts/build-registry.test.ts | sed -n '1,180p'
fi

echo "== tsconfig(s) =="
find . -maxdepth 3 -name 'tsconfig*.json' -print | sort | while read -r f; do
  echo "--- $f"
  cat -n "$f" | sed -n '1,160p'
done

echo "== TypeScript availability and arithmetic probe =="
if command -v tsc >/dev/null 2>&1; then
  echo "tsc version:"
  tsc --version
  tmp="$(mktemp -d)"
  cat > "$tmp/probe.ts" <<'TS'
const deps: string[] = ['apple', 'banana', 'apricot'];
deps.sort((a: string, b: string) => a - b);
TS
  (tsc --noEmit --strict "$tmp/probe.ts" 2>&1 | sed -n '1,120p') || true
else
  echo "tsc not available"
  node - <<'JS'
const deps = ['apple', 'banana', 'apricot'];
const out = [...deps].sort((a, b) => a - b);
console.log(JSON.stringify(out));
JS
fi

Repository: Karanjot786/TermUI

Length of output: 34283


Use a string comparator for dependency specifiers.

collectDeps() returns string[], but a - b is not valid TypeScript arithmetic for strings. If emitted, NaN values also leave the dependency array unsorted instead of matching the lexical order expected by scripts/build-registry.test.ts:127-139.

-  return [...deps].sort((a, b) => a - b);
+  return [...deps].sort((a, b) => a.localeCompare(b));
📝 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
return [...deps].sort((a, b) => a - b);
return [...deps].sort((a, b) => a.localeCompare(b));
🤖 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 `@scripts/build-registry.ts` at line 47, Update the sorting comparator in
collectDeps() to compare dependency specifier strings lexically rather than
subtracting them numerically. Preserve the returned string[] and ensure the
ordering matches the lexical order expected by the existing tests.

Source: Coding guidelines

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