Skip to content

refactor: TS integration in modules.js #472

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 5 commits 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.

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

/**
* Helper function to mark occupied Y positions in the `done` object.
*/
function markOccupiedYPositions(elements: InputOutputElement[], x: number, done: { [key: number]: number }): void {
for (const element of elements) {
if (element.layoutProperties.x === x) {
done[element.layoutProperties.y] = 1;
}
}
}

/**
* Helper function to find the next available Y position.
*/
function findNextAvailableY(done: { [key: number]: number }, startY: number): number {
let possibleY = startY;
while (done[possibleY] || done[possibleY + 10] || done[possibleY - 10]) {
possibleY += 10;
}
return possibleY;
}
Comment on lines +18 to +24
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

Add safeguard against potential infinite loop

The findNextAvailableY function lacks a safeguard against infinite loops. If all positions are marked as occupied, this function would never terminate.

 function findNextAvailableY(done: { [key: number]: number }, startY: number): number {
     let possibleY = startY;
+    const maxIterations = 1000; // Prevent infinite loops
+    let iterations = 0;
     while (done[possibleY] || done[possibleY + 10] || done[possibleY - 10]) {
         possibleY += 10;
+        iterations++;
+        if (iterations > maxIterations) {
+            console.warn('Maximum iterations reached in findNextAvailableY');
+            break;
+        }
     }
     return possibleY;
 }
📝 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
function findNextAvailableY(done: { [key: number]: number }, startY: number): number {
let possibleY = startY;
while (done[possibleY] || done[possibleY + 10] || done[possibleY - 10]) {
possibleY += 10;
}
return possibleY;
}
function findNextAvailableY(done: { [key: number]: number }, startY: number): number {
let possibleY = startY;
const maxIterations = 1000; // Prevent infinite loops
let iterations = 0;
while (done[possibleY] || done[possibleY + 10] || done[possibleY - 10]) {
possibleY += 10;
iterations++;
if (iterations > maxIterations) {
console.warn('Maximum iterations reached in findNextAvailableY');
break;
}
}
return possibleY;
}


/**
* Helper function to update element positions when the layout height changes.
*/
function updateElementPositions(elements: InputOutputElement[], oldHeight: number, newHeight: number): void {
for (const element of elements) {
if (element.layoutProperties.y === oldHeight) {
element.layoutProperties.y = newHeight;
}
}
}

export function getNextPosition(x: number = 0, scope: GlobalScope): number {
const done: { [key: number]: number } = {};

// Mark occupied Y positions for Input and Output elements
markOccupiedYPositions(scope.Input, x, done);
markOccupiedYPositions(scope.Output, x, done);

// Find the next available Y position
const possibleY = findNextAvailableY(done, 20);

// Calculate the new height and update the layout if necessary
const height = possibleY + 20;
if (height > scope.layout.height) {
const oldHeight = scope.layout.height;
scope.layout.height = height;

// Update positions of Input and Output elements
updateElementPositions(scope.Input, oldHeight, scope.layout.height);
updateElementPositions(scope.Output, oldHeight, scope.layout.height);
}

return possibleY;
}

/**
* Global
*/
export const modules: Modules = {};

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

size = parseInt(size.toString(), 10);

const obj: ModuleInstance = new modules[this.objectType](
this.x,
this.y,
this.scope,
this.direction,
size,
this.bitWidth
);

this.delete();
simulationArea.lastSelected = obj;
return obj;
}
Comment on lines +66 to +84
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

Fix return type and improve parameter handling

The function has a few issues to address:

  1. Using void in a union type is confusing (per static analysis)
  2. Modifying the input parameter directly
  3. Relying on this context which can be error-prone
-export function changeInputSize(this: ChangeInputSizeContext, size: number): ModuleInstance | void {
+export function changeInputSize(this: ChangeInputSizeContext, size: number): ModuleInstance | undefined {
     if (size === undefined || size < 2 || size > 10) return;
     if (this.inputSize === size) return;
 
-    size = parseInt(size.toString(), 10);
+    const parsedSize = parseInt(size.toString(), 10);
 
     const obj: ModuleInstance = new modules[this.objectType](
         this.x,
         this.y,
         this.scope,
         this.direction,
-        size,
+        parsedSize,
         this.bitWidth
     );
 
     this.delete();
     simulationArea.lastSelected = obj;
     return obj;
 }
📝 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
export function changeInputSize(this: ChangeInputSizeContext, size: number): ModuleInstance | void {
if (size === undefined || size < 2 || size > 10) return;
if (this.inputSize === size) return;
size = parseInt(size.toString(), 10);
const obj: ModuleInstance = new modules[this.objectType](
this.x,
this.y,
this.scope,
this.direction,
size,
this.bitWidth
);
this.delete();
simulationArea.lastSelected = obj;
return obj;
}
export function changeInputSize(this: ChangeInputSizeContext, size: number): ModuleInstance | undefined {
if (size === undefined || size < 2 || size > 10) return;
if (this.inputSize === size) return;
const parsedSize = parseInt(size.toString(), 10);
const obj: ModuleInstance = new modules[this.objectType](
this.x,
this.y,
this.scope,
this.direction,
parsedSize,
this.bitWidth
);
this.delete();
simulationArea.lastSelected = obj;
return obj;
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 66-66: void is confusing inside a union type.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

50 changes: 50 additions & 0 deletions src/simulator/src/types/modules.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
interface LayoutProperties {
x: number;
y: number;
}

interface InputOutputElement {
layoutProperties: LayoutProperties;
}

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

interface GlobalScope extends Scope {
// Add any additional properties if needed
}

interface Module {
new (
x: number,
y: number,
scope: GlobalScope,
direction: string,
size: number,
bitWidth: number
): ModuleInstance;
}

interface ModuleInstance {
delete(): void;
}

interface Modules {
[key: string]: Module;
}

interface ChangeInputSizeContext {
inputSize: number;
objectType: string;
x: number;
y: number;
scope: GlobalScope;
direction: string;
bitWidth: number;
delete(): void;
}
Loading