Skip to content

refactor: migrate modules.js to TypeScript as modules.ts #602

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions src/simulator/src/modules.js

This file was deleted.

102 changes: 102 additions & 0 deletions src/simulator/src/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/* eslint-disable import/no-cycle */
import { simulationArea } from './simulationArea'

// Define types for Input/Output layout
interface LayoutProperties {
x: number
y: number
}

interface IOComponent {
layoutProperties: LayoutProperties
}

interface Scope {
Input: IOComponent[]
Output: IOComponent[]
layout: {
height: number
}
}

// Assume globalScope exists in your global context
declare const globalScope: Scope

export function getNextPosition(x = 0, scope: Scope = globalScope): number {
let possibleY = 20
const done: Record<number, 1> = {}

for (let i = 0; i < scope.Input.length - 1; i++) {
if (scope.Input[i].layoutProperties.x === x) {
done[scope.Input[i].layoutProperties.y] = 1
}
}
Comment on lines +29 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Off-by-one skips the last input element

for (let i = 0; i < scope.Input.length - 1; i++) never visits scope.Input[scope.Input.length - 1], so the y-coordinate of the last input placed at this x can collide with the new object.

- for (let i = 0; i < scope.Input.length - 1; i++) {
+ for (let i = 0; i < scope.Input.length; i++) {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for (let i = 0; i < scope.Input.length - 1; i++) {
if (scope.Input[i].layoutProperties.x === x) {
done[scope.Input[i].layoutProperties.y] = 1
}
}
for (let i = 0; i < scope.Input.length; i++) {
if (scope.Input[i].layoutProperties.x === x) {
done[scope.Input[i].layoutProperties.y] = 1
}
}
🤖 Prompt for AI Agents
In src/simulator/src/modules.ts around lines 29 to 33, the for loop uses `i <
scope.Input.length - 1`, which skips the last element of scope.Input. Change the
loop condition to `i < scope.Input.length` to ensure the last input element is
included and its y-coordinate is checked for collisions.


for (let i = 0; i < scope.Output.length; i++) {
if (scope.Output[i].layoutProperties.x === x) {
done[scope.Output[i].layoutProperties.y] = 1
}
}

while (done[possibleY] || done[possibleY + 10] || done[possibleY - 10]) {
possibleY += 10
}

const height = possibleY + 20
if (height > scope.layout.height) {
const oldHeight = scope.layout.height
scope.layout.height = height

for (let i = 0; i < scope.Input.length; i++) {
if (scope.Input[i].layoutProperties.y === oldHeight) {
scope.Input[i].layoutProperties.y = scope.layout.height
}
}

for (let i = 0; i < scope.Output.length; i++) {
if (scope.Output[i].layoutProperties.y === oldHeight) {
scope.Output[i].layoutProperties.y = scope.layout.height
}
}
}
Comment on lines +45 to +61
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Height-adjustment updates miss elements above the old height

Only elements exactly at oldHeight are shifted, but any inputs/outputs that already exceeded oldHeight keep their positions and may now spill outside the new canvas height.

🤖 Prompt for AI Agents
In src/simulator/src/modules.ts around lines 45 to 61, the code only updates
elements whose y position equals oldHeight, missing those positioned above
oldHeight that may now be outside the new height. Modify the condition in both
loops to update elements with y positions greater than or equal to oldHeight,
ensuring all relevant elements are shifted to stay within the updated layout
height.


return possibleY
}

/**
* Global modules registry
*/
const modules: Record<string, any> = {}
export default modules

// Define `this` context type
interface ChangeInputSizeContext {
inputSize: number
objectType: string
x: number
y: number
scope: any
direction: number
bitWidth: number
delete: () => void
}

export function changeInputSize(this: ChangeInputSizeContext, size?: number): any {
if (size === undefined || size < 2 || size > 10) return
if (this.inputSize === size) return

size = parseInt(String(size), 10)

const obj = new modules[this.objectType](
this.x,
this.y,
this.scope,
this.direction,
size,
this.bitWidth
)
Comment on lines +90 to +97
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Runtime crash if module type not registered

new modules[this.objectType]( … ) assumes the constructor is present. Add a guard to throw a clear error (or no-op) instead of an unhelpful “undefined is not a constructor”.

🤖 Prompt for AI Agents
In src/simulator/src/modules.ts around lines 90 to 97, the code assumes that
modules[this.objectType] is always defined, which can cause a runtime crash if
the module type is not registered. Add a guard before the constructor call to
check if modules[this.objectType] exists; if not, throw a clear error indicating
the module type is unregistered or handle it gracefully with a no-op to prevent
the "undefined is not a constructor" error.


this.delete()
simulationArea.lastSelected = obj
return obj
}