Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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 61 packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.