Skip to content

Commit f1a020b

Browse files
thePunderWomandylhunn
authored andcommitted
fix(migrations): fix broken migration when no control flow is present (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
1 parent 3433728 commit f1a020b

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,14 @@ export function migrateTemplate(template: string): {migrated: string|null, error
134134

135135
// start from top of template
136136
// loop through each element
137-
visitor.elements[0].hasLineBreaks = hasLineBreaks;
138-
let prevElEnd = visitor.elements[0]?.el.sourceSpan.end.offset ?? result.length - 1;
139-
let nestedQueue: number[] = [prevElEnd];
140-
for (let i = 1; i < visitor.elements.length; i++) {
137+
let nestedQueue: number[] = [];
138+
for (let i = 0; i < visitor.elements.length; i++) {
141139
let currEl = visitor.elements[i];
140+
if (i === 0) {
141+
nestedQueue.push(currEl.el.sourceSpan.end.offset);
142+
currEl.hasLineBreaks = hasLineBreaks;
143+
continue;
144+
}
142145
currEl.hasLineBreaks = hasLineBreaks;
143146
currEl.nestCount = getNestedCount(currEl, nestedQueue);
144147
if (currEl.el.sourceSpan.end.offset !== nestedQueue[nestedQueue.length - 1]) {

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,4 +2063,26 @@ describe('control flow migration', () => {
20632063
expect(content).toContain('<ng-template #myTmpl let-greeting>');
20642064
});
20652065
});
2066+
2067+
describe('no migration needed', () => {
2068+
it('should do nothing when no control flow is present', async () => {
2069+
writeFile('/comp.ts', `
2070+
import {Component} from '@angular/core';
2071+
import {NgIf} from '@angular/common';
2072+
2073+
@Component({
2074+
imports: [NgIf],
2075+
template: \`<div><span>shrug</span></div>\`
2076+
})
2077+
class Comp {
2078+
toggle = false;
2079+
}
2080+
`);
2081+
2082+
await runMigration();
2083+
const content = tree.readContent('/comp.ts');
2084+
2085+
expect(content).toContain('template: `<div><span>shrug</span></div>`');
2086+
});
2087+
});
20662088
});

0 commit comments

Comments
 (0)