Skip to content

Commit

Permalink
fix(migrations): Fix empty switch case offset bug in cf migration (#5…
Browse files Browse the repository at this point in the history
…3839)

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
  • Loading branch information
thePunderWoman authored and atscott committed Jan 9, 2024
1 parent 9c58717 commit e92c86b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ export function getMainBlock(etm: ElementToMigrate, tmpl: string, offset: number
const {childStart, childEnd} = etm.getChildSpan(offset);
middle = tmpl.slice(childStart, childEnd);
} else {
middle = startMarker + endMarker;
middle = '';
}
return {start: '', middle, end: ''};
} else if (isI18nTemplate(etm, i18nAttr)) {
Expand Down
72 changes: 72 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3497,6 +3497,78 @@ describe('control flow migration', () => {

expect(content).toBe(result);
});

it('should handle empty cases safely without offset issues', 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', [
`<ng-container *ngIf="generic; else specific">`,
` <ng-container [ngSwitch]="dueWhen">`,
` <ng-container *ngSwitchCase="due.NOW">`,
` <p>Now></p>`,
` </ng-container>`,
` <ng-container *ngSwitchCase="due.SOON">`,
` <p>Soon></p>`,
` </ng-container>`,
` <ng-container *ngSwitchDefault></ng-container>`,
` </ng-container>`,
`</ng-container>`,
`<ng-template #specific>`,
` <ng-container [ngSwitch]="dueWhen">`,
` <ng-container *ngSwitchCase="due.NOW">`,
` <p>Now></p>`,
` </ng-container>`,
` <ng-container *ngSwitchCase="due.SOON">`,
` <p>Soon></p>`,
` </ng-container>`,
` <ng-container *ngSwitchDefault>`,
` <p>Default></p>`,
` </ng-container>`,
` </ng-container>`,
`</ng-template>`,
].join('\n'));

await runMigration();
const actual = tree.readContent('/comp.html');

const expected = [
`@if (generic) {`,
` @switch (dueWhen) {`,
` @case (due.NOW) {`,
` <p>Now></p>`,
` }`,
` @case (due.SOON) {`,
` <p>Soon></p>`,
` }`,
` @default {`,
` }`,
` }`,
`} @else {`,
` @switch (dueWhen) {`,
` @case (due.NOW) {`,
` <p>Now></p>`,
` }`,
` @case (due.SOON) {`,
` <p>Soon></p>`,
` }`,
` @default {`,
` <p>Default></p>`,
` }`,
` }`,
`}\n`,
].join('\n');

expect(actual).toBe(expected);
});
});

describe('error handling', () => {
Expand Down

0 comments on commit e92c86b

Please sign in to comment.