Skip to content

Commit e92c86b

Browse files
thePunderWomanatscott
authored andcommitted
fix(migrations): Fix empty switch case offset bug in cf migration (#53839)
This addresses the offset issue caused when a switch case was empty with no spaces or children being affected by the markers that were added, but not accounted for in offset. The markers are not needed for empty content and can be safely removed in this case. fixes: #53779 PR Close #53839
1 parent 9c58717 commit e92c86b

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ export function getMainBlock(etm: ElementToMigrate, tmpl: string, offset: number
541541
const {childStart, childEnd} = etm.getChildSpan(offset);
542542
middle = tmpl.slice(childStart, childEnd);
543543
} else {
544-
middle = startMarker + endMarker;
544+
middle = '';
545545
}
546546
return {start: '', middle, end: ''};
547547
} else if (isI18nTemplate(etm, i18nAttr)) {

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3497,6 +3497,78 @@ describe('control flow migration', () => {
34973497

34983498
expect(content).toBe(result);
34993499
});
3500+
3501+
it('should handle empty cases safely without offset issues', async () => {
3502+
writeFile('/comp.ts', `
3503+
import {Component} from '@angular/core';
3504+
import {NgIf} from '@angular/common';
3505+
3506+
@Component({
3507+
selector: 'declare-comp',
3508+
templateUrl: 'comp.html',
3509+
})
3510+
class DeclareComp {}
3511+
`);
3512+
3513+
writeFile('/comp.html', [
3514+
`<ng-container *ngIf="generic; else specific">`,
3515+
` <ng-container [ngSwitch]="dueWhen">`,
3516+
` <ng-container *ngSwitchCase="due.NOW">`,
3517+
` <p>Now></p>`,
3518+
` </ng-container>`,
3519+
` <ng-container *ngSwitchCase="due.SOON">`,
3520+
` <p>Soon></p>`,
3521+
` </ng-container>`,
3522+
` <ng-container *ngSwitchDefault></ng-container>`,
3523+
` </ng-container>`,
3524+
`</ng-container>`,
3525+
`<ng-template #specific>`,
3526+
` <ng-container [ngSwitch]="dueWhen">`,
3527+
` <ng-container *ngSwitchCase="due.NOW">`,
3528+
` <p>Now></p>`,
3529+
` </ng-container>`,
3530+
` <ng-container *ngSwitchCase="due.SOON">`,
3531+
` <p>Soon></p>`,
3532+
` </ng-container>`,
3533+
` <ng-container *ngSwitchDefault>`,
3534+
` <p>Default></p>`,
3535+
` </ng-container>`,
3536+
` </ng-container>`,
3537+
`</ng-template>`,
3538+
].join('\n'));
3539+
3540+
await runMigration();
3541+
const actual = tree.readContent('/comp.html');
3542+
3543+
const expected = [
3544+
`@if (generic) {`,
3545+
` @switch (dueWhen) {`,
3546+
` @case (due.NOW) {`,
3547+
` <p>Now></p>`,
3548+
` }`,
3549+
` @case (due.SOON) {`,
3550+
` <p>Soon></p>`,
3551+
` }`,
3552+
` @default {`,
3553+
` }`,
3554+
` }`,
3555+
`} @else {`,
3556+
` @switch (dueWhen) {`,
3557+
` @case (due.NOW) {`,
3558+
` <p>Now></p>`,
3559+
` }`,
3560+
` @case (due.SOON) {`,
3561+
` <p>Soon></p>`,
3562+
` }`,
3563+
` @default {`,
3564+
` <p>Default></p>`,
3565+
` }`,
3566+
` }`,
3567+
`}\n`,
3568+
].join('\n');
3569+
3570+
expect(actual).toBe(expected);
3571+
});
35003572
});
35013573

35023574
describe('error handling', () => {

0 commit comments

Comments
 (0)