Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(migrations): Fixes the root level template offset in control flow migration #52355

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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