@@ -154,3 +154,72 @@ export function isIncluding(
154154 } ;
155155 return dfs ( componentId ) ;
156156}
157+
158+ function validateComponent ( store : CCStore , componentId : CCComponentId ) {
159+ const component = store . components . get ( componentId ) ! ;
160+ if ( component . isIntrinsic ) return ;
161+
162+ const nodes = store . nodes . getManyByParentComponentId ( componentId ) ;
163+ const connections = store . connections . getManyByParentComponentId ( componentId ) ;
164+ const parentComponentPins =
165+ store . componentPins . getManyByComponentId ( componentId ) ;
166+
167+ // check nodePins and componentPins of each node
168+ for ( const node of nodes ) {
169+ const nodePins = new Set ( store . nodePins . getManyByNodeId ( node . id ) ) ;
170+ const componentPinIds = new Set (
171+ store . componentPins
172+ . getManyByComponentId ( node . componentId )
173+ . map ( ( pin ) => pin . id )
174+ ) ;
175+ invariant ( componentPinIds . size === nodePins . size ) ;
176+ for ( const nodePin of nodePins ) {
177+ invariant ( componentPinIds . has ( nodePin . componentPinId ) ) ;
178+
179+ // check whether each nodePin without connections corresponds to a parentComponentPin
180+ // and nodePin with connections corresponds to no parentComponentPin
181+ const connectionsAssociatedWithNodePin =
182+ store . connections . getConnectionsByNodePinId ( nodePin . id ) ;
183+ const parentComponentPin = parentComponentPins . find (
184+ ( pin ) => pin . implementation === nodePin . id
185+ ) ;
186+ if ( connectionsAssociatedWithNodePin . length === 0 ) {
187+ invariant ( parentComponentPin ) ;
188+ } else {
189+ invariant ( ! parentComponentPin ) ;
190+ }
191+ }
192+ }
193+
194+ // check whether each connection has valid nodePins and componentPins
195+ for ( const connection of connections ) {
196+ const fromNodePin = store . nodePins . get ( connection . from ) ;
197+ const toNodePin = store . nodePins . get ( connection . to ) ;
198+ invariant ( fromNodePin && toNodePin ) ;
199+ invariant ( fromNodePin . nodeId !== toNodePin . nodeId ) ;
200+ const fromComponentPin = store . componentPins . get (
201+ fromNodePin . componentPinId
202+ ) ;
203+ const toComponentPin = store . componentPins . get ( toNodePin . componentPinId ) ;
204+ invariant ( fromComponentPin && toComponentPin ) ;
205+ invariant ( fromComponentPin . type !== toComponentPin . type ) ;
206+ }
207+
208+ // check whether implementation of each componentPin has no connections
209+ for ( const componentPin of parentComponentPins ) {
210+ invariant ( componentPin . implementation ) ;
211+ const implementationNodePin = store . nodePins . get (
212+ componentPin . implementation
213+ ) ;
214+ invariant ( implementationNodePin ) ;
215+ const connectionsAssociatedWithNodePin =
216+ store . connections . getConnectionsByNodePinId ( implementationNodePin . id ) ;
217+ invariant ( connectionsAssociatedWithNodePin . length === 0 ) ;
218+ }
219+ }
220+
221+ export function validateAllComponents ( store : CCStore ) {
222+ for ( const component of store . components . toArray ( ) ) {
223+ validateComponent ( store , component . id ) ;
224+ }
225+ }
0 commit comments