@@ -309,40 +309,44 @@ function wrapIntoI18nContainer(i18nAttr: Attribute, content: string) {
309309/**
310310 * Counts, replaces, and removes any necessary ng-templates post control flow migration
311311 */
312- export function processNgTemplates ( template : string ) : string {
312+ export function processNgTemplates ( template : string ) : { migrated : string , err : Error | undefined } {
313313 // count usage
314- const templates = getTemplates ( template ) ;
315-
316- // swap placeholders and remove
317- for ( const [ name , t ] of templates ) {
318- const replaceRegex = new RegExp ( `${ name } \\|` , 'g' ) ;
319- const forRegex = new RegExp ( `${ name } \\#` , 'g' ) ;
320- const forMatches = [ ...template . matchAll ( forRegex ) ] ;
321- const matches = [ ...forMatches , ...template . matchAll ( replaceRegex ) ] ;
322- let safeToRemove = true ;
323- if ( matches . length > 0 ) {
324- if ( t . i18n !== null ) {
325- const container = wrapIntoI18nContainer ( t . i18n , t . children ) ;
326- template = template . replace ( replaceRegex , container ) ;
327- } else if ( t . children . trim ( ) === '' && t . isNgTemplateOutlet ) {
328- template = template . replace ( replaceRegex , t . generateTemplateOutlet ( ) ) ;
329- } else if ( forMatches . length > 0 ) {
330- if ( t . count === 2 ) {
331- template = template . replace ( forRegex , t . children ) ;
314+ try {
315+ const templates = getTemplates ( template ) ;
316+
317+ // swap placeholders and remove
318+ for ( const [ name , t ] of templates ) {
319+ const replaceRegex = new RegExp ( `${ name } \\|` , 'g' ) ;
320+ const forRegex = new RegExp ( `${ name } \\#` , 'g' ) ;
321+ const forMatches = [ ...template . matchAll ( forRegex ) ] ;
322+ const matches = [ ...forMatches , ...template . matchAll ( replaceRegex ) ] ;
323+ let safeToRemove = true ;
324+ if ( matches . length > 0 ) {
325+ if ( t . i18n !== null ) {
326+ const container = wrapIntoI18nContainer ( t . i18n , t . children ) ;
327+ template = template . replace ( replaceRegex , container ) ;
328+ } else if ( t . children . trim ( ) === '' && t . isNgTemplateOutlet ) {
329+ template = template . replace ( replaceRegex , t . generateTemplateOutlet ( ) ) ;
330+ } else if ( forMatches . length > 0 ) {
331+ if ( t . count === 2 ) {
332+ template = template . replace ( forRegex , t . children ) ;
333+ } else {
334+ template = template . replace ( forRegex , t . generateTemplateOutlet ( ) ) ;
335+ safeToRemove = false ;
336+ }
332337 } else {
333- template = template . replace ( forRegex , t . generateTemplateOutlet ( ) ) ;
334- safeToRemove = false ;
338+ template = template . replace ( replaceRegex , t . children ) ;
339+ }
340+ // the +1 accounts for the t.count's counting of the original template
341+ if ( t . count === matches . length + 1 && safeToRemove ) {
342+ template = template . replace ( t . contents , '' ) ;
335343 }
336- } else {
337- template = template . replace ( replaceRegex , t . children ) ;
338- }
339- // the +1 accounts for the t.count's counting of the original template
340- if ( t . count === matches . length + 1 && safeToRemove ) {
341- template = template . replace ( t . contents , '' ) ;
342344 }
343345 }
346+ return { migrated : template , err : undefined } ;
347+ } catch ( err ) {
348+ return { migrated : template , err : err as Error } ;
344349 }
345- return template ;
346350}
347351
348352/**
0 commit comments