fix: resolve 4 bugs in termui - #3495
Conversation
📝 WalkthroughWalkthroughThe PR updates numeric handling in three UI paths: switch knob rounding, tree selection comparison, and prompt choice validation. ChangesNumeric handling corrections
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 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/TreeSelect.ts`:
- Line 185: Replace the numeric comparator `a - b` in the sort function for
sortedA with a typed numeric-string comparator that converts string values to
numbers for comparison, then apply the same comparator to sortedB to ensure both
arrays use consistent comparison logic. This will properly handle numeric string
values like '10' and '2' so they compare correctly in strict type checking mode.
🪄 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: e0e209ce-b500-4e3e-ab0a-9c0454ac0861
📒 Files selected for processing (3)
packages/ui/src/Switch.tspackages/ui/src/TreeSelect.tspackages/ui/src/prompts.ts
| function _valuesEqual(a: string[], b: string[]): boolean { | ||
| if (a.length !== b.length) return false; | ||
| const sortedA = [...a].sort(); | ||
| const sortedA = [...a].sort((a, b) => a - b); |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C 3 'function _valuesEqual|sortedA|sortedB' packages/ui/src/TreeSelect.ts
fd -a 'tsconfig*.json' -x rg -n '"strict"\s*:\s*true' '{}'Repository: Karanjot786/TermUI
Length of output: 713
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Inspect the helper and package TypeScript config/package.json metadata without executing repository code.
sed -n '170,195p' packages/ui/src/TreeSelect.ts
printf '\n--- relevant tsconfig files ---\n'
for f in $(fd 'tsconfig.*\.json' .); do
printf '\n### %s\n' "$f"
python3 - <<'PY' "$f"
import json, sys
p=sys.argv[1]
try:
data=json.load(open(p))
except Exception as e:
print(f"ERR: {e}")
raise SystemExit(2)
print(p, data.get("compilerOptions", {}).get("strict"))
PY
doneRepository: Karanjot786/TermUI
Length of output: 5161
Use a typed comparator for both sorted arrays.
packages/ui is configured with strict type checking, and a - b on string[] values does not type-check. Sort both sortedA and sortedB with the same numeric-string comparator so values such as ['10', '2'] and ['2', '10'] compare equal.
🤖 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/TreeSelect.ts` at line 185, Replace the numeric comparator `a
- b` in the sort function for sortedA with a typed numeric-string comparator
that converts string values to numbers for comparison, then apply the same
comparator to sortedB to ensure both arrays use consistent comparison logic.
This will properly handle numeric string values like '10' and '2' so they
compare correctly in strict type checking mode.
Source: Coding guidelines
Description
This PR fixes real bugs found in the codebase:
.sort()coerces elements to strings, so[10, 9, 2]sorts as[10, 2, 9]; numeric comparator sorts correctly.Number.EPSILONtoMath.round: prevents floating-point drift (e.g.1.005 * 100rounding to 100 instead of 101).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.Type of Change
How Has This Been Tested?
Checklist
Related Issue
Ref: #3494
Summary by CodeRabbit