Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: JS to TS : src/simulator/src/hotkey_binder/model/utils.js #428

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

ThatDeparted2061
Copy link

@ThatDeparted2061 ThatDeparted2061 commented Jan 23, 2025

Fixes #414

cc @niladrix719

The netlify link generated by netlify bot wont work, since many files are inter-related with JS specifying the format as .js while calling different files within themselves, just checking one file will throw error since the new format is .ts !

Summary by CodeRabbit

  • Refactor

    • Migrated utility functions from JavaScript to TypeScript
    • Enhanced type safety for storage methods and utility functions
    • Improved operating system detection and key combination handling
  • New Features

    • Added type-safe storage methods for object serialization and retrieval
    • Implemented enhanced utility functions for object manipulation and key detection

Copy link
Contributor

coderabbitai bot commented Jan 23, 2025

Warning

Rate limit exceeded

@ThatDeparted2061 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 1 minutes and 5 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 7106538 and b684935.

📒 Files selected for processing (1)
  • src/simulator/src/hotkey_binder/model/utils.ts (1 hunks)

Walkthrough

The pull request involves migrating the utils.js file to utils.ts, converting the utility functions and methods to TypeScript. The changes include adding type annotations to existing functions like set, get, objectSize, getKey, getOS, and checkRestricted. The core functionality remains the same, with improvements in type safety and explicit type declarations for better code quality and developer experience.

Changes

File Change Summary
src/simulator/src/hotkey_binder/model/utils.js Deleted JavaScript file
src/simulator/src/hotkey_binder/model/utils.ts New TypeScript file with type-safe implementations of utility functions

Assessment against linked issues

Objective Addressed Explanation
Typescript Integration in /simulator/src files [#414]

Poem

🐰 A rabbit's tale of code so neat,
TypeScript magic, now complete!
From .js to .ts we leap,
Typing functions with a cheerful beep!
Migration done, our code's more bright,
Compiler's smile shines pure and light! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Jan 23, 2025

Deploy Preview for circuitverse failed.

Name Link
🔨 Latest commit b684935
🔍 Latest deploy log https://app.netlify.com/sites/circuitverse/deploys/6796482e45b9500008775332

@ThatDeparted2061 ThatDeparted2061 changed the title refactor: JS to TS refactor: JS to TS : src/simulator/src/hotkey_binder/model/utils.js Jan 23, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (2)
src/simulator/src/hotkey_binder/model/utils.ts (2)

18-26: Improve type safety of object utilities.

While the generic implementation of getKey is good, both functions could be more type-safe.

-export function objectSize(obj: Record<string, any>): number {
+export function objectSize<T extends Record<string, unknown>>(obj: T): number {
     return Object.keys(obj).length;
 }

 export function getKey<T extends Record<string, any>>(obj: T, val: any): string | undefined {
-    return Object.keys(obj).find(key => obj[key] === val);
+    return Object.entries(obj).find(([_, value]) => value === val)?.[0];
 }

38-59: Improve maintainability and type safety of key restrictions.

The current implementation could benefit from better type safety and maintainability improvements.

+type ModifierKey = 'Ctrl' | 'Meta';
+type KeyCombination = `${ModifierKey} + ${string}`;

 export function checkRestricted(key: string): boolean {
-    const restrictedKeys: string[] = [
+    const restrictedKeys: KeyCombination[] = [
         'Ctrl + N', 'Ctrl + W', 'Ctrl + T', 'Ctrl + C', 'Ctrl + V',
         // ... other combinations
     ];

-    // Adjust for MacOS if needed
-    const modifiedKeys = getOS() === 'MacOS' 
-        ? restrictedKeys.map(value => 
-            value.startsWith('Ctrl') 
-                ? value.replace('Ctrl', 'Meta') 
-                : value
-          )
-        : restrictedKeys;
+    const isMacOS = getOS() === 'MacOS';
+    const modifiedKeys = isMacOS
+        ? restrictedKeys.map(value => value.replace('Ctrl', 'Meta') as KeyCombination)
+        : restrictedKeys;

     return modifiedKeys.includes(key);
 }

Also consider extracting the restricted keys to a configuration file for better maintainability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6eae6ed and 7106538.

📒 Files selected for processing (2)
  • src/simulator/src/hotkey_binder/model/utils.js (0 hunks)
  • src/simulator/src/hotkey_binder/model/utils.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • src/simulator/src/hotkey_binder/model/utils.js
🔇 Additional comments (1)
src/simulator/src/hotkey_binder/model/utils.ts (1)

1-59: Verify TypeScript migration impact.

Let's verify that all dependent files are properly updated to handle the TypeScript migration.

✅ Verification successful

TypeScript migration is properly implemented

The migration of utils.ts is part of the project's ongoing TypeScript integration effort. All current imports from dependent files are compatible with the new TypeScript implementation, and no immediate changes are required in the dependent files. This follows the pattern of gradual migration as indicated in the project's todos.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for any remaining .js imports of this file
rg -l "from ['\"].*utils\.js['\"]" --type ts --type vue

# Check for any files still using the old JavaScript version
rg -l "import.*from ['\"].*utils['\"]" --type js

# Look for potential usage of removed or renamed exports
ast-grep --pattern 'import { $_ } from "*/utils"'

Length of output: 1762


Script:

#!/bin/bash
# Check what's being imported from utils
rg "import \{.*\} from .*utils" -A 1

# Look for migration documentation
fd "MIGRATION|UPGRADE" --type f
cat README.md

Length of output: 12188

src/simulator/src/hotkey_binder/model/utils.ts Outdated Show resolved Hide resolved
src/simulator/src/hotkey_binder/model/utils.ts Outdated Show resolved Hide resolved
src/simulator/src/hotkey_binder/model/utils.ts Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript Integration in /simulator/src files
1 participant