Skip to content

refactor: convert subcircuit.js to TypeScript#813

Open
pantha704 wants to merge 1 commit intoCircuitVerse:mainfrom
pantha704:ts-subcircuit#661
Open

refactor: convert subcircuit.js to TypeScript#813
pantha704 wants to merge 1 commit intoCircuitVerse:mainfrom
pantha704:ts-subcircuit#661

Conversation

@pantha704
Copy link
Contributor

@pantha704 pantha704 commented Jan 17, 2026

Part of #661 (1 file per PR rule)

Changes

Converted subcircuit.js to TypeScript in both src/ and v1/src/.

TypeScript Improvements

  • Added class property declarations with explicit types
  • Added method return type annotations (: void, : boolean, : string, : any, : [string, number, number])
  • Replaced var with const/let throughout
  • Added Record<string, any[]> type for temp_map objects
  • Added JSDoc documentation for all methods
  • Constructor parameter types added

No Logic Changes

  • ✅ Same class structure: SubCircuit extends CircuitElement
  • ✅ Same exported functions: loadSubCircuit, createSubCircuitPrompt
  • ✅ Same behavior, only added type safety
  • ✅ Build passes (bun run build)

Code Understanding and AI Usage

  • I have reviewed every single line of the changes
  • I can explain the purpose of each type annotation
  • No logic was changed, only types added

Summary by CodeRabbit

  • Refactor
    • Improved internal code structure with comprehensive type annotations and enhanced type safety throughout the subcircuit system. These changes strengthen codebase stability and reduce potential runtime errors for more reliable operation.

✏️ Tip: You can customize this high-level summary in your review settings.

Part of CircuitVerse#661 (1 file per PR rule)

- Added class property declarations with types
- Added method return type annotations (: void, : boolean, : string, : any)
- Replaced var with const/let throughout
- Added Record<string, any[]> for temp_map objects
- Added JSDoc documentation for all methods
- Applied same changes to v1/src

No logic changes - strictly TypeScript migration.
@netlify
Copy link

netlify bot commented Jan 17, 2026

Deploy Preview for circuitverse ready!

Name Link
🔨 Latest commit d4456d7
🔍 Latest deploy log https://app.netlify.com/projects/circuitverse/deploys/696b471f4774370008036c5b
😎 Deploy Preview https://deploy-preview-813--circuitverse.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 43 (🔴 down 1 from production)
Accessibility: 73 (no change from production)
Best Practices: 92 (no change from production)
SEO: 82 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify project configuration.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 17, 2026

Walkthrough

This change introduces comprehensive TypeScript type annotations to the SubCircuit class in subcircuit.ts. The modifications add explicit types to function signatures, constructor parameters, class fields, method return types, and local variables. Variable declarations are updated from var to let or const for proper scoping. Method signatures now include return type annotations (e.g., void, any, boolean, string, tuple types), and constructor parameters are typed with specific constraints such as x: number, y: number, and id: string | undefined. The refactoring maintains existing runtime behavior while enhancing type safety and code clarity.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: converting subcircuit.js to TypeScript with explicit type annotations throughout the codebase.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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 and usage tips.

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: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
v1/src/simulator/src/subcircuit.ts (1)

200-212: Potential NPE: Inconsistent optional chaining usage.

Line 202 uses optional chaining (?.) suggesting this.localScope.Input[i] could be undefined, but line 205 accesses this.localScope.Input[i].output1 without the guard. If the optional chaining on line 202 prevents an error, line 205 will still throw.

Suggested fix
     makeConnections(): void {
         for (let i = 0; i < this.inputNodes.length; i++) {
             this.localScope.Input[i]?.output1.connectWireLess(
                 this.inputNodes[i]
             )
-            this.localScope.Input[i].output1.subcircuitOverride = true
+            if (this.localScope.Input[i]) {
+                this.localScope.Input[i].output1.subcircuitOverride = true
+            }
         }
src/simulator/src/subcircuit.ts (1)

200-212: Potential NPE: Inconsistent optional chaining usage.

Same issue as in v1: Line 202 uses ?. but line 205 does not. If this.localScope.Input[i] can be undefined, line 205 will throw.

Suggested fix
     makeConnections(): void {
         for (let i = 0; i < this.inputNodes.length; i++) {
             this.localScope.Input[i]?.output1.connectWireLess(
                 this.inputNodes[i]
             )
-            this.localScope.Input[i].output1.subcircuitOverride = true
+            if (this.localScope.Input[i]) {
+                this.localScope.Input[i].output1.subcircuitOverride = true
+            }
         }
🧹 Nitpick comments (6)
v1/src/simulator/src/subcircuit.ts (3)

479-498: Unused variables: rX, lX, uY, dY are declared but never used.

These variables are assigned from layoutProperties but are not referenced anywhere in the method body.

Remove unused variables
     getElementHover(): any {
-        const rX = this.layoutProperties.rightDimensionX
-        const lX = this.layoutProperties.leftDimensionX
-        const uY = this.layoutProperties.upDimensionY
-        const dY = this.layoutProperties.downDimensionY
-
         for (const el of circuitElementList) {

583-588: Consider a more precise tuple type for determine_label.

The first element is always 'left', 'right', or 'center'. A literal union type would provide better type safety when used with ctx.textAlign.

Suggested type refinement
-    determine_label(x: number, y: number): [string, number, number] {
+    determine_label(x: number, y: number): ['left' | 'right' | 'center', number, number] {

272-276: Deprecation note acknowledged.

The reBuild method is marked for deprecation. Consider adding a @deprecated JSDoc tag to trigger IDE warnings when this method is called.

Add `@deprecated` tag
     /**
-     * Needs to be deprecated, removed
+     * `@deprecated` Needs to be deprecated, removed
      */
     reBuild(): void {
     }
src/simulator/src/subcircuit.ts (3)

479-498: Unused variables: rX, lX, uY, dY are never used.

These variables are declared but not referenced in the method body.

Remove unused variables
     getElementHover(): any {
-        const rX = this.layoutProperties.rightDimensionX
-        const lX = this.layoutProperties.leftDimensionX
-        const uY = this.layoutProperties.upDimensionY
-        const dY = this.layoutProperties.downDimensionY
-
         for (const el of circuitElementList) {

583-588: Consider using literal union type for better type safety.

Suggested type refinement
-    determine_label(x: number, y: number): [string, number, number] {
+    determine_label(x: number, y: number): ['left' | 'right' | 'center', number, number] {

272-276: Consider adding @deprecated JSDoc tag.

Add deprecation annotation
     /**
-     * Needs to be deprecated, removed
+     * `@deprecated` Needs to be deprecated, removed
      */
     reBuild(): void {
     }

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.

1 participant