@@ -416,19 +416,10 @@ export class Session {
416
416
[ ] . concat ( compiledGraph . declarations . map ( declaration => declaration . target ) )
417
417
) ;
418
418
419
- const standardActions = [ ] . concat (
420
- compiledGraph . tables ,
421
- compiledGraph . assertions ,
422
- compiledGraph . operations ,
423
- compiledGraph . declarations
424
- ) ;
425
-
426
- this . checkActionNameUniqueness ( standardActions ) ;
419
+ this . removeNonUniqueActionsFromCompiledGraph ( compiledGraph ) ;
427
420
428
421
this . checkTestNameUniqueness ( compiledGraph . tests ) ;
429
422
430
- this . checkCanonicalTargetUniqueness ( standardActions ) ;
431
-
432
423
this . checkTableConfigValidity ( compiledGraph . tables ) ;
433
424
434
425
this . checkCircularity (
@@ -574,41 +565,6 @@ export class Session {
574
565
} ) ;
575
566
}
576
567
577
- private checkActionNameUniqueness ( actions : IActionProto [ ] ) {
578
- const allNames : string [ ] = [ ] ;
579
- actions . forEach ( action => {
580
- const name = targetAsReadableString ( action . target ) ;
581
- if ( allNames . includes ( name ) ) {
582
- this . compileError (
583
- new Error (
584
- `Duplicate action name detected. Names within a schema must be unique across tables, declarations, assertions, and operations`
585
- ) ,
586
- action . fileName ,
587
- action . target
588
- ) ;
589
- }
590
- allNames . push ( name ) ;
591
- } ) ;
592
- }
593
-
594
- private checkCanonicalTargetUniqueness ( actions : IActionProto [ ] ) {
595
- const allCanonicalTargets = new StringifiedSet < dataform . ITarget > ( targetStringifier ) ;
596
- actions . forEach ( action => {
597
- if ( allCanonicalTargets . has ( action . canonicalTarget ) ) {
598
- this . compileError (
599
- new Error (
600
- `Duplicate canonical target detected. Canonical targets must be unique across tables, declarations, assertions, and operations:\n"${ JSON . stringify (
601
- action . canonicalTarget
602
- ) } "`
603
- ) ,
604
- action . fileName ,
605
- action . target
606
- ) ;
607
- }
608
- allCanonicalTargets . add ( action . canonicalTarget ) ;
609
- } ) ;
610
- }
611
-
612
568
private checkTableConfigValidity ( tables : dataform . ITable [ ] ) {
613
569
tables . forEach ( table => {
614
570
// type
@@ -866,6 +822,65 @@ export class Session {
866
822
) ;
867
823
} ) ;
868
824
}
825
+
826
+ private removeNonUniqueActionsFromCompiledGraph ( compiledGraph : dataform . CompiledGraph ) {
827
+ function getNonUniqueTargets ( targets : dataform . ITarget [ ] ) : StringifiedSet < dataform . ITarget > {
828
+ const allTargets = new StringifiedSet < dataform . ITarget > ( targetStringifier ) ;
829
+ const nonUniqueTargets = new StringifiedSet < dataform . ITarget > ( targetStringifier ) ;
830
+
831
+ targets . forEach ( target => {
832
+ if ( allTargets . has ( target ) ) {
833
+ nonUniqueTargets . add ( target ) ;
834
+ }
835
+ allTargets . add ( target ) ;
836
+ } ) ;
837
+
838
+ return nonUniqueTargets ;
839
+ }
840
+
841
+ const actions = [ ] . concat (
842
+ compiledGraph . tables ,
843
+ compiledGraph . assertions ,
844
+ compiledGraph . operations ,
845
+ compiledGraph . declarations
846
+ ) ;
847
+
848
+ const nonUniqueActionsTargets = getNonUniqueTargets ( actions . map ( action => action . target ) ) ;
849
+ const nonUniqueActionsCanonicalTargets = getNonUniqueTargets ( actions . map ( action => action . canonicalTarget ) ) ;
850
+
851
+ const isUniqueAction = ( action : IActionProto ) => {
852
+ const isNonUniqueTarget = nonUniqueActionsTargets . has ( action . target ) ;
853
+ const isNonUniqueCanonicalTarget = nonUniqueActionsCanonicalTargets . has ( action . canonicalTarget ) ;
854
+
855
+ if ( isNonUniqueTarget ) {
856
+ this . compileError (
857
+ new Error (
858
+ `Duplicate action name detected. Names within a schema must be unique across tables, declarations, assertions, and operations`
859
+ ) ,
860
+ action . fileName ,
861
+ action . target
862
+ ) ;
863
+ }
864
+ if ( isNonUniqueCanonicalTarget ) {
865
+ this . compileError (
866
+ new Error (
867
+ `Duplicate canonical target detected. Canonical targets must be unique across tables, declarations, assertions, and operations:\n"${ JSON . stringify (
868
+ action . canonicalTarget
869
+ ) } "`
870
+ ) ,
871
+ action . fileName ,
872
+ action . target
873
+ ) ;
874
+ }
875
+
876
+ return ! isNonUniqueTarget && ! isNonUniqueCanonicalTarget ;
877
+ }
878
+
879
+ compiledGraph . tables = compiledGraph . tables . filter ( isUniqueAction ) ;
880
+ compiledGraph . operations = compiledGraph . operations . filter ( isUniqueAction ) ;
881
+ compiledGraph . declarations = compiledGraph . declarations . filter ( isUniqueAction ) ;
882
+ compiledGraph . assertions = compiledGraph . assertions . filter ( isUniqueAction ) ;
883
+ }
869
884
}
870
885
871
886
function declaresDataset ( type : string , hasOutput ?: boolean ) {
0 commit comments