@@ -654,6 +654,75 @@ describe('control flow migration', () => {
654654 ] . join ( '\n' ) ) ;
655655 } ) ;
656656
657+ it ( 'should migrate an if else case with no semicolon before else' , async ( ) => {
658+ writeFile ( '/comp.ts' , `
659+ import {Component} from '@angular/core';
660+ import {NgIf} from '@angular/common';
661+
662+ @Component({
663+ templateUrl: './comp.html'
664+ })
665+ class Comp {
666+ show = false;
667+ }
668+ ` ) ;
669+
670+ writeFile ( '/comp.html' , [
671+ `<div>` ,
672+ `<span *ngIf="show else elseBlock">Content here</span>` ,
673+ `<ng-template #elseBlock>Else Content</ng-template>` ,
674+ `</div>` ,
675+ ] . join ( '\n' ) ) ;
676+
677+ await runMigration ( ) ;
678+ const content = tree . readContent ( '/comp.html' ) ;
679+
680+ expect ( content ) . toBe ( [
681+ `<div>` ,
682+ ` @if (show) {` ,
683+ ` <span>Content here</span>` ,
684+ ` } @else {` ,
685+ ` Else Content` ,
686+ ` }` ,
687+ `</div>` ,
688+ ] . join ( '\n' ) ) ;
689+ } ) ;
690+
691+ it ( 'should migrate an if then else case with no semicolons' , async ( ) => {
692+ writeFile ( '/comp.ts' , `
693+ import {Component} from '@angular/core';
694+ import {NgIf} from '@angular/common';
695+
696+ @Component({
697+ templateUrl: './comp.html'
698+ })
699+ class Comp {
700+ show = false;
701+ }
702+ ` ) ;
703+
704+ writeFile ( '/comp.html' , [
705+ `<div>` ,
706+ `<span *ngIf="show then thenTmpl else elseTmpl"></span>` ,
707+ `<ng-template #thenTmpl><span>Content here</span></ng-template>` ,
708+ `<ng-template #elseTmpl>Else Content</ng-template>` ,
709+ `</div>` ,
710+ ] . join ( '\n' ) ) ;
711+
712+ await runMigration ( ) ;
713+ const content = tree . readContent ( '/comp.html' ) ;
714+
715+ expect ( content ) . toBe ( [
716+ `<div>` ,
717+ ` @if (show) {` ,
718+ ` <span>Content here</span>` ,
719+ ` } @else {` ,
720+ ` Else Content` ,
721+ ` }` ,
722+ `</div>` ,
723+ ] . join ( '\n' ) ) ;
724+ } ) ;
725+
657726 it ( 'should migrate an if else case with no space after ;' , async ( ) => {
658727 writeFile ( '/comp.ts' , `
659728 import {Component} from '@angular/core';
@@ -1703,6 +1772,87 @@ describe('control flow migration', () => {
17031772 expect ( content ) . toContain (
17041773 'template: `<ul>@for (itm of \'1,2,3\'; track itm) {<li>{{itm}}</li>}</ul>`' ) ;
17051774 } ) ;
1775+
1776+ it ( 'should migrate ngForTemplate' , async ( ) => {
1777+ writeFile ( '/comp.ts' , `
1778+ import {Component} from '@angular/core';
1779+ import {NgFor} from '@angular/common';
1780+ interface Item {
1781+ id: number;
1782+ text: string;
1783+ }
1784+
1785+ @Component({
1786+ imports: [NgFor],
1787+ templateUrl: 'comp.html',
1788+ })
1789+ class Comp {
1790+ items: Item[] = [{id: 1, text: 'blah'},{id: 2, text: 'stuff'}];
1791+ }
1792+ ` ) ;
1793+
1794+ writeFile ( '/comp.html' , [
1795+ `<ng-container *ngFor="let item of things; template: itemTemplate"></ng-container>` ,
1796+ `<ng-container *ngFor="let item of items; template: itemTemplate"></ng-container>` ,
1797+ `<ng-template #itemTemplate let-item>` ,
1798+ ` <p>Stuff</p>` ,
1799+ `</ng-template>` ,
1800+ ] . join ( '\n' ) ) ;
1801+
1802+ await runMigration ( ) ;
1803+ const actual = tree . readContent ( '/comp.html' ) ;
1804+
1805+ const expected = [
1806+ `@for (item of things; track item) {` ,
1807+ ` <ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item}"></ng-container>` ,
1808+ `}` ,
1809+ `@for (item of items; track item) {` ,
1810+ ` <ng-container *ngTemplateOutlet="itemTemplate; context: {$implicit: item}"></ng-container>` ,
1811+ `}` ,
1812+ `<ng-template #itemTemplate let-item>` ,
1813+ ` <p>Stuff</p>` ,
1814+ `</ng-template>` ,
1815+ ] . join ( '\n' ) ;
1816+
1817+ expect ( actual ) . toBe ( expected ) ;
1818+ } ) ;
1819+
1820+ it ( 'should migrate ngForTemplate when template is only used once' , async ( ) => {
1821+ writeFile ( '/comp.ts' , `
1822+ import {Component} from '@angular/core';
1823+ import {NgFor} from '@angular/common';
1824+ interface Item {
1825+ id: number;
1826+ text: string;
1827+ }
1828+
1829+ @Component({
1830+ imports: [NgFor],
1831+ templateUrl: 'comp.html',
1832+ })
1833+ class Comp {
1834+ items: Item[] = [{id: 1, text: 'blah'},{id: 2, text: 'stuff'}];
1835+ }
1836+ ` ) ;
1837+
1838+ writeFile ( '/comp.html' , [
1839+ `<ng-container *ngFor="let item of items; template: itemTemplate"></ng-container>` ,
1840+ `<ng-template #itemTemplate let-item>` ,
1841+ ` <p>Stuff</p>` ,
1842+ `</ng-template>` ,
1843+ ] . join ( '\n' ) ) ;
1844+
1845+ await runMigration ( ) ;
1846+ const actual = tree . readContent ( '/comp.html' ) ;
1847+
1848+ const expected = [
1849+ `@for (item of items; track item) {` ,
1850+ ` <p>Stuff</p>` ,
1851+ `}\n` ,
1852+ ] . join ( '\n' ) ;
1853+
1854+ expect ( actual ) . toBe ( expected ) ;
1855+ } ) ;
17061856 } ) ;
17071857
17081858 describe ( 'ngForOf' , ( ) => {
@@ -3155,6 +3305,42 @@ describe('control flow migration', () => {
31553305 expect ( content ) . toBe ( result ) ;
31563306 } ) ;
31573307
3308+ it ( 'should migrate template with ngTemplateOutlet' , async ( ) => {
3309+ writeFile ( '/comp.ts' , `
3310+ import {Component} from '@angular/core';
3311+ import {NgIf} from '@angular/common';
3312+
3313+ @Component({
3314+ selector: 'declare-comp',
3315+ templateUrl: 'comp.html',
3316+ })
3317+ class DeclareComp {}
3318+ ` ) ;
3319+
3320+ writeFile ( '/comp.html' , [
3321+ `<ng-template [ngIf]="!thing" [ngIfElse]="elseTmpl">` ,
3322+ ` {{ this.value(option) }}` ,
3323+ `</ng-template>` ,
3324+ `<ng-template` ,
3325+ ` #elseTmpl` ,
3326+ ` *ngTemplateOutlet="thing; context: {option: option}">` ,
3327+ `</ng-template>` ,
3328+ ] . join ( '\n' ) ) ;
3329+
3330+ await runMigration ( ) ;
3331+ const content = tree . readContent ( '/comp.html' ) ;
3332+
3333+ const result = [
3334+ `@if (!thing) {` ,
3335+ ` {{ this.value(option) }}` ,
3336+ `} @else {` ,
3337+ ` <ng-container *ngTemplateOutlet="thing; context: {option: option}"></ng-container>` ,
3338+ `}\n` ,
3339+ ] . join ( '\n' ) ;
3340+
3341+ expect ( content ) . toBe ( result ) ;
3342+ } ) ;
3343+
31583344 it ( 'should move templates after they were migrated to new syntax' , async ( ) => {
31593345 writeFile ( '/comp.ts' , `
31603346 import {Component} from '@angular/core';
0 commit comments