Fix: Add error handling for JSON.parse in trackpad component#319
Fix: Add error handling for JSON.parse in trackpad component#319upendra512 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Conversation
Add error handling for JSON.parse when reading 'rein_invert' from localStorage to prevent component crashes on malformed data. Fixes AOSSIE-Org#308 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
WalkthroughThe trackpad component's Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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 Tip CodeRabbit can use oxc to improve the quality of JavaScript and TypeScript code reviews.Add a configuration file to your project to customize how CodeRabbit runs oxc. |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/routes/trackpad.tsx`:
- Around line 33-41: The useState initializer for invertScroll in the Trackpad
component currently returns JSON.parse(s) directly which can yield non-boolean
and widen the state to any; change the state to an explicit boolean type (e.g.,
useState<boolean>) and guard the parsed value so only a true boolean is accepted
(parse, check typeof parsed === "boolean", and return parsed, otherwise return
false) while preserving the try/catch fallback to false; update references to
the invertScroll state accordingly.
🪄 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: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: af1013ac-2b55-4222-bef5-224496df20e4
📒 Files selected for processing (1)
src/routes/trackpad.tsx
| const [invertScroll] = useState(() => { | ||
| if (typeof window === "undefined") return false | ||
| const s = localStorage.getItem("rein_invert") | ||
| return s ? JSON.parse(s) : false | ||
| if (!s) return false | ||
| try { | ||
| return JSON.parse(s) | ||
| } catch { | ||
| return false | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Verify expected type at hook boundary and inspect current state initialization.
fd 'useTrackpadGesture\.(ts|tsx)$' --exec sed -n '1,220p' {}
rg -nP -C3 'const \[invertScroll\]\s*=\s*useState' src/routes/trackpad.tsxRepository: AOSSIE-Org/Rein
Length of output: 6403
🏁 Script executed:
head -10 src/routes/trackpad.tsx | cat -nRepository: AOSSIE-Org/Rein
Length of output: 723
Constrain parsed localStorage value to boolean and type the state explicitly.
Line 38 returns JSON.parse(s) directly, which makes invertScroll effectively any and accepts non-boolean JSON (e.g., "1" or {}). Keep the crash fix, but enforce a boolean guard.
Proposed fix
- const [invertScroll] = useState(() => {
+ const [invertScroll] = useState<boolean>(() => {
if (typeof window === "undefined") return false
const s = localStorage.getItem("rein_invert")
if (!s) return false
try {
- return JSON.parse(s)
+ const parsed: unknown = JSON.parse(s)
+ return typeof parsed === "boolean" ? parsed : false
} catch {
return false
}
})This addresses the TypeScript guideline: "Avoid 'any', use explicit types".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/routes/trackpad.tsx` around lines 33 - 41, The useState initializer for
invertScroll in the Trackpad component currently returns JSON.parse(s) directly
which can yield non-boolean and widen the state to any; change the state to an
explicit boolean type (e.g., useState<boolean>) and guard the parsed value so
only a true boolean is accepted (parse, check typeof parsed === "boolean", and
return parsed, otherwise return false) while preserving the try/catch fallback
to false; update references to the invertScroll state accordingly.
Description
Fixes Issue #308 - JSON.parse without try/catch crashes trackpad component
Changes
JSON.parse()when readingrein_invertfrom localStorageTesting
Related Issue
Closes #308
First-time contributor ?? - Excited to contribute to AOSSIE!
Summary by CodeRabbit
Release Notes