Skip to content

Commit 3831942

Browse files
committedNov 13, 2023
fix(migrations): Fixes issue with multiple if elses with same template (#52863)
This should fix the issue where if the same ng-template is used with multiple if / else statements, it replaces all usages properly. fixes: #52854 PR Close #52863
1 parent 30757bd commit 3831942

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed
 

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -283,16 +283,18 @@ export function processNgTemplates(template: string): string {
283283

284284
// swap placeholders and remove
285285
for (const [name, t] of templates) {
286-
const placeholder = `${name}|`;
287-
288-
if (template.indexOf(placeholder) > -1) {
286+
const replaceRegex = new RegExp(`${name}\\|`, 'g');
287+
const matches = [...template.matchAll(replaceRegex)];
288+
if (matches.length > 0) {
289289
if (t.i18n !== null) {
290290
const container = wrapIntoI18nContainer(t.i18n, t.children);
291-
template = template.replace(placeholder, container);
291+
template = template.replace(replaceRegex, container);
292292
} else {
293-
template = template.replace(placeholder, t.children);
293+
template = template.replace(replaceRegex, t.children);
294294
}
295-
if (t.count <= 2) {
295+
296+
// the +1 accounts for the t.count's counting of the original template
297+
if (t.count === matches.length + 1) {
296298
template = template.replace(t.contents, '');
297299
}
298300
}

‎packages/core/schematics/test/control_flow_migration_spec.ts

+43
Original file line numberDiff line numberDiff line change
@@ -2730,6 +2730,49 @@ describe('control flow migration', () => {
27302730

27312731
expect(actual).toBe(expected);
27322732
});
2733+
2734+
it('should migrate multiple if/else using the same ng-template', async () => {
2735+
writeFile('/comp.ts', `
2736+
import {Component} from '@angular/core';
2737+
import {NgIf} from '@angular/common';
2738+
2739+
@Component({
2740+
selector: 'declare-comp',
2741+
templateUrl: 'comp.html',
2742+
})
2743+
class DeclareComp {}
2744+
`);
2745+
2746+
writeFile('/comp.html', [
2747+
`<div>`,
2748+
` <div *ngIf="hasPermission; else noPermission">presentation</div>`,
2749+
` <div *ngIf="someOtherPermission; else noPermission">presentation</div>`,
2750+
`</div>`,
2751+
`<ng-template #noPermission>`,
2752+
` <p>No Permissions</p>`,
2753+
`</ng-template>`,
2754+
].join('\n'));
2755+
2756+
await runMigration();
2757+
const actual = tree.readContent('/comp.html');
2758+
2759+
const expected = [
2760+
`<div>`,
2761+
` @if (hasPermission) {`,
2762+
`<div>presentation</div>`,
2763+
`} @else {\n`,
2764+
` <p>No Permissions</p>\n`,
2765+
`}`,
2766+
` @if (someOtherPermission) {`,
2767+
`<div>presentation</div>`,
2768+
`} @else {\n`,
2769+
` <p>No Permissions</p>\n`,
2770+
`}`,
2771+
`</div>\n`,
2772+
].join('\n');
2773+
2774+
expect(actual).toBe(expected);
2775+
});
27332776
});
27342777

27352778
describe('imports', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.