-
Notifications
You must be signed in to change notification settings - Fork 150
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
base: main
Are you sure you want to change the base?
Changes from all commits
d7205df
620b100
7291b0a
0f1b4a8
7b0c9e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
-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
Suggested change
🧰 Tools🪛 Biome (1.9.4)[error] 66-66: void is confusing inside a union type. Unsafe fix: Use undefined instead. (lint/suspicious/noConfusingVoidType) |
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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.📝 Committable suggestion