Skip to content

Commit 6291c8d

Browse files
fix(migrations): fix off by one issue with template removal in CF migration (#53255)
When ng-templates are removed, an extra space was being added when it was unnecessary. This resulted in malformed html if there was no space afterwards. fixes: #53248 PR Close #53255
1 parent fadfee4 commit 6291c8d

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export class Template {
218218
}
219219

220220
generateContents(tmpl: string) {
221-
this.contents = tmpl.slice(this.el.sourceSpan.start.offset, this.el.sourceSpan.end.offset + 1);
221+
this.contents = tmpl.slice(this.el.sourceSpan.start.offset, this.el.sourceSpan.end.offset);
222222
this.children = '';
223223
if (this.el.children.length > 0) {
224224
this.children = tmpl.slice(

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3685,7 +3685,59 @@ describe('control flow migration', () => {
36853685

36863686
expect(actual).toBe(expected);
36873687
});
3688+
3689+
it('should remove a template with no overlap with following elements', async () => {
3690+
writeFile('/comp.ts', `
3691+
import {Component} from '@angular/core';
3692+
import {NgIf} from '@angular/common';
3693+
3694+
@Component({
3695+
templateUrl: './comp.html'
3696+
})
3697+
class Comp {
3698+
show = false;
3699+
}
3700+
`);
3701+
3702+
writeFile('/comp.html', [
3703+
`<ng-container *ngIf="stuff">`,
3704+
` <div>`,
3705+
` <ul>`,
3706+
` <li>`,
3707+
` <span>`,
3708+
` <ng-container *ngIf="things; else elseTmpl">`,
3709+
` <p>Hmm</p>`,
3710+
` </ng-container>`,
3711+
` <ng-template #elseTmpl> 0 </ng-template></span>`,
3712+
` </li>`,
3713+
` </ul>`,
3714+
` </div>`,
3715+
`</ng-container>`,
3716+
].join('\n'));
3717+
3718+
await runMigration();
3719+
const content = tree.readContent('/comp.html');
3720+
3721+
expect(content).toBe([
3722+
`@if (stuff) {`,
3723+
` <div>`,
3724+
` <ul>`,
3725+
` <li>`,
3726+
` <span>`,
3727+
` @if (things) {`,
3728+
` <p>Hmm</p>`,
3729+
` } @else {`,
3730+
` 0`,
3731+
` }`,
3732+
` </span>`,
3733+
` </li>`,
3734+
` </ul>`,
3735+
` </div>`,
3736+
`}`,
3737+
].join('\n'));
3738+
});
36883739
});
3740+
36893741
describe('formatting', () => {
36903742
it('should reformat else if', async () => {
36913743
writeFile('/comp.ts', `
@@ -4052,7 +4104,7 @@ describe('control flow migration', () => {
40524104
`<span>Content here</span>`,
40534105
`} @else {`,
40544106
`Else Content`,
4055-
`}`,
4107+
`}\n`,
40564108
`</div>`,
40574109
].join('\n'));
40584110
});
@@ -4396,7 +4448,7 @@ describe('control flow migration', () => {
43964448
await runMigration();
43974449
const content = tree.readContent('/comp.ts');
43984450
expect(content).toContain(
4399-
'template: `@if (isLoggedIn$ | async; as logIn) {\n Log In\n} @else {\n Log Out\n}`');
4451+
'template: `@if (isLoggedIn$ | async; as logIn) {\n Log In\n} @else {\n Log Out\n}\n`');
44004452
});
44014453
});
44024454
});

0 commit comments

Comments
 (0)