mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
fix(migrations): fix broken migration when no control flow is present (#52399)
This addresses a bug that caused the control flow migration to crash when no control flow was present in the template. PR Close #52399
This commit is contained in:
committed by
Dylan Hunn
parent
3433728a82
commit
f1a020b511
@@ -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]) {
|
||||
|
||||
@@ -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>`');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user