forked from angular/angular
/
type_check_block_spec.ts
1580 lines (1438 loc) · 55.7 KB
/
type_check_block_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 ts from 'typescript';
import {initMockFileSystem} from '../../file_system/testing';
import {TypeCheckingConfig} from '../api';
import {ALL_ENABLED_CONFIG, tcb, TestDeclaration, TestDirective} from '../testing';
describe('type check blocks', () => {
beforeEach(() => initMockFileSystem('Native'));
it('should generate a basic block for a binding', () => {
expect(tcb('{{hello}} {{world}}')).toContain('"" + (((this).hello)) + (((this).world));');
});
it('should generate literal map expressions', () => {
const TEMPLATE = '{{ method({foo: a, bar: b}) }}';
expect(tcb(TEMPLATE)).toContain('(this).method({ "foo": ((this).a), "bar": ((this).b) })');
});
it('should generate literal array expressions', () => {
const TEMPLATE = '{{ method([a, b]) }}';
expect(tcb(TEMPLATE)).toContain('(this).method([((this).a), ((this).b)])');
});
it('should handle non-null assertions', () => {
const TEMPLATE = `{{a!}}`;
expect(tcb(TEMPLATE)).toContain('((((this).a))!)');
});
it('should handle unary - operator', () => {
const TEMPLATE = `{{-1}}`;
expect(tcb(TEMPLATE)).toContain('(-1)');
});
it('should handle keyed property access', () => {
const TEMPLATE = `{{a[b]}}`;
expect(tcb(TEMPLATE)).toContain('(((this).a))[((this).b)]');
});
it('should handle nested ternary expressions', () => {
const TEMPLATE = `{{a ? b : c ? d : e}}`;
expect(tcb(TEMPLATE))
.toContain('(((this).a) ? ((this).b) : ((((this).c) ? ((this).d) : (((this).e)))))');
});
it('should handle nullish coalescing operator', () => {
expect(tcb('{{ a ?? b }}')).toContain('((((this).a)) ?? (((this).b)))');
expect(tcb('{{ a ?? b ?? c }}')).toContain('(((((this).a)) ?? (((this).b))) ?? (((this).c)))');
expect(tcb('{{ (a ?? b) + (c ?? e) }}'))
.toContain('(((((this).a)) ?? (((this).b))) + ((((this).c)) ?? (((this).e))))');
});
it('should handle attribute values for directive inputs', () => {
const TEMPLATE = `<div dir inputA="value"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'DirA',
selector: '[dir]',
inputs: {inputA: 'inputA'},
}];
expect(tcb(TEMPLATE, DIRECTIVES)).toContain('_t1: i0.DirA = null!; _t1.inputA = ("value");');
});
it('should handle multiple bindings to the same property', () => {
const TEMPLATE = `<div dir-a [inputA]="1" [inputA]="2"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'DirA',
selector: '[dir-a]',
inputs: {inputA: 'inputA'},
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('_t1.inputA = (1);');
expect(block).toContain('_t1.inputA = (2);');
});
it('should handle empty bindings', () => {
const TEMPLATE = `<div dir-a [inputA]=""></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'DirA',
selector: '[dir-a]',
inputs: {inputA: 'inputA'},
}];
expect(tcb(TEMPLATE, DIRECTIVES)).toContain('_t1.inputA = (undefined);');
});
it('should handle bindings without value', () => {
const TEMPLATE = `<div dir-a [inputA]></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'DirA',
selector: '[dir-a]',
inputs: {inputA: 'inputA'},
}];
expect(tcb(TEMPLATE, DIRECTIVES)).toContain('_t1.inputA = (undefined);');
});
it('should handle implicit vars on ng-template', () => {
const TEMPLATE = `<ng-template let-a></ng-template>`;
expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;');
});
it('should handle method calls of template variables', () => {
const TEMPLATE = `<ng-template let-a>{{a(1)}}</ng-template>`;
expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;');
expect(tcb(TEMPLATE)).toContain('_t2(1)');
});
it('should handle implicit vars when using microsyntax', () => {
const TEMPLATE = `<div *ngFor="let user of users"></div>`;
expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;');
});
it('should handle direct calls of an implicit template variable', () => {
const TEMPLATE = `<div *ngFor="let a of letters">{{a(1)}}</div>`;
expect(tcb(TEMPLATE)).toContain('var _t2 = _t1.$implicit;');
expect(tcb(TEMPLATE)).toContain('_t2(1)');
});
describe('type constructors', () => {
it('should handle missing property bindings', () => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: 'inputA',
fieldB: 'inputB',
},
isGeneric: true,
}];
const actual = tcb(TEMPLATE, DIRECTIVES);
expect(actual).toContain(
'const _ctor1: <T extends string = any>(init: Pick<i0.Dir<T>, "fieldA" | "fieldB">) => i0.Dir<T> = null!;');
expect(actual).toContain(
'var _t1 = _ctor1({ "fieldA": (((this).foo)), "fieldB": null as any });');
});
it('should handle multiple bindings to the same property', () => {
const TEMPLATE = `<div dir [inputA]="1" [inputA]="2"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: 'inputA',
},
isGeneric: true,
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('"fieldA": (1)');
expect(block).not.toContain('"fieldA": (2)');
});
it('should only apply property bindings to directives', () => {
const TEMPLATE = `
<div dir [style.color]="'blue'" [class.strong]="false" [attr.enabled]="true"></div>
`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {'color': 'color', 'strong': 'strong', 'enabled': 'enabled'},
isGeneric: true,
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).not.toContain('Dir.ngTypeCtor');
expect(block).toContain('"blue"; false; true;');
});
it('should generate a circular directive reference correctly', () => {
const TEMPLATE = `
<div dir #d="dir" [input]="d"></div>
`;
const DIRECTIVES: TestDirective[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
exportAs: ['dir'],
inputs: {input: 'input'},
isGeneric: true,
}];
const actual = tcb(TEMPLATE, DIRECTIVES);
expect(actual).toContain(
'const _ctor1: <T extends string = any>(init: Pick<i0.Dir<T>, "input">) => i0.Dir<T> = null!;');
expect(actual).toContain(
'var _t2 = _ctor1({ "input": (null!) }); ' +
'var _t1 = _t2; ' +
'_t2.input = (_t1);');
});
it('should generate circular references between two directives correctly', () => {
const TEMPLATE = `
<div #a="dirA" dir-a [inputA]="b">A</div>
<div #b="dirB" dir-b [inputB]="a">B</div>
`;
const DIRECTIVES: TestDirective[] = [
{
type: 'directive',
name: 'DirA',
selector: '[dir-a]',
exportAs: ['dirA'],
inputs: {inputA: 'inputA'},
isGeneric: true,
},
{
type: 'directive',
name: 'DirB',
selector: '[dir-b]',
exportAs: ['dirB'],
inputs: {inputB: 'inputB'},
isGeneric: true,
}
];
const actual = tcb(TEMPLATE, DIRECTIVES);
expect(actual).toContain(
'const _ctor1: <T extends string = any>(init: Pick<i0.DirA<T>, "inputA">) => i0.DirA<T> = null!; const _ctor2: <T extends string = any>(init: Pick<i0.DirB<T>, "inputB">) => i0.DirB<T> = null!;');
expect(actual).toContain(
'var _t4 = _ctor1({ "inputA": (null!) }); ' +
'var _t3 = _t4; ' +
'var _t2 = _ctor2({ "inputB": (_t3) }); ' +
'var _t1 = _t2; ' +
'_t4.inputA = (_t1); ' +
'_t2.inputB = (_t3);');
});
it('should handle empty bindings', () => {
const TEMPLATE = `<div dir-a [inputA]=""></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'DirA',
selector: '[dir-a]',
inputs: {inputA: 'inputA'},
isGeneric: true,
}];
expect(tcb(TEMPLATE, DIRECTIVES)).toContain('"inputA": (undefined)');
});
it('should handle bindings without value', () => {
const TEMPLATE = `<div dir-a [inputA]></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'DirA',
selector: '[dir-a]',
inputs: {inputA: 'inputA'},
isGeneric: true,
}];
expect(tcb(TEMPLATE, DIRECTIVES)).toContain('"inputA": (undefined)');
});
it('should use coercion types if declared', () => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: 'inputA',
},
isGeneric: true,
coercedInputFields: ['fieldA'],
}];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t1: typeof i0.Dir.ngAcceptInputType_fieldA = null!; ' +
'_t1 = (((this).foo));');
});
});
it('should only generate code for DOM elements that are actually referenced', () => {
const TEMPLATE = `
<div></div>
<button #me (click)="handle(me)"></button>
`;
const block = tcb(TEMPLATE);
expect(block).not.toContain('"div"');
expect(block).toContain(
'var _t2 = document.createElement("button"); ' +
'var _t1 = _t2; ' +
'_t2.addEventListener');
});
it('should only generate directive declarations that have bindings or are referenced', () => {
const TEMPLATE = `
<div
hasInput [input]="value"
hasOutput (output)="handle()"
hasReference #ref="ref"
noReference
noBindings>{{ref.a}}</div>
`;
const DIRECTIVES: TestDeclaration[] = [
{
type: 'directive',
name: 'HasInput',
selector: '[hasInput]',
inputs: {input: 'input'},
},
{
type: 'directive',
name: 'HasOutput',
selector: '[hasOutput]',
outputs: {output: 'output'},
},
{
type: 'directive',
name: 'HasReference',
selector: '[hasReference]',
exportAs: ['ref'],
},
{
type: 'directive',
name: 'NoReference',
selector: '[noReference]',
exportAs: ['no-ref'],
},
{
type: 'directive',
name: 'NoBindings',
selector: '[noBindings]',
inputs: {unset: 'unset'},
},
];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('var _t1: i0.HasInput = null!');
expect(block).toContain('_t1.input = (((this).value));');
expect(block).toContain('var _t2: i0.HasOutput = null!');
expect(block).toContain('_t2["output"]');
expect(block).toContain('var _t4: i0.HasReference = null!');
expect(block).toContain('var _t3 = _t4;');
expect(block).toContain('(_t3).a');
expect(block).not.toContain('NoBindings');
expect(block).not.toContain('NoReference');
});
it('should generate a forward element reference correctly', () => {
const TEMPLATE = `
{{ i.value }}
<input #i>
`;
expect(tcb(TEMPLATE))
.toContain(
'var _t2 = document.createElement("input"); var _t1 = _t2; "" + (((_t1).value));');
});
it('should generate a forward directive reference correctly', () => {
const TEMPLATE = `
{{d.value}}
<div dir #d="dir"></div>
`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
exportAs: ['dir'],
}];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t2: i0.Dir = null!; ' +
'var _t1 = _t2; ' +
'"" + (((_t1).value));');
});
it('should handle style and class bindings specially', () => {
const TEMPLATE = `
<div [style]="a" [class]="b"></div>
`;
const block = tcb(TEMPLATE);
expect(block).toContain('((this).a); ((this).b);');
// There should be no assignments to the class or style properties.
expect(block).not.toContain('.class = ');
expect(block).not.toContain('.style = ');
});
it('should only apply property bindings to directives', () => {
const TEMPLATE = `
<div dir [style.color]="'blue'" [class.strong]="false" [attr.enabled]="true"></div>
`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {'color': 'color', 'strong': 'strong', 'enabled': 'enabled'},
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).not.toContain('var _t1: Dir = null!;');
expect(block).not.toContain('"color"');
expect(block).not.toContain('"strong"');
expect(block).not.toContain('"enabled"');
expect(block).toContain('"blue"; false; true;');
});
it('should generate a circular directive reference correctly', () => {
const TEMPLATE = `
<div dir #d="dir" [input]="d"></div>
`;
const DIRECTIVES: TestDirective[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
exportAs: ['dir'],
inputs: {input: 'input'},
}];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t2: i0.Dir = null!; ' +
'var _t1 = _t2; ' +
'_t2.input = (_t1);');
});
it('should generate circular references between two directives correctly', () => {
const TEMPLATE = `
<div #a="dirA" dir-a [inputA]="b">A</div>
<div #b="dirB" dir-b [inputB]="a">B</div>
`;
const DIRECTIVES: TestDirective[] = [
{
type: 'directive',
name: 'DirA',
selector: '[dir-a]',
exportAs: ['dirA'],
inputs: {inputA: 'inputA'},
},
{
type: 'directive',
name: 'DirB',
selector: '[dir-b]',
exportAs: ['dirB'],
inputs: {inputA: 'inputB'},
}
];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t2: i0.DirB = null!; ' +
'var _t1 = _t2; ' +
'var _t3: i0.DirA = null!; ' +
'_t3.inputA = (_t1); ' +
'var _t4 = _t3; ' +
'_t2.inputA = (_t4);');
});
it('should handle undeclared properties', () => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: 'inputA',
},
undeclaredInputFields: ['fieldA']
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).not.toContain('var _t1: Dir = null!;');
expect(block).toContain('(((this).foo)); ');
});
it('should assign restricted properties to temp variables by default', () => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: 'inputA',
},
restrictedInputFields: ['fieldA']
}];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t1: i0.Dir = null!; ' +
'var _t2: (typeof _t1)["fieldA"] = null!; ' +
'_t2 = (((this).foo)); ');
});
it('should assign properties via element access for field names that are not JS identifiers',
() => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
'some-input.xs': 'inputA',
},
stringLiteralInputFields: ['some-input.xs'],
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain(
'var _t1: i0.Dir = null!; ' +
'_t1["some-input.xs"] = (((this).foo)); ');
});
it('should handle a single property bound to multiple fields', () => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
field1: 'inputA',
field2: 'inputA',
},
}];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t1: i0.Dir = null!; ' +
'_t1.field2 = _t1.field1 = (((this).foo));');
});
it('should handle a single property bound to multiple fields, where one of them is coerced',
() => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
field1: 'inputA',
field2: 'inputA',
},
coercedInputFields: ['field1'],
}];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t1: typeof i0.Dir.ngAcceptInputType_field1 = null!; ' +
'var _t2: i0.Dir = null!; ' +
'_t2.field2 = _t1 = (((this).foo));');
});
it('should handle a single property bound to multiple fields, where one of them is undeclared',
() => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
field1: 'inputA',
field2: 'inputA',
},
undeclaredInputFields: ['field1'],
}];
expect(tcb(TEMPLATE, DIRECTIVES))
.toContain(
'var _t1: i0.Dir = null!; ' +
'_t1.field2 = (((this).foo));');
});
it('should use coercion types if declared', () => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: 'inputA',
},
coercedInputFields: ['fieldA'],
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).not.toContain('var _t1: Dir = null!;');
expect(block).toContain(
'var _t1: typeof i0.Dir.ngAcceptInputType_fieldA = null!; ' +
'_t1 = (((this).foo));');
});
it('should use coercion types if declared, even when backing field is not declared', () => {
const TEMPLATE = `<div dir [inputA]="foo"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: 'inputA',
},
coercedInputFields: ['fieldA'],
undeclaredInputFields: ['fieldA'],
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).not.toContain('var _t1: Dir = null!;');
expect(block).toContain(
'var _t1: typeof i0.Dir.ngAcceptInputType_fieldA = null!; ' +
'_t1 = (((this).foo));');
});
it('should use transform type if an input has one', () => {
const TEMPLATE = `<div dir [fieldA]="expr"></div>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {
fieldA: {
bindingPropertyName: 'fieldA',
classPropertyName: 'fieldA',
required: false,
transform: {
node: ts.factory.createFunctionDeclaration(
undefined, undefined, undefined, undefined, [], undefined, undefined),
type: ts.factory.createUnionTypeNode([
ts.factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword),
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
])
},
},
},
coercedInputFields: ['fieldA'],
}];
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain(
'var _t1: boolean | string = null!; ' +
'_t1 = (((this).expr));');
});
it('should handle $any casts', () => {
const TEMPLATE = `{{$any(a)}}`;
const block = tcb(TEMPLATE);
expect(block).toContain('(((this).a) as any)');
});
it('should handle $any accessed through `this`', () => {
const TEMPLATE = `{{this.$any(a)}}`;
const block = tcb(TEMPLATE);
expect(block).toContain('((this).$any(((this).a)))');
});
it('should handle $any accessed through a property read', () => {
const TEMPLATE = `{{foo.$any(a)}}`;
const block = tcb(TEMPLATE);
expect(block).toContain('((((this).foo)).$any(((this).a)))');
});
describe('experimental DOM checking via lib.dom.d.ts', () => {
it('should translate unclaimed bindings to their property equivalent', () => {
const TEMPLATE = `<label [for]="'test'"></label>`;
const CONFIG = {...ALL_ENABLED_CONFIG, checkTypeOfDomBindings: true};
expect(tcb(TEMPLATE, /* declarations */ undefined, CONFIG))
.toContain('_t1["htmlFor"] = ("test");');
});
});
describe('template guards', () => {
it('should emit invocation guards', () => {
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'NgIf',
selector: '[ngIf]',
inputs: {'ngIf': 'ngIf'},
ngTemplateGuards: [{
inputName: 'ngIf',
type: 'invocation',
}]
}];
const TEMPLATE = `<div *ngIf="person">{{person.name}}</div>`;
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('if (i0.NgIf.ngTemplateGuard_ngIf(_t1, ((this).person)))');
});
it('should emit binding guards', () => {
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'NgIf',
selector: '[ngIf]',
inputs: {'ngIf': 'ngIf'},
ngTemplateGuards: [{
inputName: 'ngIf',
type: 'binding',
}]
}];
const TEMPLATE = `<div *ngIf="person !== null">{{person.name}}</div>`;
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('if ((((this).person)) !== (null))');
});
it('should not emit guards when the child scope is empty', () => {
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'NgIf',
selector: '[ngIf]',
inputs: {'ngIf': 'ngIf'},
ngTemplateGuards: [{
inputName: 'ngIf',
type: 'invocation',
}]
}];
const TEMPLATE = `<div *ngIf="person">static</div>`;
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).not.toContain('NgIf.ngTemplateGuard_ngIf');
});
});
describe('outputs', () => {
it('should emit subscribe calls for directive outputs', () => {
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
outputs: {'outputField': 'dirOutput'},
}];
const TEMPLATE = `<div dir (dirOutput)="foo($event)"></div>`;
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain(
'_t1["outputField"].subscribe(($event): any => { (this).foo($event); });');
});
it('should emit a listener function with AnimationEvent for animation events', () => {
const TEMPLATE = `<div (@animation.done)="foo($event)"></div>`;
const block = tcb(TEMPLATE);
expect(block).toContain('($event: i1.AnimationEvent): any => { (this).foo($event); }');
});
it('should emit addEventListener calls for unclaimed outputs', () => {
const TEMPLATE = `<div (event)="foo($event)"></div>`;
const block = tcb(TEMPLATE);
expect(block).toContain(
'_t1.addEventListener("event", ($event): any => { (this).foo($event); });');
});
it('should allow to cast $event using $any', () => {
const TEMPLATE = `<div (event)="foo($any($event))"></div>`;
const block = tcb(TEMPLATE);
expect(block).toContain(
'_t1.addEventListener("event", ($event): any => { (this).foo(($event as any)); });');
});
it('should detect writes to template variables', () => {
const TEMPLATE = `<ng-template let-v><div (event)="v = 3"></div></ng-template>`;
const block = tcb(TEMPLATE);
expect(block).toContain('_t3.addEventListener("event", ($event): any => { (_t2 = 3); });');
});
it('should ignore accesses to $event through `this`', () => {
const TEMPLATE = `<div (event)="foo(this.$event)"></div>`;
const block = tcb(TEMPLATE);
expect(block).toContain(
'_t1.addEventListener("event", ($event): any => { (this).foo(((this).$event)); });');
});
});
describe('config', () => {
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
exportAs: ['dir'],
inputs: {'dirInput': 'dirInput'},
outputs: {'outputField': 'dirOutput'},
hasNgTemplateContextGuard: true,
}];
const BASE_CONFIG: TypeCheckingConfig = {
applyTemplateContextGuards: true,
checkQueries: false,
checkTemplateBodies: true,
alwaysCheckSchemaInTemplateBodies: true,
checkTypeOfInputBindings: true,
honorAccessModifiersForInputBindings: false,
strictNullInputBindings: true,
checkTypeOfAttributes: true,
checkTypeOfDomBindings: false,
checkTypeOfOutputEvents: true,
checkTypeOfAnimationEvents: true,
checkTypeOfDomEvents: true,
checkTypeOfDomReferences: true,
checkTypeOfNonDomReferences: true,
checkTypeOfPipes: true,
strictSafeNavigationTypes: true,
useContextGenericType: true,
strictLiteralTypes: true,
enableTemplateTypeChecker: false,
useInlineTypeConstructors: true,
suggestionsForSuboptimalTypeInference: false,
};
describe('config.applyTemplateContextGuards', () => {
const TEMPLATE = `<div *dir>{{ value }}</div>`;
const GUARD_APPLIED = 'if (i0.Dir.ngTemplateContextGuard(';
it('should apply template context guards when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain(GUARD_APPLIED);
});
it('should not apply template context guards when disabled', () => {
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, applyTemplateContextGuards: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).not.toContain(GUARD_APPLIED);
});
});
describe('config.checkTemplateBodies', () => {
const TEMPLATE = `<ng-template #ref>{{a}}</ng-template>{{ref}}`;
it('should descend into template bodies when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('((this).a)');
});
it('should not descend into template bodies when disabled', () => {
const DISABLED_CONFIG: TypeCheckingConfig = {...BASE_CONFIG, checkTemplateBodies: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).not.toContain('((this).a)');
});
it('generates a references var when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('var _t1 = (_t2 as any as i1.TemplateRef<any>);');
});
it('generates a reference var when disabled', () => {
const DISABLED_CONFIG: TypeCheckingConfig = {...BASE_CONFIG, checkTemplateBodies: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).toContain('var _t1 = (_t2 as any as i1.TemplateRef<any>);');
});
});
describe('config.strictNullInputBindings', () => {
const TEMPLATE = `<div dir [dirInput]="a" [nonDirInput]="b"></div>`;
it('should include null and undefined when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('_t1.dirInput = (((this).a));');
expect(block).toContain('((this).b);');
});
it('should use the non-null assertion operator when disabled', () => {
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, strictNullInputBindings: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).toContain('_t1.dirInput = (((this).a)!);');
expect(block).toContain('((this).b)!;');
});
});
describe('config.checkTypeOfBindings', () => {
it('should check types of bindings when enabled', () => {
const TEMPLATE = `<div dir [dirInput]="a" [nonDirInput]="b"></div>`;
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('_t1.dirInput = (((this).a));');
expect(block).toContain('((this).b);');
});
it('should not check types of bindings when disabled', () => {
const TEMPLATE = `<div dir [dirInput]="a" [nonDirInput]="b"></div>`;
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, checkTypeOfInputBindings: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).toContain('_t1.dirInput = ((((this).a) as any));');
expect(block).toContain('(((this).b) as any);');
});
it('should wrap the cast to any in parentheses when required', () => {
const TEMPLATE = `<div dir [dirInput]="a === b"></div>`;
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, checkTypeOfInputBindings: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).toContain('_t1.dirInput = ((((((this).a)) === (((this).b))) as any));');
});
});
describe('config.checkTypeOfOutputEvents', () => {
const TEMPLATE = `<div dir (dirOutput)="foo($event)" (nonDirOutput)="foo($event)"></div>`;
it('should check types of directive outputs when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain(
'_t1["outputField"].subscribe(($event): any => { (this).foo($event); });');
expect(block).toContain(
'_t2.addEventListener("nonDirOutput", ($event): any => { (this).foo($event); });');
});
it('should not check types of directive outputs when disabled', () => {
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, checkTypeOfOutputEvents: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).toContain('($event: any): any => { (this).foo($event); }');
// Note that DOM events are still checked, that is controlled by `checkTypeOfDomEvents`
expect(block).toContain(
'addEventListener("nonDirOutput", ($event): any => { (this).foo($event); });');
});
});
describe('config.checkTypeOfAnimationEvents', () => {
const TEMPLATE = `<div (@animation.done)="foo($event)"></div>`;
it('should check types of animation events when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('($event: i1.AnimationEvent): any => { (this).foo($event); }');
});
it('should not check types of animation events when disabled', () => {
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, checkTypeOfAnimationEvents: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).toContain('($event: any): any => { (this).foo($event); }');
});
});
describe('config.checkTypeOfDomEvents', () => {
const TEMPLATE = `<div dir (dirOutput)="foo($event)" (nonDirOutput)="foo($event)"></div>`;
it('should check types of DOM events when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain(
'_t1["outputField"].subscribe(($event): any => { (this).foo($event); });');
expect(block).toContain(
'_t2.addEventListener("nonDirOutput", ($event): any => { (this).foo($event); });');
});
it('should not check types of DOM events when disabled', () => {
const DISABLED_CONFIG: TypeCheckingConfig = {...BASE_CONFIG, checkTypeOfDomEvents: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
// Note that directive outputs are still checked, that is controlled by
// `checkTypeOfOutputEvents`
expect(block).toContain(
'_t1["outputField"].subscribe(($event): any => { (this).foo($event); });');
expect(block).toContain('($event: any): any => { (this).foo($event); }');
});
});
describe('config.checkTypeOfDomReferences', () => {
const TEMPLATE = `<input #ref>{{ref.value}}`;
it('should trace references when enabled', () => {
const block = tcb(TEMPLATE);
expect(block).toContain('(_t1).value');
});
it('should use any for reference types when disabled', () => {
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, checkTypeOfDomReferences: false};
const block = tcb(TEMPLATE, [], DISABLED_CONFIG);
expect(block).toContain(
'var _t1 = _t2 as any; ' +
'"" + (((_t1).value));');
});
});
describe('config.checkTypeOfNonDomReferences', () => {
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
exportAs: ['dir'],
inputs: {'dirInput': 'dirInput'},
outputs: {'outputField': 'dirOutput'},
hasNgTemplateContextGuard: true,
}];
const TEMPLATE =
`<div dir #ref="dir">{{ref.value}}</div><ng-template #ref2></ng-template>{{ref2.value2}}`;
it('should trace references to a directive when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('(_t1).value');
});
it('should trace references to an <ng-template> when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain(
'var _t3 = (_t4 as any as i1.TemplateRef<any>); ' +
'"" + (((_t3).value2));');
});
it('should use any for reference types when disabled', () => {
const DISABLED_CONFIG:
TypeCheckingConfig = {...BASE_CONFIG, checkTypeOfNonDomReferences: false};
const block = tcb(TEMPLATE, DIRECTIVES, DISABLED_CONFIG);
expect(block).toContain(
'var _t1 = _t2 as any; ' +
'"" + (((_t1).value));');
});
});
describe('config.checkTypeOfAttributes', () => {
const TEMPLATE = `<textarea dir disabled cols="3" [rows]="2">{{ref.value}}</textarea>`;
const DIRECTIVES: TestDeclaration[] = [{
type: 'directive',
name: 'Dir',
selector: '[dir]',
inputs: {'disabled': 'disabled', 'cols': 'cols', 'rows': 'rows'},
}];
it('should assign string value to the input when enabled', () => {
const block = tcb(TEMPLATE, DIRECTIVES);
expect(block).toContain('_t1.disabled = ("");');
expect(block).toContain('_t1.cols = ("3");');
expect(block).toContain('_t1.rows = (2);');
});