@@ -287,6 +287,16 @@ describe('static-queries migration', () => {
287
287
288
288
ngOnInit() {
289
289
new A(this);
290
+
291
+ new class Inline {
292
+ constructor(private ctx: MyComp) {
293
+ this.a();
294
+ }
295
+
296
+ a() {
297
+ this.ctx.query2.useStatically();
298
+ }
299
+ }(this);
290
300
}
291
301
}
292
302
@@ -302,7 +312,33 @@ describe('static-queries migration', () => {
302
312
expect ( tree . readContent ( '/index.ts' ) )
303
313
. toContain ( `@${ queryType } ('test', { static: true }) query: any;` ) ;
304
314
expect ( tree . readContent ( '/index.ts' ) )
305
- . toContain ( `@${ queryType } ('test', { static: false }) query2: any;` ) ;
315
+ . toContain ( `@${ queryType } ('test', { static: true }) query2: any;` ) ;
316
+ } ) ;
317
+
318
+ it ( 'should detect queries used in parenthesized new expressions' , ( ) => {
319
+ writeFile ( '/index.ts' , `
320
+ import {Component, ${ queryType } } from '@angular/core';
321
+
322
+ @Component({template: '<span #test></span>'})
323
+ export class MyComp {
324
+ @${ queryType } ('test') query: any;
325
+
326
+ ngOnInit() {
327
+ new ((A))(this);
328
+ }
329
+ }
330
+
331
+ export class A {
332
+ constructor(ctx: MyComp) {
333
+ ctx.query.test();
334
+ }
335
+ }
336
+ ` ) ;
337
+
338
+ runMigration ( ) ;
339
+
340
+ expect ( tree . readContent ( '/index.ts' ) )
341
+ . toContain ( `@${ queryType } ('test', { static: true }) query: any;` ) ;
306
342
} ) ;
307
343
308
344
it ( 'should detect queries in lifecycle hook with string literal name' , ( ) => {
@@ -520,5 +556,159 @@ describe('static-queries migration', () => {
520
556
expect ( tree . readContent ( '/src/index.ts' ) )
521
557
. toContain ( `@${ queryType } ('test', { static: true }) query: any;` ) ;
522
558
} ) ;
559
+
560
+ it ( 'should not mark queries used in promises as static' , ( ) => {
561
+ writeFile ( '/index.ts' , `
562
+ import {Component, ${ queryType } } from '@angular/core';
563
+
564
+ @Component({template: '<span #test></span>'})
565
+ export class MyComp {
566
+ private @${ queryType } ('test') query: any;
567
+ private @${ queryType } ('test') query2: any;
568
+
569
+ ngOnInit() {
570
+ const a = Promise.resolve();
571
+
572
+ Promise.resolve().then(() => {
573
+ this.query.doSomething();
574
+ });
575
+
576
+ Promise.reject().catch(() => {
577
+ this.query.doSomething();
578
+ });
579
+
580
+ a.then(() => {}).then(() => {
581
+ this.query.doSomething();
582
+ });
583
+
584
+ Promise.resolve().then(this.createPromiseCb());
585
+ }
586
+
587
+ createPromiseCb() {
588
+ this.query2.doSomething();
589
+ return () => { /* empty callback */}
590
+ }
591
+ }
592
+ ` ) ;
593
+
594
+ runMigration ( ) ;
595
+
596
+ expect ( tree . readContent ( '/index.ts' ) )
597
+ . toContain ( `@${ queryType } ('test', { static: false }) query: any;` ) ;
598
+ expect ( tree . readContent ( '/index.ts' ) )
599
+ . toContain ( `@${ queryType } ('test', { static: true }) query2: any;` ) ;
600
+ } ) ;
601
+
602
+ it ( 'should not mark queries used in setTimeout as static' , ( ) => {
603
+ writeFile ( '/index.ts' , `
604
+ import {Component, ${ queryType } } from '@angular/core';
605
+
606
+ @Component({template: '<span #test></span>'})
607
+ export class MyComp {
608
+ private @${ queryType } ('test') query: any;
609
+ private @${ queryType } ('test') query2: any;
610
+ private @${ queryType } ('test') query3: any;
611
+
612
+ ngOnInit() {
613
+ setTimeout(function() {
614
+ this.query.doSomething();
615
+ });
616
+
617
+ setTimeout(createCallback(this));
618
+ }
619
+ }
620
+
621
+ function createCallback(instance: MyComp) {
622
+ instance.query2.doSomething();
623
+ return () => instance.query3.doSomething();
624
+ }
625
+ ` ) ;
626
+
627
+ runMigration ( ) ;
628
+
629
+ expect ( tree . readContent ( '/index.ts' ) )
630
+ . toContain ( `@${ queryType } ('test', { static: false }) query: any;` ) ;
631
+ expect ( tree . readContent ( '/index.ts' ) )
632
+ . toContain ( `@${ queryType } ('test', { static: true }) query2: any;` ) ;
633
+ expect ( tree . readContent ( '/index.ts' ) )
634
+ . toContain ( `@${ queryType } ('test', { static: false }) query3: any;` ) ;
635
+ } ) ;
636
+
637
+ it ( 'should not mark queries used in "addEventListener" as static' , ( ) => {
638
+ writeFile ( '/index.ts' , `
639
+ import {Component, ElementRef, ${ queryType } } from '@angular/core';
640
+
641
+ @Component({template: '<span #test></span>'})
642
+ export class MyComp {
643
+ private @${ queryType } ('test') query: any;
644
+
645
+ constructor(private elementRef: ElementRef) {}
646
+
647
+ ngOnInit() {
648
+ this.elementRef.addEventListener(() => {
649
+ this.query.classList.add('test');
650
+ });
651
+ }
652
+ }
653
+ ` ) ;
654
+
655
+ runMigration ( ) ;
656
+
657
+ expect ( tree . readContent ( '/index.ts' ) )
658
+ . toContain ( `@${ queryType } ('test', { static: false }) query: any;` ) ;
659
+ } ) ;
660
+
661
+ it ( 'should not mark queries used in "requestAnimationFrame" as static' , ( ) => {
662
+ writeFile ( '/index.ts' , `
663
+ import {Component, ElementRef, ${ queryType } } from '@angular/core';
664
+
665
+ @Component({template: '<span #test></span>'})
666
+ export class MyComp {
667
+ private @${ queryType } ('test') query: any;
668
+
669
+ constructor(private elementRef: ElementRef) {}
670
+
671
+ ngOnInit() {
672
+ requestAnimationFrame(() => {
673
+ this.query.classList.add('test');
674
+ });
675
+ }
676
+ }
677
+ ` ) ;
678
+
679
+ runMigration ( ) ;
680
+
681
+ expect ( tree . readContent ( '/index.ts' ) )
682
+ . toContain ( `@${ queryType } ('test', { static: false }) query: any;` ) ;
683
+ } ) ;
684
+
685
+ it ( 'should mark queries used in immediately-invoked function expression as static' , ( ) => {
686
+ writeFile ( '/index.ts' , `
687
+ import {Component, ${ queryType } } from '@angular/core';
688
+
689
+ @Component({template: '<span #test></span>'})
690
+ export class MyComp {
691
+ private @${ queryType } ('test') query: any;
692
+ private @${ queryType } ('test') query2: any;
693
+
694
+ ngOnInit() {
695
+ (() => {
696
+ this.query.usedStatically();
697
+ })();
698
+
699
+ (function(ctx) {
700
+ ctx.query2.useStatically();
701
+ })(this);
702
+ }
703
+ }
704
+ ` ) ;
705
+
706
+ runMigration ( ) ;
707
+
708
+ expect ( tree . readContent ( '/index.ts' ) )
709
+ . toContain ( `@${ queryType } ('test', { static: true }) query: any;` ) ;
710
+ expect ( tree . readContent ( '/index.ts' ) )
711
+ . toContain ( `@${ queryType } ('test', { static: true }) query2: any;` ) ;
712
+ } ) ;
523
713
}
524
714
} ) ;
0 commit comments