Skip to content

Commit

Permalink
fix(migrations): Fixes the root level template offset in control flow…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
thePunderWoman authored and dylhunn committed Oct 24, 2023
1 parent 114a2f8 commit 54fed68
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
Expand Up @@ -155,8 +155,8 @@ export function migrateTemplate(template: string): {migrated: string|null, error
let offset = 0;
let nestLevel = -1;
let postOffsets: number[] = [];
let migrateResult: Result = {tmpl: result, offsets: {pre: 0, post: 0}};
for (const el of visitor.elements) {
let migrateResult: Result = {tmpl: result, offsets: {pre: 0, post: 0}};
// applies the post offsets after closing
if (el.nestCount <= nestLevel) {
const count = nestLevel - el.nestCount;
Expand Down Expand Up @@ -189,7 +189,6 @@ export function migrateTemplate(template: string): {migrated: string|null, error
result = migrateResult.tmpl;
offset += migrateResult.offsets.pre;
postOffsets.push(migrateResult.offsets.post);
const nm = el.el.name;
nestLevel = el.nestCount;
}

Expand Down
61 changes: 60 additions & 1 deletion packages/core/schematics/test/control_flow_migration_spec.ts
Expand Up @@ -1798,7 +1798,66 @@ describe('control flow migration', () => {
});
});

describe('template removal', () => {
describe('template', () => {
it('should migrate a root level template thats not used in control flow', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgIf} from '@angular/common';
@Component({
selector: 'declare-comp',
templateUrl: './comp.html'
})
class DeclareComp {
}
`);

writeFile('/comp.html', [
`<div class="content">`,
` <ng-container *ngTemplateOutlet="navigation" />`,
` <ng-container *ngIf="content()">`,
` <div class="class-1"></div>`,
` </ng-container>`,
`</div>`,
`<ng-template #navigation>`,
` <div class="cont">`,
` <button`,
` *ngIf="shouldShowMe()"`,
` class="holy-classname-batman"`,
` >`,
` Wow...a button!`,
` </button>`,
` </div>`,
`</ng-template>`,
].join('\n'));

await runMigration();

const content = tree.readContent('/comp.html');
const result = [
`<div class="content">`,
` <ng-container *ngTemplateOutlet="navigation" />`,
` @if (content()) {\n`,
` <div class="class-1"></div>\n `,
`}`,
`</div>`,
`<ng-template #navigation>`,
` <div class="cont">`,
` @if (shouldShowMe()) {`,
`<button\n `,
` class="holy-classname-batman"`,
` >`,
` Wow...a button!`,
` </button>`,
`}`,
` </div>`,
`</ng-template>`,

].join('\n');

expect(content).toBe(result);
});

it('should not remove a template thats not used in control flow', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
Expand Down

0 comments on commit 54fed68

Please sign in to comment.