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): fix off by one issue with template removal in CF migration #53255

Closed
wants to merge 1 commit into from
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 @@ -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`');
});
});
});