@@ -74,8 +74,12 @@ function migrateNgIf(etm: ElementToMigrate, tmpl: string, offset: number): Resul
7474 if ( etm . thenAttr !== undefined || etm . elseAttr !== undefined ) {
7575 // bound if then / if then else
7676 return buildBoundIfElseBlock ( etm , tmpl , offset ) ;
77- } else if ( matchThen && matchThen . length > 0 ) {
77+ } else if ( matchThen && matchThen . length > 0 && matchElse && matchElse . length > 0 ) {
78+ // then else
7879 return buildStandardIfThenElseBlock ( etm , tmpl , matchThen [ 0 ] , matchElse ! [ 0 ] , offset ) ;
80+ } else if ( matchThen && matchThen . length > 0 ) {
81+ // just then
82+ return buildStandardIfThenBlock ( etm , tmpl , matchThen [ 0 ] , offset ) ;
7983 } else if ( ( matchElse && matchElse . length > 0 ) ) {
8084 // just else
8185 return buildStandardIfElseBlock ( etm , tmpl , matchElse ! [ 0 ] , offset ) ;
@@ -189,6 +193,17 @@ function buildStandardIfThenElseBlock(
189193 return buildIfThenElseBlock ( etm , tmpl , condition , thenPlaceholder , elsePlaceholder , offset ) ;
190194}
191195
196+ function buildStandardIfThenBlock (
197+ etm : ElementToMigrate , tmpl : string , thenString : string , offset : number ) : Result {
198+ // includes the mandatory semicolon before as
199+ const condition = etm . getCondition ( )
200+ . replace ( ' as ' , '; as ' )
201+ // replace 'let' with 'as' whatever spaces are between ; and 'let'
202+ . replace ( / ; \s * l e t / g, '; as' ) ;
203+ const thenPlaceholder = `#${ etm . getTemplateName ( thenString ) } |` ;
204+ return buildIfThenBlock ( etm , tmpl , condition , thenPlaceholder , offset ) ;
205+ }
206+
192207function buildIfThenElseBlock (
193208 etm : ElementToMigrate , tmpl : string , condition : string , thenPlaceholder : string ,
194209 elsePlaceholder : string , offset : number ) : Result {
@@ -214,3 +229,28 @@ function buildIfThenElseBlock(
214229
215230 return { tmpl : updatedTmpl , offsets : { pre, post} } ;
216231}
232+
233+ function buildIfThenBlock (
234+ etm : ElementToMigrate , tmpl : string , condition : string , thenPlaceholder : string ,
235+ offset : number ) : Result {
236+ const lbString = etm . hasLineBreaks ? '\n' : '' ;
237+
238+ const originals = getOriginals ( etm , tmpl , offset ) ;
239+
240+ const startBlock = `@if (${ condition } ) {${ lbString } ` ;
241+
242+ const postBlock = thenPlaceholder + `${ lbString } }` ;
243+ const ifThenBlock = startBlock + postBlock ;
244+
245+ const tmplStart = tmpl . slice ( 0 , etm . start ( offset ) ) ;
246+ const tmplEnd = tmpl . slice ( etm . end ( offset ) ) ;
247+
248+ const updatedTmpl = tmplStart + ifThenBlock + tmplEnd ;
249+
250+ // We ignore the contents of the element on if then else.
251+ // If there's anything there, we need to account for the length in the offset.
252+ const pre = originals . start . length + originals . childLength - startBlock . length ;
253+ const post = originals . end . length - postBlock . length ;
254+
255+ return { tmpl : updatedTmpl , offsets : { pre, post} } ;
256+ }
0 commit comments