-
Notifications
You must be signed in to change notification settings - Fork 184
Convert backupCircuit.js to TypeScript #672
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
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,122 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { projectSavedSet } from './project'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { moduleList, updateOrder } from '../metadata'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* eslint-disable no-param-reassign */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Type Declarations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface NodeLike { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| saveObject: () => Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface SubCircuitLike { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| removeConnections: () => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| makeConnections: () => void; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| interface Scope { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| SubCircuit: SubCircuitLike[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allNodes: NodeLike[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| nodes: NodeLike[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| layout: Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verilogMetadata: Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testbenchData: Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| restrictedCircuitElementsUsed: Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| backups: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| history: string[]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| timeStamp: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| [key: string]: any; // For dynamic moduleList entries | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Global declaration (CircuitVerse uses globalScope) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| declare const globalScope: Scope; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Helper Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function extract(obj: NodeLike): Record<string, any> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return obj.saveObject(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Check if backup is available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param scope Circuit scope | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns true if any data exists for backup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @category data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function checkIfBackup(scope: Scope): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = 0; i < updateOrder.length; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const key = updateOrder[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (scope[key]?.length) return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Create a serialized backup of the current circuit state. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param scope Current working scope (defaults to globalScope) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns JSON-compatible backup data object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function backUp(scope: Scope = globalScope): Record<string, any> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Disconnect subcircuits before saving | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const sub of scope.SubCircuit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sub.removeConnections(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+67
to
+69
Contributor
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. Add guard for optional SubCircuit property. Since Apply this diff to add a safe guard: // Disconnect subcircuits before saving
- for (const sub of scope.SubCircuit) {
- sub.removeConnections();
+ if (scope.SubCircuit) {
+ for (const sub of scope.SubCircuit) {
+ sub.removeConnections();
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const data: Record<string, any> = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| layout: scope.layout, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| verilogMetadata: scope.verilogMetadata, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| allNodes: scope.allNodes.map(extract), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| testbenchData: scope.testbenchData, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| id: scope.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: scope.name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Save all module objects dynamically | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const moduleName of moduleList) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (scope[moduleName]?.length) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data[moduleName] = scope[moduleName].map(extract); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+71
to
+85
Contributor
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. Add guards for optional properties. The code accesses optional properties without checking if they exist:
Apply this diff to add safe guards: const data: Record<string, any> = {
layout: scope.layout,
verilogMetadata: scope.verilogMetadata,
- allNodes: scope.allNodes.map(extract),
- testbenchData: scope.testbenchData,
+ allNodes: scope.allNodes?.map(extract) || [],
+ testbenchData: scope.testbenchData || {},
id: scope.id,
name: scope.name,
};📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.restrictedCircuitElementsUsed = scope.restrictedCircuitElementsUsed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Save intermediate nodes (wire nodes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| data.nodes = scope.nodes.map(node => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scope.allNodes.indexOf(node) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+90
to
+92
Contributor
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. Add guards for optional nodes property. The Apply this diff: // Save intermediate nodes (wire nodes)
- data.nodes = scope.nodes.map(node =>
- scope.allNodes.indexOf(node)
- );
+ data.nodes = scope.nodes?.map(node =>
+ scope.allNodes?.indexOf(node) ?? -1
+ ) || [];📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Reconnect subcircuits after saving | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (const sub of scope.SubCircuit) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sub.makeConnections(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+95
to
+97
Contributor
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. Add guard for optional SubCircuit property. Same issue as the disconnection code—need to check if Apply this diff: // Reconnect subcircuits after saving
- for (const sub of scope.SubCircuit) {
- sub.makeConnections();
+ if (scope.SubCircuit) {
+ for (const sub of scope.SubCircuit) {
+ sub.makeConnections();
+ }
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Schedule a new backup in the current scope. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Ensures duplicates aren’t stored consecutively. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param scope Current scope (default = globalScope) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns The serialized backup string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function scheduleBackup(scope: Scope = globalScope): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const backup = JSON.stringify(backUp(scope)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scope.backups.length === 0 || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scope.backups[scope.backups.length - 1] !== backup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scope.backups.push(backup); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scope.history = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scope.timeStamp = Date.now(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| projectSavedSet(false); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return backup; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🧩 Analysis chain
Fix type mismatches with the actual Scope class.
The Scope interface has several type inconsistencies with the actual Scope class defined in
circuit.ts:idshould benumber | string(not justnumber)restrictedCircuitElementsUsedshould beany[](notRecord<string, any>)SubCircuit,allNodes, andnodesshould be optional properties marked with?, as they are in the actual Scope classtestbenchDatais not defined in the Scope class fromcircuit.tsApply this diff to align the interface with the actual Scope class:
interface Scope { - SubCircuit: SubCircuitLike[]; - allNodes: NodeLike[]; - nodes: NodeLike[]; + SubCircuit?: SubCircuitLike[]; + allNodes?: NodeLike[]; + nodes?: NodeLike[]; layout: Record<string, any>; verilogMetadata: Record<string, any>; - testbenchData: Record<string, any>; - id: number; + testbenchData?: Record<string, any>; + id: number | string; name: string; - restrictedCircuitElementsUsed: Record<string, any>; + restrictedCircuitElementsUsed: any[]; backups: string[]; history: string[]; timeStamp: number; [key: string]: any; }Please verify whether
testbenchDataexists on the Scope class or is dynamically added. Run this script to check:🏁 Script executed:
Length of output: 5108
🏁 Script executed:
Length of output: 2101
🏁 Script executed:
Length of output: 3424
Fix critical type mismatches and incompatibility with the actual Scope class.
The Scope interface has several serious type inconsistencies with the actual Scope class in
circuit.ts:SubCircuitproperty doesn't exist in the Scope class—it hasCircuitElementinstead. ThebackUp()function (lines 67, 95) will fail at runtime trying to accessscope.SubCircuit.idshould benumber | string(not justnumber)restrictedCircuitElementsUsedshould beany[](notRecord<string, any>)allNodesandnodesshould be optional (marked with?)The
testbenchDataproperty (line 25) is intentionally included and actively used in the backup logic (line 73, 85), so it should remain.Apply this diff:
interface Scope { - SubCircuit: SubCircuitLike[]; + CircuitElement?: SubCircuitLike[]; allNodes?: NodeLike[]; nodes?: NodeLike[]; layout: Record<string, any>; verilogMetadata: Record<string, any>; testbenchData: Record<string, any>; - id: number; + id: number | string; name: string; - restrictedCircuitElementsUsed: Record<string, any>; + restrictedCircuitElementsUsed: any[]; backups: string[]; history: string[]; timeStamp: number; [key: string]: any; }Then update the
backUp()function loops (lines 67, 95) to iterate overscope.CircuitElementinstead ofscope.SubCircuit.🤖 Prompt for AI Agents