@@ -18,6 +18,7 @@ import {
18
18
addRouteDeclarationToModule ,
19
19
addSymbolToNgModuleMetadata ,
20
20
findNodes ,
21
+ hasTopLevelIdentifier ,
21
22
insertAfterLastOccurrence ,
22
23
insertImport ,
23
24
} from './ast-utils' ;
@@ -776,4 +777,74 @@ describe('ast utils', () => {
776
777
expect ( result ) . toBe ( fileContent ) ;
777
778
} ) ;
778
779
} ) ;
780
+
781
+ describe ( 'hasTopLevelIdentifier' , ( ) => {
782
+ const filePath = './src/foo.ts' ;
783
+
784
+ it ( 'should find top-level class declaration with a specific name' , ( ) => {
785
+ const fileContent = `class FooClass {}` ;
786
+ const source = getTsSource ( filePath , fileContent ) ;
787
+
788
+ expect ( hasTopLevelIdentifier ( source , 'FooClass' ) ) . toBe ( true ) ;
789
+ expect ( hasTopLevelIdentifier ( source , 'Foo' ) ) . toBe ( false ) ;
790
+ } ) ;
791
+
792
+ it ( 'should find top-level interface declaration with a specific name' , ( ) => {
793
+ const fileContent = `interface FooInterface {}` ;
794
+ const source = getTsSource ( filePath , fileContent ) ;
795
+
796
+ expect ( hasTopLevelIdentifier ( source , 'FooInterface' ) ) . toBe ( true ) ;
797
+ expect ( hasTopLevelIdentifier ( source , 'Foo' ) ) . toBe ( false ) ;
798
+ } ) ;
799
+
800
+ it ( 'should find top-level variable declaration with a specific name' , ( ) => {
801
+ const fileContent = `
802
+ const singleVar = 1;
803
+
804
+ const fooVar = 1, barVar = 2;
805
+ ` ;
806
+ const source = getTsSource ( filePath , fileContent ) ;
807
+
808
+ expect ( hasTopLevelIdentifier ( source , 'singleVar' ) ) . toBe ( true ) ;
809
+ expect ( hasTopLevelIdentifier ( source , 'fooVar' ) ) . toBe ( true ) ;
810
+ expect ( hasTopLevelIdentifier ( source , 'barVar' ) ) . toBe ( true ) ;
811
+ expect ( hasTopLevelIdentifier ( source , 'bar' ) ) . toBe ( false ) ;
812
+ } ) ;
813
+
814
+ it ( 'should find top-level imports with a specific name' , ( ) => {
815
+ const fileContent = `
816
+ import { FooInterface } from '@foo/interfaces';
817
+
818
+ class FooClass implements FooInterface {}
819
+ ` ;
820
+ const source = getTsSource ( filePath , fileContent ) ;
821
+
822
+ expect ( hasTopLevelIdentifier ( source , 'FooInterface' ) ) . toBe ( true ) ;
823
+ expect ( hasTopLevelIdentifier ( source , 'Foo' ) ) . toBe ( false ) ;
824
+ } ) ;
825
+
826
+ it ( 'should find top-level aliased imports with a specific name' , ( ) => {
827
+ const fileContent = `
828
+ import { FooInterface as AliasedFooInterface } from '@foo/interfaces';
829
+
830
+ class FooClass implements AliasedFooInterface {}
831
+ ` ;
832
+ const source = getTsSource ( filePath , fileContent ) ;
833
+
834
+ expect ( hasTopLevelIdentifier ( source , 'AliasedFooInterface' ) ) . toBe ( true ) ;
835
+ expect ( hasTopLevelIdentifier ( source , 'FooInterface' ) ) . toBe ( false ) ;
836
+ expect ( hasTopLevelIdentifier ( source , 'Foo' ) ) . toBe ( false ) ;
837
+ } ) ;
838
+
839
+ it ( 'should be able to skip imports from a certain module' , ( ) => {
840
+ const fileContent = `
841
+ import { FooInterface } from '@foo/interfaces';
842
+
843
+ class FooClass implements FooInterface {}
844
+ ` ;
845
+ const source = getTsSource ( filePath , fileContent ) ;
846
+
847
+ expect ( hasTopLevelIdentifier ( source , 'FooInterface' , '@foo/interfaces' ) ) . toBe ( false ) ;
848
+ } ) ;
849
+ } ) ;
779
850
} ) ;
0 commit comments