Skip to content

Commit 99e7629

Browse files
cexbrayatpkozlowski-opensource
authored andcommitted
fix(core): do not remove used ng-template nodes in control flow migration (#52186)
This fixes an issue where `ng-template` nodes were removed even when used in other places than control flow directives. Template to migrate: ```html <ng-template #blockUsedElsewhere><div>Block</div></ng-template> <ng-container *ngTemplateOutlet="blockUsedElsewhere"></ng-container> ``` Before: ```html <ng-container *ngTemplateOutlet="blockUsedElsewhere"></ng-container> ``` After: ```html <ng-template #blockUsedElsewhere><div>Block</div></ng-template> <ng-container *ngTemplateOutlet="blockUsedElsewhere"></ng-container> ``` PR Close #52186
1 parent 6f19117 commit 99e7629

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

packages/core/schematics/ng-generate/control-flow-migration/util.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export function migrateTemplate(template: string): {migrated: string|null, error
175175
}
176176

177177
for (const [_, t] of visitor.templates) {
178-
if (t.count === 2) {
178+
if (t.count < 2) {
179179
result = result.replace(t.contents, '');
180180
}
181181
}
@@ -282,6 +282,9 @@ function buildIfElseBlock(
282282
offset = offset + etm.preOffset(startBlock.length) +
283283
etm.postOffset(mainBlock.length + postBlock.length);
284284

285+
// decrease usage count of elseTmpl
286+
elseTmpl.count--;
287+
285288
return {tmpl: updatedTmpl, offset};
286289
}
287290

@@ -306,6 +309,10 @@ function buildIfThenElseBlock(
306309

307310
offset = offset + etm.preOffset(startBlock.length) + etm.postOffset(postBlock.length);
308311

312+
// decrease usage count of thenTmpl and elseTmpl
313+
thenTmpl.count--;
314+
elseTmpl.count--;
315+
309316
return {tmpl: updatedTmpl, offset};
310317
}
311318

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,33 @@ describe('control flow migration', () => {
336336
`<ng-container *ngTemplateOutlet="elseBlock"></ng-container>`,
337337
].join('\n'));
338338
});
339+
340+
it('should not remove ng-templates used by other directives', async () => {
341+
writeFile('/comp.ts', `
342+
import {Component} from '@angular/core';
343+
import {NgIf} from '@angular/common';
344+
345+
@Component({
346+
templateUrl: './comp.html'
347+
})
348+
class Comp {
349+
show = false;
350+
}
351+
`);
352+
353+
writeFile('/comp.html', [
354+
`<ng-template #blockUsedElsewhere><div>Block</div></ng-template>`,
355+
`<ng-container *ngTemplateOutlet="blockUsedElsewhere"></ng-container>`,
356+
].join('\n'));
357+
358+
await runMigration();
359+
const content = tree.readContent('/comp.html');
360+
361+
expect(content).toBe([
362+
`<ng-template #blockUsedElsewhere><div>Block</div></ng-template>`,
363+
`<ng-container *ngTemplateOutlet="blockUsedElsewhere"></ng-container>`,
364+
].join('\n'));
365+
});
339366
});
340367

341368
describe('ngFor', () => {

0 commit comments

Comments
 (0)