@@ -10,13 +10,16 @@ import {Attribute, HtmlParser, ParseTreeResult, visitAll} from '@angular/compile
1010import { dirname , join } from 'path' ;
1111import ts from 'typescript' ;
1212
13- import { AnalyzedFile , CommonCollector , ElementCollector , ElementToMigrate , Template , TemplateCollector } from './types' ;
13+ import { AnalyzedFile , CommonCollector , ElementCollector , ElementToMigrate , endMarker , startMarker , Template , TemplateCollector } from './types' ;
1414
1515const importRemovals = [
1616 'NgIf' , 'NgIfElse' , 'NgIfThenElse' , 'NgFor' , 'NgForOf' , 'NgForTrackBy' , 'NgSwitch' ,
1717 'NgSwitchCase' , 'NgSwitchDefault'
1818] ;
1919const importWithCommonRemovals = [ ...importRemovals , 'CommonModule' ] ;
20+ const startMarkerRegex = new RegExp ( startMarker , 'gm' ) ;
21+ const endMarkerRegex = new RegExp ( endMarker , 'gm' ) ;
22+ const replaceMarkerRegex = new RegExp ( `${ startMarker } |${ endMarker } ` , 'gm' ) ;
2023
2124/**
2225 * Analyzes a source file to find file that need to be migrated and the text ranges within them.
@@ -348,7 +351,7 @@ export function processNgTemplates(template: string): {migrated: string, err: Er
348351 }
349352 // the +1 accounts for the t.count's counting of the original template
350353 if ( t . count === matches . length + 1 && safeToRemove ) {
351- template = template . replace ( t . contents , '' ) ;
354+ template = template . replace ( t . contents , ` ${ startMarker } ${ endMarker } ` ) ;
352355 }
353356 // templates may have changed structure from nested replaced templates
354357 // so we need to reprocess them before the next loop.
@@ -474,6 +477,8 @@ export function getMainBlock(etm: ElementToMigrate, tmpl: string, offset: number
474477 if ( etm . hasChildren ( ) ) {
475478 const { childStart, childEnd} = etm . getChildSpan ( offset ) ;
476479 middle = tmpl . slice ( childStart , childEnd ) ;
480+ } else {
481+ middle = startMarker + endMarker ;
477482 }
478483 return { start : '' , middle, end : '' } ;
479484 } else if ( isI18nTemplate ( etm , i18nAttr ) ) {
@@ -512,6 +517,13 @@ export function getMainBlock(etm: ElementToMigrate, tmpl: string, offset: number
512517
513518const selfClosingList = 'input|br|img|base|wbr|area|col|embed|hr|link|meta|param|source|track' ;
514519
520+ function isInMigratedBlock ( line : string , isCurrentlyInMigratedBlock : boolean ) {
521+ return line . match ( startMarkerRegex ) &&
522+ ( ! line . match ( endMarkerRegex ) ||
523+ line . lastIndexOf ( startMarker ) > line . lastIndexOf ( endMarker ) ) ||
524+ ( isCurrentlyInMigratedBlock && ! line . match ( endMarkerRegex ) ) ;
525+ }
526+
515527/**
516528 * re-indents all the lines in the template properly post migration
517529 */
@@ -561,8 +573,19 @@ export function formatTemplate(tmpl: string, templateType: string): string {
561573 let indent = '' ;
562574 // the pre-existing indent in an inline template that we'd like to preserve
563575 let mindent = '' ;
576+ let depth = 0 ;
577+ let inMigratedBlock = false ;
564578 for ( let [ index , line ] of lines . entries ( ) ) {
565- if ( line . trim ( ) === '' && index !== 0 && index !== lines . length - 1 ) {
579+ depth +=
580+ [ ...line . matchAll ( startMarkerRegex ) ] . length - [ ...line . matchAll ( endMarkerRegex ) ] . length ;
581+ inMigratedBlock = depth > 0 ;
582+ let lineWasMigrated = false ;
583+ if ( line . match ( replaceMarkerRegex ) ) {
584+ line = line . replace ( replaceMarkerRegex , '' ) ;
585+ lineWasMigrated = true ;
586+ }
587+ if ( ( line . trim ( ) === '' && index !== 0 && index !== lines . length - 1 ) &&
588+ ( inMigratedBlock || lineWasMigrated ) ) {
566589 // skip blank lines except if it's the first line or last line
567590 // this preserves leading and trailing spaces if they are already present
568591 continue ;
@@ -584,7 +607,7 @@ export function formatTemplate(tmpl: string, templateType: string): string {
584607 indent = indent . slice ( 2 ) ;
585608 }
586609
587- formatted . push ( mindent + indent + line . trim ( ) ) ;
610+ formatted . push ( mindent + ( line . trim ( ) !== '' ? indent : '' ) + line . trim ( ) ) ;
588611
589612 // this matches any self closing element that actually has a />
590613 if ( closeMultiLineElRegex . test ( line ) ) {
0 commit comments