Skip to content

Commit

Permalink
fix(migrations): fix broken migration when no control flow is present (
Browse files Browse the repository at this point in the history
…angular#52399)

This addresses a bug that caused the control flow migration to crash when no control flow was present in the template.

PR Close angular#52399
  • Loading branch information
thePunderWoman authored and tbondwilkinson committed Dec 6, 2023
1 parent 4e0b7f5 commit df6ecf6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Expand Up @@ -134,11 +134,14 @@ export function migrateTemplate(template: string): {migrated: string|null, error

// start from top of template
// loop through each element
visitor.elements[0].hasLineBreaks = hasLineBreaks;
let prevElEnd = visitor.elements[0]?.el.sourceSpan.end.offset ?? result.length - 1;
let nestedQueue: number[] = [prevElEnd];
for (let i = 1; i < visitor.elements.length; i++) {
let nestedQueue: number[] = [];
for (let i = 0; i < visitor.elements.length; i++) {
let currEl = visitor.elements[i];
if (i === 0) {
nestedQueue.push(currEl.el.sourceSpan.end.offset);
currEl.hasLineBreaks = hasLineBreaks;
continue;
}
currEl.hasLineBreaks = hasLineBreaks;
currEl.nestCount = getNestedCount(currEl, nestedQueue);
if (currEl.el.sourceSpan.end.offset !== nestedQueue[nestedQueue.length - 1]) {
Expand Down
22 changes: 22 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Expand Up @@ -2063,4 +2063,26 @@ describe('control flow migration', () => {
expect(content).toContain('<ng-template #myTmpl let-greeting>');
});
});

describe('no migration needed', () => {
it('should do nothing when no control flow is present', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgIf} from '@angular/common';
@Component({
imports: [NgIf],
template: \`<div><span>shrug</span></div>\`
})
class Comp {
toggle = false;
}
`);

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

expect(content).toContain('template: `<div><span>shrug</span></div>`');
});
});
});

0 comments on commit df6ecf6

Please sign in to comment.