Skip to content

Commit

Permalink
fix(migrations): fix off by one issue with template removal in CF mig…
Browse files Browse the repository at this point in the history
…ration (#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
  • Loading branch information
thePunderWoman authored and pkozlowski-opensource committed Nov 29, 2023
1 parent fadfee4 commit 6291c8d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Expand Up @@ -218,7 +218,7 @@ export class Template {
}

generateContents(tmpl: string) {
this.contents = tmpl.slice(this.el.sourceSpan.start.offset, this.el.sourceSpan.end.offset + 1);
this.contents = tmpl.slice(this.el.sourceSpan.start.offset, this.el.sourceSpan.end.offset);
this.children = '';
if (this.el.children.length > 0) {
this.children = tmpl.slice(
Expand Down
56 changes: 54 additions & 2 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Expand Up @@ -3685,7 +3685,59 @@ describe('control flow migration', () => {

expect(actual).toBe(expected);
});

it('should remove a template with no overlap with following elements', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgIf} from '@angular/common';
@Component({
templateUrl: './comp.html'
})
class Comp {
show = false;
}
`);

writeFile('/comp.html', [
`<ng-container *ngIf="stuff">`,
` <div>`,
` <ul>`,
` <li>`,
` <span>`,
` <ng-container *ngIf="things; else elseTmpl">`,
` <p>Hmm</p>`,
` </ng-container>`,
` <ng-template #elseTmpl> 0 </ng-template></span>`,
` </li>`,
` </ul>`,
` </div>`,
`</ng-container>`,
].join('\n'));

await runMigration();
const content = tree.readContent('/comp.html');

expect(content).toBe([
`@if (stuff) {`,
` <div>`,
` <ul>`,
` <li>`,
` <span>`,
` @if (things) {`,
` <p>Hmm</p>`,
` } @else {`,
` 0`,
` }`,
` </span>`,
` </li>`,
` </ul>`,
` </div>`,
`}`,
].join('\n'));
});
});

describe('formatting', () => {
it('should reformat else if', async () => {
writeFile('/comp.ts', `
Expand Down Expand Up @@ -4052,7 +4104,7 @@ describe('control flow migration', () => {
`<span>Content here</span>`,
`} @else {`,
`Else Content`,
`}`,
`}\n`,
`</div>`,
].join('\n'));
});
Expand Down Expand Up @@ -4396,7 +4448,7 @@ describe('control flow migration', () => {
await runMigration();
const content = tree.readContent('/comp.ts');
expect(content).toContain(
'template: `@if (isLoggedIn$ | async; as logIn) {\n Log In\n} @else {\n Log Out\n}`');
'template: `@if (isLoggedIn$ | async; as logIn) {\n Log In\n} @else {\n Log Out\n}\n`');
});
});
});

0 comments on commit 6291c8d

Please sign in to comment.