/
references_and_rename_spec.ts
1535 lines (1320 loc) · 56.4 KB
/
references_and_rename_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {absoluteFrom, absoluteFrom as _} from '@angular/compiler-cli/src/ngtsc/file_system';
import {initMockFileSystem, TestFile} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {extractCursorInfo, LanguageServiceTestEnvironment} from './env';
import {assertFileNames, assertTextSpans, createModuleWithDeclarations, humanizeDocumentSpanLike} from './test_utils';
describe('find references and rename locations', () => {
let env: LanguageServiceTestEnvironment;
beforeEach(() => {
initMockFileSystem('Native');
});
afterEach(() => {
// Clear env so it's not accidentally carried over to the next test.
env = undefined!;
});
describe('cursor is on binding in component class', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({templateUrl: './app.html'})
export class AppCmp {
myP¦rop!: string;
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
const templateFile = {name: _('/app.html'), contents: '{{myProp}}'};
env = createModuleWithDeclarations([appFile], [templateFile]);
});
it('gets component member references from TS file and external template', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.html', 'app.ts']);
assertTextSpans(refs, ['myProp']);
});
it('gets rename locations from TS file and external template', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.html', 'app.ts']);
assertTextSpans(renameLocations, ['myProp']);
});
});
describe('when cursor is on binding in an external template', () => {
let cursor: number;
beforeEach(() => {
const appFile = {
name: _('/app.ts'),
contents: `
import {Component} from '@angular/core';
@Component({templateUrl: './app.html'})
export class AppCmp {
myProp = '';
}`,
};
const cursorInfo = extractCursorInfo('{{myP¦rop}}');
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile], [templateFile]);
});
it('gets references', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.html', 'app.ts']);
assertTextSpans(refs, ['myProp']);
});
it('gets rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.html', 'app.ts']);
assertTextSpans(renameLocations, ['myProp']);
});
});
describe('when cursor is on function call in external template', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div (click)="set¦Title(2)"></div>'})
export class AppCmp {
setTitle(s: number) {}
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile]);
});
it('gets component member reference in ts file', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts']);
assertTextSpans(refs, ['setTitle']);
});
it('gets rename location in ts file', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts']);
assertTextSpans(renameLocations, ['setTitle']);
});
});
describe('when cursor in on argument to a function call in an external template', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div (click)="setTitle(ti¦tle)"></div>'})
export class AppCmp {
title = '';
setTitle(s: string) {}
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile]);
});
it('gets member reference in ts file', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertTextSpans(refs, ['title']);
});
it('finds rename location in ts file', () => {
const refs = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertTextSpans(refs, ['title']);
});
});
describe('when cursor is on $event in method call arguments', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div (click)="setTitle($even¦t)"></div>'})
export class AppCmp {
setTitle(s: any) {}
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile]);
});
it('find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(1);
assertTextSpans(refs, ['$event']);
});
it('gets no rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations).toBeUndefined();
});
});
describe('when cursor in on LHS of property write in external template', () => {
let cursor: number;
beforeEach(() => {
const appFile = {
name: _('/app.ts'),
contents: `
import {Component} from '@angular/core';
@Component({templateUrl: './app.html' })
export class AppCmp {
title = '';
}`,
};
const templateFileWithCursor = `<div (click)="ti¦tle = 'newtitle'"></div>`;
const cursorInfo = extractCursorInfo(templateFileWithCursor);
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile], [templateFile]);
});
it('gets member reference in ts file', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts', 'app.html']);
assertTextSpans(refs, ['title']);
});
it('gets rename location in ts file', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts', 'app.html']);
assertTextSpans(renameLocations, ['title']);
});
});
describe('when cursor in on RHS of property write in external template', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div (click)="title = otherT¦itle"></div>' })
export class AppCmp {
title = '';
otherTitle = '';
}`);
cursor = cursorInfo.cursor;
const appFile = {
name: _('/app.ts'),
contents: cursorInfo.text,
};
env = createModuleWithDeclarations([appFile]);
});
it('get reference to member in ts file', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts']);
assertTextSpans(refs, ['otherTitle']);
});
it('finds rename location in ts file', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts']);
assertTextSpans(renameLocations, ['otherTitle']);
});
});
describe('when cursor in on a keyed read', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '{{hero["na¦me"]}}' })
export class AppCmp {
hero: {name: string} = {name: 'Superman'};
}`);
cursor = cursorInfo.cursor;
const appFile = {
name: _('/app.ts'),
contents: cursorInfo.text,
};
env = createModuleWithDeclarations([appFile]);
});
it('gets reference to member type definition and initialization in component class', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
// 3 references: the type definition, the value assignment, and the read in the template
expect(refs.length).toBe(3);
assertFileNames(refs, ['app.ts']);
// TODO(atscott): investigate if we can make the template keyed read be just the 'name' part.
// The TypeScript implementation specifically adjusts the span to accommodate string literals:
// https://sourcegraph.com/github.com/microsoft/TypeScript@d5779c75d3dd19565b60b9e2960b8aac36d4d635/-/blob/src/services/findAllReferences.ts#L508-512
// One possible solution would be to extend `FullTemplateMapping` to include the matched TCB
// node and then do the same thing that TS does: if the node is a string, adjust the span.
assertTextSpans(refs, ['name', '"name"']);
});
it('gets rename locations in component class', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations).toBeUndefined();
// TODO(atscott): We should handle this case. The fix requires us to fix the result span as
// described above.
// 3 references: the type definition, the value assignment, and the read in the template
// expect(renameLocations.length).toBe(3);
//
// assertFileNames(renameLocations, ['app.ts']);
// assertTextSpans(renameLocations, ['name']);
});
});
describe('when cursor in on RHS of keyed write in a template', () => {
let cursor: number;
beforeEach(() => {
const appFile = {
name: _('/app.ts'),
contents: `
import {Component} from '@angular/core';
@Component({templateUrl: './app.html' })
export class AppCmp {
hero: {name: string} = {name: 'Superman'};
batman = 'batman';
}`,
};
const templateFileWithCursor = `<div (click)="hero['name'] = bat¦man"></div>`;
const cursorInfo = extractCursorInfo(templateFileWithCursor);
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile], [templateFile]);
});
it('get references in ts file', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts', 'app.html']);
assertTextSpans(refs, ['batman']);
});
it('finds rename location in ts file', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts', 'app.html']);
assertTextSpans(renameLocations, ['batman']);
});
});
describe('when cursor in on an element reference', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<input #myInput /> {{ myIn¦put.value }}'})
export class AppCmp {
title = '';
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile]);
});
it('get reference to declaration in template', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertTextSpans(refs, ['myInput']);
// Get the declaration by finding the reference that appears first in the template
refs.sort((a, b) => a.textSpan.start - b.textSpan.start);
expect(refs[0].isDefinition).toBe(true);
});
it('finds rename location in template', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertTextSpans(renameLocations, ['myInput']);
});
});
describe('when cursor in on a template reference', () => {
let cursor: number;
beforeEach(() => {
const templateWithCursor = `
<ng-template #myTemplate >bla</ng-template>
<ng-container [ngTemplateOutlet]="myTem¦plate"></ng-container>`;
const appFile = {
name: _('/app.ts'),
contents: `
import {Component} from '@angular/core';
@Component({templateUrl: './app.html'})
export class AppCmp {
title = '';
}`,
};
const cursorInfo = extractCursorInfo(templateWithCursor);
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile], [templateFile]);
});
it('gets reference to declaration', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertTextSpans(refs, ['myTemplate']);
assertFileNames(refs, ['app.html']);
// Get the declaration by finding the reference that appears first in the template
refs.sort((a, b) => a.textSpan.start - b.textSpan.start);
expect(refs[0].isDefinition).toBe(true);
});
it('finds rename location in template', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertTextSpans(renameLocations, ['myTemplate']);
assertFileNames(renameLocations, ['app.html']);
});
});
describe('template references', () => {
describe('directives', () => {
let appFile: TestFile;
let dirFile: TestFile;
beforeEach(() => {
const dirFileContents = `
import {Directive} from '@angular/core';
@Directive({selector: '[dir]', exportAs: 'myDir'})
export class Dir {
dirValue!: string;
doSomething() {}
}`;
const appFileContents = `
import {Component} from '@angular/core';
@Component({templateUrl: './app.html'})
export class AppCmp {}`;
appFile = {name: _('/app.ts'), contents: appFileContents};
dirFile = {name: _('/dir.ts'), contents: dirFileContents};
});
describe('when cursor is on usage of template reference', () => {
let cursor: number;
beforeEach(() => {
const templateWithCursor = '<div [dir] #dirRef="myDir"></div> {{ dirR¦ef }}';
const cursorInfo = extractCursorInfo(templateWithCursor);
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, dirFile], [templateFile]);
});
it('should get references', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.html']);
assertTextSpans(refs, ['dirRef']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.html']);
assertTextSpans(renameLocations, ['dirRef']);
});
});
describe('when cursor is on a property read of directive reference', () => {
let cursor: number;
beforeEach(() => {
const fileWithCursor = '<div [dir] #dirRef="myDir"></div> {{ dirRef.dirV¦alue }}';
const cursorInfo = extractCursorInfo(fileWithCursor);
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, dirFile], [templateFile]);
});
it('should get references', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['dir.ts', 'app.html']);
assertTextSpans(refs, ['dirValue']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['dir.ts', 'app.html']);
assertTextSpans(renameLocations, ['dirValue']);
});
});
describe('when cursor is on a safe prop read', () => {
let cursor: number;
beforeEach(() => {
const fileWithCursor = '<div [dir] #dirRef="myDir"></div> {{ dirRef?.dirV¦alue }}';
const cursorInfo = extractCursorInfo(fileWithCursor);
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, dirFile], [templateFile]);
});
it('should get references', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['dir.ts', 'app.html']);
assertTextSpans(refs, ['dirValue']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['dir.ts', 'app.html']);
assertTextSpans(renameLocations, ['dirValue']);
});
});
describe('when cursor is on safe method call', () => {
let cursor: number;
beforeEach(() => {
const fileWithCursor = '<div [dir] #dirRef="myDir"></div> {{ dirRef?.doSometh¦ing() }}';
const cursorInfo = extractCursorInfo(fileWithCursor);
cursor = cursorInfo.cursor;
const templateFile = {name: _('/app.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, dirFile], [templateFile]);
});
it('should get references', () => {
const refs = getReferencesAtPosition(_('/app.html'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['dir.ts', 'app.html']);
assertTextSpans(refs, ['doSomething']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.html'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['dir.ts', 'app.html']);
assertTextSpans(renameLocations, ['doSomething']);
});
});
});
});
describe('template variables', () => {
describe('when cursor is on variable which was initialized implicitly', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
<div *ngFor="let hero of heroes">
<span *ngIf="hero">
{{her¦o}}
</span>
</div>
`);
cursor = cursorInfo.cursor;
const appFile = {
name: _('/app.ts'),
contents: `
import {Component} from '@angular/core';
@Component({templateUrl: './template.ng.html'})
export class AppCmp {
heroes: string[] = [];
}`
};
const templateFile = {name: _('/template.ng.html'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile], [templateFile]);
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/template.ng.html'), cursor)!;
expect(refs.length).toBe(3);
assertFileNames(refs, ['template.ng.html']);
assertTextSpans(refs, ['hero']);
const originalRefs = env.ngLS.getReferencesAtPosition(_('/template.ng.html'), cursor)!;
// Get the declaration by finding the reference that appears first in the template
originalRefs.sort((a, b) => a.textSpan.start - b.textSpan.start);
expect(originalRefs[0].isDefinition).toBe(true);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/template.ng.html'), cursor)!;
expect(renameLocations.length).toBe(3);
assertFileNames(renameLocations, ['template.ng.html']);
assertTextSpans(renameLocations, ['hero']);
});
});
describe('when cursor is on renamed variable', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div *ngFor="let hero of heroes; let iRef = index">{{iR¦ef}}</div>'})
export class AppCmp {
heroes: string[] = [];
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile]);
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts']);
assertTextSpans(refs, ['iRef']);
const originalRefs = env.ngLS.getReferencesAtPosition(_('/app.ts'), cursor)!;
// Get the declaration by finding the reference that appears first in the template
originalRefs.sort((a, b) => a.textSpan.start - b.textSpan.start);
expect(originalRefs[0].isDefinition).toBe(true);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts']);
assertTextSpans(renameLocations, ['iRef']);
});
});
describe('when cursor is on initializer of variable', () => {
let cursor: number;
beforeEach(() => {
const dirFile = `
import {Directive, Input} from '@angular/core';
export class ExampleContext<T> {
constructor(readonly $implicit: T, readonly identifier: string) {}
}
@Directive({ selector: '[example]' })
export class ExampleDirective<T> {
@Input() set example(v: T) { }
static ngTemplateContextGuard<T>(dir: ExampleDirective<T>, ctx: unknown):
ctx is ExampleContext<T> {
return true;
}
}`;
const fileWithCursor = `
import {Component, NgModule} from '@angular/core';
import {ExampleDirective} from './example-directive';
@Component({template: '<div *example="state; let id = identif¦ier">{{id}}</div>'})
export class AppCmp {
state = {};
}
@NgModule({declarations: [AppCmp, ExampleDirective]})
export class AppModule {}`;
const cursorInfo = extractCursorInfo(fileWithCursor);
cursor = cursorInfo.cursor;
env = LanguageServiceTestEnvironment.setup([
{name: _('/app.ts'), contents: cursorInfo.text, isRoot: true},
{name: _('/example-directive.ts'), contents: dirFile},
]);
env.expectNoSourceDiagnostics();
env.expectNoTemplateDiagnostics(absoluteFrom('/app.ts'), 'AppCmp');
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts', 'example-directive.ts']);
assertTextSpans(refs, ['identifier']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts', 'example-directive.ts']);
assertTextSpans(renameLocations, ['identifier']);
});
});
describe('when cursor is on property read of variable', () => {
let cursor: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div *ngFor="let hero of heroes">{{hero.na¦me}}</div>'})
export class AppCmp {
heroes: Array<{name: string}> = [];
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile]);
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts']);
assertTextSpans(refs, ['name']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts']);
assertTextSpans(renameLocations, ['name']);
});
});
});
describe('pipes', () => {
let prefixPipeFile: TestFile;
let pipeNameExprPosition: number;
beforeEach(() => {
const cursorInfo = extractCursorInfo(`
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({ name: 'pref¦ixPipe' })
export class PrefixPipe implements PipeTransform {
transform(value: string, prefix: string): string;
transform(value: number, prefix: number): number;
transform(value: string|number, prefix: string|number): string|number {
return '';
}
}`);
pipeNameExprPosition = cursorInfo.cursor;
prefixPipeFile = {name: _('/prefix-pipe.ts'), contents: cursorInfo.text};
});
for (const checkTypeOfPipes of [true, false]) {
describe(`when cursor is on pipe name, checkTypeOfPipes: ${checkTypeOfPipes}`, () => {
let cursor: number;
beforeEach(() => {
const appContentsWithCursor = `
import {Component} from '@angular/core';
@Component({template: '{{birthday | prefi¦xPipe: "MM/dd/yy"}}'})
export class AppCmp {
birthday = '';
}
`;
const cursorInfo = extractCursorInfo(appContentsWithCursor);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
// checkTypeOfPipes is set to false when strictTemplates is false
env =
createModuleWithDeclarations([appFile, prefixPipeFile], [], {strictTemplates: false});
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(5);
assertFileNames(refs, ['index.d.ts', 'prefix-pipe.ts', 'app.ts']);
assertTextSpans(refs, ['transform', 'prefixPipe']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['prefix-pipe.ts', 'app.ts']);
assertTextSpans(renameLocations, ['prefixPipe']);
});
});
}
describe('when cursor is on pipe name expression', () => {
it('finds rename locations', () => {
const appFile = {
name: _('/app.ts'),
contents: `
import {Component} from '@angular/core';
@Component({template: '{{birthday | prefixPipe: "MM/dd/yy"}}'})
export class AppCmp {
birthday = '';
}
`
};
env = createModuleWithDeclarations([appFile, prefixPipeFile]);
const renameLocations =
getRenameLocationsAtPosition(prefixPipeFile.name, pipeNameExprPosition)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['prefix-pipe.ts', 'app.ts']);
assertTextSpans(renameLocations, ['prefixPipe']);
});
it('finds rename locations in base class', () => {
const baseFile = {
name: _('/base_pipe.ts'),
contents: `
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({ name: 'basePipe' })
export class BasePipe implements PipeTransform {
transform(value: string, prefix: string): string;
transform(value: number, prefix: number): number;
transform(value: string|number, prefix: string|number): string|number {
return '';
}
}`
};
const cursorInfo = extractCursorInfo(`
import {Pipe, PipeTransform} from '@angular/core';
import {BasePipe} from './base_pipe';
@Pipe({ name: 'prefixPi¦pe' })
export class PrefixPipe extends BasePipe implements PipeTransform {
}`);
env = createModuleWithDeclarations([
baseFile,
{name: _('/prefix_pipe.ts'), contents: cursorInfo.text},
{
name: _('/app.ts'),
contents: `
import {Component} from '@angular/core';
@Component({template: '{{"a" | prefixPipe: "MM/dd/yy"}}'})
export class AppCmp { }
`
},
]);
const renameLocations =
getRenameLocationsAtPosition(_('/prefix_pipe.ts'), cursorInfo.cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['prefix_pipe.ts', 'app.ts']);
assertTextSpans(renameLocations, ['prefixPipe']);
});
});
describe('when cursor is on pipe argument', () => {
let cursor: number;
beforeEach(() => {
const appContentsWithCursor = `
import {Component} from '@angular/core';
@Component({template: '{{birthday | prefixPipe: pr¦efix}}'})
export class AppCmp {
birthday = '';
prefix = '';
}
`;
const cursorInfo = extractCursorInfo(appContentsWithCursor);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, prefixPipeFile]);
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toBe(2);
assertFileNames(refs, ['app.ts']);
assertTextSpans(refs, ['prefix']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toBe(2);
assertFileNames(renameLocations, ['app.ts']);
assertTextSpans(renameLocations, ['prefix']);
});
});
});
describe('inputs', () => {
const dirFileContents = `
import {Directive, Input} from '@angular/core';
@Directive({selector: '[string-model]'})
export class StringModel {
@Input() model!: string;
@Input('alias') aliasedModel!: string;
}`;
describe('when cursor is on the input in the template', () => {
let cursor: number;
beforeEach(() => {
const stringModelTestFile = {name: _('/string-model.ts'), contents: dirFileContents};
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div string-model [mod¦el]="title"></div>'})
export class AppCmp {
title = 'title';
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, stringModelTestFile]);
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toEqual(2);
assertFileNames(refs, ['string-model.ts', 'app.ts']);
assertTextSpans(refs, ['model']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toEqual(2);
assertFileNames(renameLocations, ['string-model.ts', 'app.ts']);
assertTextSpans(renameLocations, ['model']);
});
});
describe('when cursor is on an input that maps to multiple directives', () => {
let cursor: number;
beforeEach(() => {
const otherDirFile = {
name: _('/other-dir.ts'),
contents: `
import {Directive, Input} from '@angular/core';
@Directive({selector: '[string-model]'})
export class OtherDir {
@Input('model') otherDirAliasedInput!: any;
}
`
};
const stringModelTestFile = {name: _('/string-model.ts'), contents: dirFileContents};
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div string-model [mod¦el]="title"></div>'})
export class AppCmp {
title = 'title';
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, stringModelTestFile, otherDirFile]);
});
it('should find references', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toEqual(3);
assertFileNames(refs, ['string-model.ts', 'app.ts', 'other-dir.ts']);
assertTextSpans(refs, ['model', 'otherDirAliasedInput']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations).toBeUndefined();
// TODO(atscott): The below assertions are the correct ones if we were supporting aliases
// expect(renameLocations.length).toEqual(3);
// assertFileNames(renameLocations, ['string-model.ts', 'app.ts', 'other-dir']);
// assertTextSpans(renameLocations, ['model']);
});
});
describe('should work when cursor is on text attribute input', () => {
let cursor: number;
beforeEach(() => {
const stringModelTestFile = {name: _('/string-model.ts'), contents: dirFileContents};
const cursorInfo = extractCursorInfo(`
import {Component} from '@angular/core';
@Component({template: '<div string-model mod¦el="title"></div>'})
export class AppCmp {
title = 'title';
}`);
cursor = cursorInfo.cursor;
const appFile = {name: _('/app.ts'), contents: cursorInfo.text};
env = createModuleWithDeclarations([appFile, stringModelTestFile]);
});
it('should work for text attributes', () => {
const refs = getReferencesAtPosition(_('/app.ts'), cursor)!;
expect(refs.length).toEqual(2);
assertFileNames(refs, ['string-model.ts', 'app.ts']);
assertTextSpans(refs, ['model']);
});
it('should find rename locations', () => {
const renameLocations = getRenameLocationsAtPosition(_('/app.ts'), cursor)!;
expect(renameLocations.length).toEqual(2);
assertFileNames(renameLocations, ['string-model.ts', 'app.ts']);
assertTextSpans(renameLocations, ['model']);
});
});
describe('when cursor is on the class member input', () => {
let cursor: number;
beforeEach(() => {
const dirFileWithCursor = `
import {Directive, Input} from '@angular/core';
@Directive({selector: '[string-model]'})
export class StringModel {
@Input() mod¦el!: string;
}`;
const cursorInfo = extractCursorInfo(dirFileWithCursor);
cursor = cursorInfo.cursor;
const stringModelTestFile = {name: _('/string-model.ts'), contents: cursorInfo.text};
const appFile = {
name: _('/app.ts'),