Skip to content

Commit ba85d08

Browse files
naaajiipkozlowski-opensource
authored andcommitted
fix(migrations): handle empty ngSwitchCase (#56105)
empty ngSwitchCase generate `case ()` which isn't valid syntax therefore adding quotes will help prevent us migrate empty case if no condition was provided fix #56030 PR Close #56105
1 parent 4fdc93b commit ba85d08

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ function migrateNgSwitchCase(etm: ElementToMigrate, tmpl: string, offset: number
9494
// includes the mandatory semicolon before as
9595
const lbString = etm.hasLineBreaks ? '\n' : '';
9696
const leadingSpace = etm.hasLineBreaks ? '' : ' ';
97-
const condition = etm.attr.value;
97+
// ngSwitchCases with no values results into `case ()` which isn't valid, based off empty
98+
// value we add quotes instead of generating empty case
99+
const condition = etm.attr.value.length === 0 ? `''` : etm.attr.value;
98100

99101
const originals = getOriginals(etm, tmpl, offset);
100102

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,32 @@ describe('control flow migration', () => {
187187
);
188188
});
189189

190+
it('should migrate an empty case', async () => {
191+
writeFile(
192+
'/comp.ts',
193+
`
194+
import {Component} from '@angular/core';
195+
import {ngSwitch, ngSwitchCase} from '@angular/common';
196+
@Component({
197+
template: \`<div [ngSwitch]="testOpts">` +
198+
`<p *ngSwitchCase="">Option 1</p>` +
199+
`<p *ngSwitchCase="2">Option 2</p>` +
200+
`</div>\`
201+
})
202+
class Comp {
203+
testOpts = "1";
204+
}
205+
`,
206+
);
207+
208+
await runMigration();
209+
const content = tree.readContent('/comp.ts');
210+
211+
expect(content).toContain(
212+
`template: \`<div>@switch (testOpts) { @case ('') { <p>Option 1</p> } @case (2) { <p>Option 2</p> }}</div>`,
213+
);
214+
});
215+
190216
it('should migrate multiple inline templates in the same file', async () => {
191217
writeFile(
192218
'/comp.ts',

0 commit comments

Comments
 (0)