@@ -679,6 +679,100 @@ describe('find references', () => {
679
679
assertTextSpans ( refs , [ '<div dir>' , 'Dir' ] ) ;
680
680
assertFileNames ( refs , [ 'app.ts' , 'dir.ts' ] ) ;
681
681
} ) ;
682
+
683
+ it ( 'gets references to all matching directives when cursor is on an attribute' , ( ) => {
684
+ const dirFile = `
685
+ import {Directive} from '@angular/core';
686
+
687
+ @Directive({selector: '[dir]'})
688
+ export class Dir {}` ;
689
+ const dirFile2 = `
690
+ import {Directive} from '@angular/core';
691
+
692
+ @Directive({selector: '[dir]'})
693
+ export class Dir2 {}` ;
694
+ const { text, cursor} = extractCursorInfo ( `
695
+ import {Component, NgModule} from '@angular/core';
696
+ import {Dir} from './dir';
697
+ import {Dir2} from './dir2';
698
+
699
+ @Component({template: '<div di¦r></div>'})
700
+ export class AppCmp {
701
+ }
702
+
703
+ @NgModule({declarations: [AppCmp, Dir, Dir2]})
704
+ export class AppModule {}
705
+ ` ) ;
706
+ env = LanguageServiceTestEnvironment . setup ( [
707
+ { name : _ ( '/app.ts' ) , contents : text , isRoot : true } ,
708
+ { name : _ ( '/dir.ts' ) , contents : dirFile } ,
709
+ { name : _ ( '/dir2.ts' ) , contents : dirFile2 } ,
710
+ ] ) ;
711
+ const refs = getReferencesAtPosition ( _ ( '/app.ts' ) , cursor ) ! ;
712
+ expect ( refs . length ) . toBe ( 8 ) ;
713
+ assertTextSpans ( refs , [ '<div dir>' , 'Dir' , 'Dir2' ] ) ;
714
+ assertFileNames ( refs , [ 'app.ts' , 'dir.ts' , 'dir2.ts' ] ) ;
715
+ } ) ;
716
+ } ) ;
717
+
718
+ describe ( 'components' , ( ) => {
719
+ it ( 'works for component classes' , ( ) => {
720
+ const { text, cursor} = extractCursorInfo ( `
721
+ import {Component} from '@angular/core';
722
+
723
+ @Component({selector: 'my-comp', template: ''})
724
+ export class MyCo¦mp {}` ) ;
725
+ const appFile = `
726
+ import {Component, NgModule} from '@angular/core';
727
+ import {MyComp} from './comp';
728
+
729
+ @Component({template: '<my-comp></my-comp>'})
730
+ export class AppCmp {
731
+ }
732
+
733
+ @NgModule({declarations: [AppCmp, MyComp]})
734
+ export class AppModule {}
735
+ ` ;
736
+ env = LanguageServiceTestEnvironment . setup ( [
737
+ { name : _ ( '/app.ts' ) , contents : appFile , isRoot : true } ,
738
+ { name : _ ( '/comp.ts' ) , contents : text } ,
739
+ ] ) ;
740
+ const refs = getReferencesAtPosition ( _ ( '/comp.ts' ) , cursor ) ! ;
741
+ // 4 references are: class declaration, template usage, app import and use in declarations
742
+ // list.
743
+ expect ( refs . length ) . toBe ( 4 ) ;
744
+ assertTextSpans ( refs , [ '<my-comp>' , 'MyComp' ] ) ;
745
+ assertFileNames ( refs , [ 'app.ts' , 'comp.ts' ] ) ;
746
+ } ) ;
747
+
748
+ it ( 'gets works when cursor is on element tag' , ( ) => {
749
+ const compFile = `
750
+ import {Component} from '@angular/core';
751
+
752
+ @Component({selector: 'my-comp', template: ''})
753
+ export class MyComp {}` ;
754
+ const { text, cursor} = extractCursorInfo ( `
755
+ import {Component, NgModule} from '@angular/core';
756
+ import {MyComp} from './comp';
757
+
758
+ @Component({template: '<my-c¦omp></my-comp>'})
759
+ export class AppCmp {
760
+ }
761
+
762
+ @NgModule({declarations: [AppCmp, MyComp]})
763
+ export class AppModule {}
764
+ ` ) ;
765
+ env = LanguageServiceTestEnvironment . setup ( [
766
+ { name : _ ( '/app.ts' ) , contents : text , isRoot : true } ,
767
+ { name : _ ( '/comp.ts' ) , contents : compFile } ,
768
+ ] ) ;
769
+ const refs = getReferencesAtPosition ( _ ( '/app.ts' ) , cursor ) ! ;
770
+ // 4 references are: class declaration, template usage, app import and use in declarations
771
+ // list.
772
+ expect ( refs . length ) . toBe ( 4 ) ;
773
+ assertTextSpans ( refs , [ '<my-comp>' , 'MyComp' ] ) ;
774
+ assertFileNames ( refs , [ 'app.ts' , 'comp.ts' ] ) ;
775
+ } ) ;
682
776
} ) ;
683
777
684
778
function getReferencesAtPosition ( fileName : string , position : number ) {
0 commit comments