11import type { Output , OutputComponent } from '@/registry/schema' ;
22
33export function validateOutput ( registry : Output ) {
4- function validateComponent (
5- comp : OutputComponent ,
6- ctx : {
7- stack ?: Map < string , Set < string > > ;
8- } = { } ,
9- ) {
10- const { stack = new Map < string , Set < string > > ( ) } = ctx ;
4+ const validatedComps = new Set < string > ( ) ;
5+ const fileToComps = new Map < string , Set < string > > ( ) ;
6+
7+ function validateComponent ( comp : OutputComponent ) {
8+ if ( validatedComps . has ( comp . name ) ) return ;
9+ validatedComps . add ( comp . name ) ;
1110
1211 for ( const file of comp . files ) {
13- const parents = stack . get ( file . path ) ;
12+ const parents = fileToComps . get ( file . path ) ;
1413
1514 if ( parents ) {
1615 parents . add ( comp . name ) ;
1716 } else {
18- stack . set ( file . path , new Set ( [ comp . name ] ) ) ;
17+ fileToComps . set ( file . path , new Set ( [ comp . name ] ) ) ;
1918 }
2019 }
2120
@@ -26,28 +25,22 @@ export function validateOutput(registry: Output) {
2625 continue ;
2726 }
2827
29- validateComponent ( subComp , {
30- stack,
31- } ) ;
28+ validateComponent ( subComp ) ;
3229 }
3330
3431 for ( const file of comp . files ) {
35- const parents = stack . get ( file . path ) ;
36- if ( ! parents ) continue ;
37- if ( parents . size <= 1 ) continue ;
32+ const parents = fileToComps . get ( file . path ) ;
33+ if ( ! parents || parents . size <= 1 ) continue ;
3834
3935 throw new Error (
4036 `Duplicated file in same component ${ Array . from ( parents ) . join ( ', ' ) } : ${ file . path } ` ,
4137 ) ;
4238 }
4339 }
4440
45- const compSet = new Set < string > ( ) ;
4641 for ( const comp of registry . components ) {
47- if ( compSet . has ( comp . name ) )
48- throw new Error ( `duplicated component name ${ comp . name } ` ) ;
49- compSet . add ( comp . name ) ;
50-
42+ // per comp
43+ fileToComps . clear ( ) ;
5144 validateComponent ( comp ) ;
5245 }
5346}
0 commit comments