Skip to content

Commit 90eb879

Browse files
thePunderWomandylhunn
authored andcommitted
fix(migrations): Fixes the root level template offset in control flow migration (#52355)
When migrating an ng-template later on in a file, the migrationResult was not being reset to zero and causing offsets to be double applied due to ng-template nodes being included in the migration loop. PR Close #52355
1 parent d82d586 commit 90eb879

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ export function migrateTemplate(template: string): {migrated: string|null, error
155155
let offset = 0;
156156
let nestLevel = -1;
157157
let postOffsets: number[] = [];
158-
let migrateResult: Result = {tmpl: result, offsets: {pre: 0, post: 0}};
159158
for (const el of visitor.elements) {
159+
let migrateResult: Result = {tmpl: result, offsets: {pre: 0, post: 0}};
160160
// applies the post offsets after closing
161161
if (el.nestCount <= nestLevel) {
162162
const count = nestLevel - el.nestCount;
@@ -189,7 +189,6 @@ export function migrateTemplate(template: string): {migrated: string|null, error
189189
result = migrateResult.tmpl;
190190
offset += migrateResult.offsets.pre;
191191
postOffsets.push(migrateResult.offsets.post);
192-
const nm = el.el.name;
193192
nestLevel = el.nestCount;
194193
}
195194

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,66 @@ describe('control flow migration', () => {
17981798
});
17991799
});
18001800

1801-
describe('template removal', () => {
1801+
describe('template', () => {
1802+
it('should migrate a root level template thats not used in control flow', async () => {
1803+
writeFile('/comp.ts', `
1804+
import {Component} from '@angular/core';
1805+
import {NgIf} from '@angular/common';
1806+
1807+
@Component({
1808+
selector: 'declare-comp',
1809+
templateUrl: './comp.html'
1810+
})
1811+
class DeclareComp {
1812+
}
1813+
`);
1814+
1815+
writeFile('/comp.html', [
1816+
`<div class="content">`,
1817+
` <ng-container *ngTemplateOutlet="navigation" />`,
1818+
` <ng-container *ngIf="content()">`,
1819+
` <div class="class-1"></div>`,
1820+
` </ng-container>`,
1821+
`</div>`,
1822+
`<ng-template #navigation>`,
1823+
` <div class="cont">`,
1824+
` <button`,
1825+
` *ngIf="shouldShowMe()"`,
1826+
` class="holy-classname-batman"`,
1827+
` >`,
1828+
` Wow...a button!`,
1829+
` </button>`,
1830+
` </div>`,
1831+
`</ng-template>`,
1832+
].join('\n'));
1833+
1834+
await runMigration();
1835+
1836+
const content = tree.readContent('/comp.html');
1837+
const result = [
1838+
`<div class="content">`,
1839+
` <ng-container *ngTemplateOutlet="navigation" />`,
1840+
` @if (content()) {\n`,
1841+
` <div class="class-1"></div>\n `,
1842+
`}`,
1843+
`</div>`,
1844+
`<ng-template #navigation>`,
1845+
` <div class="cont">`,
1846+
` @if (shouldShowMe()) {`,
1847+
`<button\n `,
1848+
` class="holy-classname-batman"`,
1849+
` >`,
1850+
` Wow...a button!`,
1851+
` </button>`,
1852+
`}`,
1853+
` </div>`,
1854+
`</ng-template>`,
1855+
1856+
].join('\n');
1857+
1858+
expect(content).toBe(result);
1859+
});
1860+
18021861
it('should not remove a template thats not used in control flow', async () => {
18031862
writeFile('/comp.ts', `
18041863
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)