@@ -12,6 +12,12 @@ import {ElementCollector, ElementToMigrate, MigrateError, Result} from './types'
1212import { calculateNesting , getMainBlock , getOriginals , hasLineBreaks , parseTemplate , reduceNestingOffset } from './util' ;
1313
1414export const ngfor = '*ngFor' ;
15+ export const nakedngfor = 'ngFor' ;
16+ const fors = [
17+ ngfor ,
18+ nakedngfor ,
19+ ] ;
20+
1521export const commaSeparatedSyntax = new Map ( [
1622 [ '(' , ')' ] ,
1723 [ '{' , '}' ] ,
@@ -34,7 +40,7 @@ export function migrateFor(template: string): {migrated: string, errors: Migrate
3440 }
3541
3642 let result = template ;
37- const visitor = new ElementCollector ( [ ngfor ] ) ;
43+ const visitor = new ElementCollector ( fors ) ;
3844 visitAll ( visitor , parsed . rootNodes ) ;
3945 calculateNesting ( visitor , hasLineBreaks ( template ) ) ;
4046
@@ -65,8 +71,14 @@ export function migrateFor(template: string): {migrated: string, errors: Migrate
6571 return { migrated : result , errors} ;
6672}
6773
68-
6974function migrateNgFor ( etm : ElementToMigrate , tmpl : string , offset : number ) : Result {
75+ if ( etm . forAttrs !== undefined ) {
76+ return migrateBoundNgFor ( etm , tmpl , offset ) ;
77+ }
78+ return migrateStandardNgFor ( etm , tmpl , offset ) ;
79+ }
80+
81+ function migrateStandardNgFor ( etm : ElementToMigrate , tmpl : string , offset : number ) : Result {
7082 const aliasWithEqualRegexp = / = \s * ( c o u n t | i n d e x | f i r s t | l a s t | e v e n | o d d ) / gm;
7183 const aliasWithAsRegexp = / ( c o u n t | i n d e x | f i r s t | l a s t | e v e n | o d d ) \s + a s / gm;
7284 const aliases = [ ] ;
@@ -136,6 +148,43 @@ function migrateNgFor(etm: ElementToMigrate, tmpl: string, offset: number): Resu
136148 return { tmpl : updatedTmpl , offsets : { pre, post} } ;
137149}
138150
151+ function migrateBoundNgFor ( etm : ElementToMigrate , tmpl : string , offset : number ) : Result {
152+ const forAttrs = etm . forAttrs ! ;
153+ const aliasMap = forAttrs . aliases ;
154+
155+ const originals = getOriginals ( etm , tmpl , offset ) ;
156+ const condition = `${ forAttrs . item } of ${ forAttrs . forOf } ` ;
157+
158+ const aliases = [ ] ;
159+ let aliasedIndex = '$index' ;
160+ for ( const [ key , val ] of aliasMap ) {
161+ aliases . push ( ` let ${ key . trim ( ) } = $${ val } ` ) ;
162+ if ( val . trim ( ) === 'index' ) {
163+ aliasedIndex = key ;
164+ }
165+ }
166+ const aliasStr = ( aliases . length > 0 ) ? `;${ aliases . join ( ';' ) } ` : '' ;
167+
168+ let trackBy = forAttrs . item ;
169+ if ( forAttrs . trackBy !== '' ) {
170+ // build trackby value
171+ trackBy = `${ forAttrs . trackBy . trim ( ) } (${ aliasedIndex } , ${ forAttrs . item } )` ;
172+ }
173+
174+ const { start, middle, end} = getMainBlock ( etm , tmpl , offset ) ;
175+ const startBlock = `@for (${ condition } ; track ${ trackBy } ${ aliasStr } ) {\n ${ start } ` ;
176+
177+ const endBlock = `${ end } \n}` ;
178+ const forBlock = startBlock + middle + endBlock ;
179+
180+ const updatedTmpl = tmpl . slice ( 0 , etm . start ( offset ) ) + forBlock + tmpl . slice ( etm . end ( offset ) ) ;
181+
182+ const pre = originals . start . length - startBlock . length ;
183+ const post = originals . end . length - endBlock . length ;
184+
185+ return { tmpl : updatedTmpl , offsets : { pre, post} } ;
186+ }
187+
139188function getNgForParts ( expression : string ) : string [ ] {
140189 const parts : string [ ] = [ ] ;
141190 const commaSeparatedStack : string [ ] = [ ] ;
0 commit comments