@@ -156,8 +156,7 @@ describe('type check blocks', () => {
156
156
isGeneric : true ,
157
157
} ] ;
158
158
const block = tcb ( TEMPLATE , DIRECTIVES ) ;
159
- expect ( block ) . toContain (
160
- 'var _t1 = Dir.ngTypeCtor({ "color": (null as any), "strong": (null as any), "enabled": (null as any) });' ) ;
159
+ expect ( block ) . not . toContain ( 'Dir.ngTypeCtor' ) ;
161
160
expect ( block ) . toContain ( '"blue"; false; true;' ) ;
162
161
} ) ;
163
162
@@ -204,9 +203,9 @@ describe('type check blocks', () => {
204
203
] ;
205
204
expect ( tcb ( TEMPLATE , DIRECTIVES ) )
206
205
. toContain (
207
- 'var _t3 = DirA .ngTypeCtor((null!)); ' +
208
- 'var _t2 = DirB .ngTypeCtor({ "inputB ": (_t3) }); ' +
209
- 'var _t1 = DirA .ngTypeCtor({ "inputA ": (_t2) });' ) ;
206
+ 'var _t3 = DirB .ngTypeCtor((null!)); ' +
207
+ 'var _t2 = DirA .ngTypeCtor({ "inputA ": (_t3) }); ' +
208
+ 'var _t1 = DirB .ngTypeCtor({ "inputB ": (_t2) });' ) ;
210
209
} ) ;
211
210
212
211
it ( 'should handle empty bindings' , ( ) => {
@@ -247,9 +246,8 @@ describe('type check blocks', () => {
247
246
} ] ;
248
247
expect ( tcb ( TEMPLATE , DIRECTIVES ) )
249
248
. toContain (
250
- 'var _t1 = Dir.ngTypeCtor({ "fieldA": (((ctx).foo)) }); ' +
251
- 'var _t2: typeof Dir.ngAcceptInputType_fieldA = (null!); ' +
252
- '_t2 = (((ctx).foo));' ) ;
249
+ 'var _t1: typeof Dir.ngAcceptInputType_fieldA = (null!); ' +
250
+ '_t1 = (((ctx).foo));' ) ;
253
251
} ) ;
254
252
} ) ;
255
253
@@ -264,6 +262,58 @@ describe('type check blocks', () => {
264
262
expect ( block ) . toContain ( '(ctx).handle(_t1);' ) ;
265
263
} ) ;
266
264
265
+ it ( 'should only generate directive declarations that have bindings or are referenced' , ( ) => {
266
+ const TEMPLATE = `
267
+ <div
268
+ hasInput [input]="value"
269
+ hasOutput (output)="handle()"
270
+ hasReference #ref="ref"
271
+ noReference
272
+ noBindings>{{ref.a}}</div>
273
+ ` ;
274
+ const DIRECTIVES : TestDeclaration [ ] = [
275
+ {
276
+ type : 'directive' ,
277
+ name : 'HasInput' ,
278
+ selector : '[hasInput]' ,
279
+ inputs : { input : 'input' } ,
280
+ } ,
281
+ {
282
+ type : 'directive' ,
283
+ name : 'HasOutput' ,
284
+ selector : '[hasOutput]' ,
285
+ outputs : { output : 'output' } ,
286
+ } ,
287
+ {
288
+ type : 'directive' ,
289
+ name : 'HasReference' ,
290
+ selector : '[hasReference]' ,
291
+ exportAs : [ 'ref' ] ,
292
+ } ,
293
+ {
294
+ type : 'directive' ,
295
+ name : 'NoReference' ,
296
+ selector : '[noReference]' ,
297
+ exportAs : [ 'no-ref' ] ,
298
+ } ,
299
+ {
300
+ type : 'directive' ,
301
+ name : 'NoBindings' ,
302
+ selector : '[noBindings]' ,
303
+ inputs : { unset : 'unset' } ,
304
+ } ,
305
+ ] ;
306
+ const block = tcb ( TEMPLATE , DIRECTIVES ) ;
307
+ expect ( block ) . toContain ( 'var _t1: HasInput = (null!)' ) ;
308
+ expect ( block ) . toContain ( '_t1.input = (((ctx).value));' ) ;
309
+ expect ( block ) . toContain ( 'var _t2: HasOutput = (null!)' ) ;
310
+ expect ( block ) . toContain ( '_t2["output"]' ) ;
311
+ expect ( block ) . toContain ( 'var _t3: HasReference = (null!)' ) ;
312
+ expect ( block ) . toContain ( '(_t3).a' ) ;
313
+ expect ( block ) . not . toContain ( 'NoBindings' ) ;
314
+ expect ( block ) . not . toContain ( 'NoReference' ) ;
315
+ } ) ;
316
+
267
317
it ( 'should generate a forward element reference correctly' , ( ) => {
268
318
const TEMPLATE = `
269
319
{{ i.value }}
@@ -310,7 +360,7 @@ describe('type check blocks', () => {
310
360
inputs : { 'color' : 'color' , 'strong' : 'strong' , 'enabled' : 'enabled' } ,
311
361
} ] ;
312
362
const block = tcb ( TEMPLATE , DIRECTIVES ) ;
313
- expect ( block ) . toContain ( 'var _t1: Dir = (null!);' ) ;
363
+ expect ( block ) . not . toContain ( 'var _t1: Dir = (null!);' ) ;
314
364
expect ( block ) . not . toContain ( '"color"' ) ;
315
365
expect ( block ) . not . toContain ( '"strong"' ) ;
316
366
expect ( block ) . not . toContain ( '"enabled"' ) ;
@@ -357,10 +407,10 @@ describe('type check blocks', () => {
357
407
] ;
358
408
expect ( tcb ( TEMPLATE , DIRECTIVES ) )
359
409
. toContain (
360
- 'var _t1: DirA = (null!); ' +
361
- 'var _t2: DirB = (null!); ' +
362
- '_t1 .inputA = (_t2 ); ' +
363
- '_t2 .inputA = (_t1 );' ) ;
410
+ 'var _t1: DirB = (null!); ' +
411
+ 'var _t2: DirA = (null!); ' +
412
+ '_t2 .inputA = (_t1 ); ' +
413
+ '_t1 .inputA = (_t2 );' ) ;
364
414
} ) ;
365
415
366
416
it ( 'should handle undeclared properties' , ( ) => {
@@ -374,10 +424,9 @@ describe('type check blocks', () => {
374
424
} ,
375
425
undeclaredInputFields : [ 'fieldA' ]
376
426
} ] ;
377
- expect ( tcb ( TEMPLATE , DIRECTIVES ) )
378
- . toContain (
379
- 'var _t1: Dir = (null!); ' +
380
- '(((ctx).foo)); ' ) ;
427
+ const block = tcb ( TEMPLATE , DIRECTIVES ) ;
428
+ expect ( block ) . not . toContain ( 'var _t1: Dir = (null!);' ) ;
429
+ expect ( block ) . toContain ( '(((ctx).foo)); ' ) ;
381
430
} ) ;
382
431
383
432
it ( 'should assign restricted properties to temp variables by default' , ( ) => {
@@ -448,9 +497,9 @@ describe('type check blocks', () => {
448
497
} ] ;
449
498
expect ( tcb ( TEMPLATE , DIRECTIVES ) )
450
499
. toContain (
451
- 'var _t1: Dir = (null!); ' +
452
- 'var _t2: typeof Dir.ngAcceptInputType_field1 = (null!); ' +
453
- '_t1 .field2 = _t2 = (((ctx).foo));' ) ;
500
+ 'var _t1: typeof Dir.ngAcceptInputType_field1 = (null!); ' +
501
+ 'var _t2: Dir = (null!); ' +
502
+ '_t2 .field2 = _t1 = (((ctx).foo));' ) ;
454
503
} ) ;
455
504
456
505
it ( 'should handle a single property bound to multiple fields, where one of them is undeclared' ,
@@ -483,11 +532,11 @@ describe('type check blocks', () => {
483
532
} ,
484
533
coercedInputFields : [ 'fieldA' ] ,
485
534
} ] ;
486
- expect ( tcb ( TEMPLATE , DIRECTIVES ) )
487
- . toContain (
488
- 'var _t1: Dir = (null!); ' +
489
- 'var _t2 : typeof Dir.ngAcceptInputType_fieldA = (null!); ' +
490
- '_t2 = (((ctx).foo));') ;
535
+ const block = tcb ( TEMPLATE , DIRECTIVES ) ;
536
+ expect ( block ) . not . toContain ( 'var _t1: Dir = (null!);' ) ;
537
+ expect ( block ) . toContain (
538
+ 'var _t1 : typeof Dir.ngAcceptInputType_fieldA = (null!); ' +
539
+ '_t1 = (((ctx).foo));') ;
491
540
} ) ;
492
541
493
542
it ( 'should use coercion types if declared, even when backing field is not declared' , ( ) => {
@@ -502,11 +551,11 @@ describe('type check blocks', () => {
502
551
coercedInputFields : [ 'fieldA' ] ,
503
552
undeclaredInputFields : [ 'fieldA' ] ,
504
553
} ] ;
505
- expect ( tcb ( TEMPLATE , DIRECTIVES ) )
506
- . toContain (
507
- 'var _t1: Dir = (null!); ' +
508
- 'var _t2 : typeof Dir.ngAcceptInputType_fieldA = (null!); ' +
509
- '_t2 = (((ctx).foo));') ;
554
+ const block = tcb ( TEMPLATE , DIRECTIVES ) ;
555
+ expect ( block ) . not . toContain ( 'var _t1: Dir = (null!);' ) ;
556
+ expect ( block ) . toContain (
557
+ 'var _t1 : typeof Dir.ngAcceptInputType_fieldA = (null!); ' +
558
+ '_t1 = (((ctx).foo));') ;
510
559
} ) ;
511
560
512
561
it ( 'should handle $any casts' , ( ) => {
@@ -721,7 +770,7 @@ describe('type check blocks', () => {
721
770
expect ( block ) . toContain ( 'function ($event: any): any { (ctx).foo($event); }' ) ;
722
771
// Note that DOM events are still checked, that is controlled by `checkTypeOfDomEvents`
723
772
expect ( block ) . toContain (
724
- '_t2 .addEventListener("nonDirOutput", function ($event): any { (ctx).foo($event); });' ) ;
773
+ '_t1 .addEventListener("nonDirOutput", function ($event): any { (ctx).foo($event); });' ) ;
725
774
} ) ;
726
775
} ) ;
727
776
0 commit comments