refactor: convert events.js to TypeScript#812
refactor: convert events.js to TypeScript#812pantha704 wants to merge 1 commit intoCircuitVerse:mainfrom
Conversation
Part of CircuitVerse#661 (1 file per PR rule) - Added return type annotations for all functions - Added parameter type annotations (copyData: string, copyList: any[]) - Replaced var with const/let throughout - Added Record types for internal objects - Added JSDoc documentation - Applied same changes to v1/src No logic changes - strictly TypeScript migration.
WalkthroughThe pull request adds explicit TypeScript typing to four exported functions in the events.ts file: 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
✅ Deploy Preview for circuitverse ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (3)
v1/src/simulator/src/events.ts (2)
65-90: Potential division by zero when pasting empty or wire-only content.If all objects in
tempScopeare Wires,countremains 0, causingapproxXandapproxYto becomeNaN. This would corrupt object positions.Proposed fix
- approxX /= count - approxY /= count + if (count > 0) { + approxX /= count + approxY /= count + }
193-199: Fix array iteration over getDependencies() result.
getDependencies()returns an array of dependency IDs, butObject.keys(dependencyList)iterates over array indices ('0', '1', etc.) rather than the IDs themselves. This causesscopeList[dependency]lookups to fail. Iterate directly over the array instead:dependencyList.forEach((dependency) => { ... })src/simulator/src/events.ts (1)
65-90: Same division by zero risk as v1 version.When all pasted objects are Wires,
countis 0, leading toNaNcoordinates.Proposed fix
- approxX /= count - approxY /= count + if (count > 0) { + approxX /= count + approxY /= count + }
🧹 Nitpick comments (4)
v1/src/simulator/src/events.ts (3)
139-140: Consider using explicitundefinedreturn for clarity.The implicit return when
copyList.length === 0works but could be more explicit given thestring | undefinedreturn type annotation.Suggested improvement
- if (copyList.length === 0) return + if (copyList.length === 0) return undefined
223-224: Same implicit return pattern ascut().Consistency with explicit
return undefinedwould improve readability.
331-341: Consider adding explicit type for scope parameter.While the default value provides type inference, an explicit type annotation would improve clarity and catch type mismatches at call sites.
Suggested improvement
-export function selectAll(scope = globalScope): void { +export function selectAll(scope: typeof globalScope = globalScope): void {Based on learnings,
globalScopeis a global variable that should be typed by extending the Window interface. Usingtypeof globalScopehere would leverage that typing.src/simulator/src/events.ts (1)
331-341: Consider explicit type annotation for scope parameter.Same suggestion as v1 version - using
typeof globalScopewould provide better type safety at call sites while maintaining the current default value behavior.

Part of #661 (1 file per PR rule)
Changes
Converted
events.jsto TypeScript in bothsrc/andv1/src/.TypeScript Improvements
: void,: string | undefined)copyData: string,copyList: any[],cutflag: boolean)varwithconst/letthroughoutRecord<string, any>andRecord<string, boolean>types for internal objectssaveScope(id: string): void)No Logic Changes
paste,cut,copy,selectAllbun run build)Code Understanding and AI Usage
Summary by CodeRabbit
Refactor
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.