Skip to content

Commit a359951

Browse files
fix(migrations): properly handle ngIfThen cases in CF migration (#53256)
The migration was handling bound casees of [ngIfThenElse] and also needed [ngIfThen]. fixes: #53254 PR Close #53256
1 parent 6291c8d commit a359951

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import ts from 'typescript';
1212
export const ngtemplate = 'ng-template';
1313
export const boundngifelse = '[ngIfElse]';
1414
export const boundngifthenelse = '[ngIfThenElse]';
15+
export const boundngifthen = '[ngIfThen]';
1516
export const nakedngfor = 'ngFor';
1617

1718
function allFormsOf(selector: string): string[] {
@@ -315,7 +316,8 @@ export class ElementCollector extends RecursiveVisitor {
315316
for (const attr of el.attrs) {
316317
if (this._attributes.includes(attr.name)) {
317318
const elseAttr = el.attrs.find(x => x.name === boundngifelse);
318-
const thenAttr = el.attrs.find(x => x.name === boundngifthenelse);
319+
const thenAttr =
320+
el.attrs.find(x => x.name === boundngifthenelse || x.name === boundngifthen);
319321
const forAttrs = attr.name === nakedngfor ? this.getForAttrs(el) : undefined;
320322
this.elements.push(new ElementToMigrate(el, attr, elseAttr, thenAttr, forAttrs));
321323
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export function getTemplates(template: string): Map<string, Template> {
290290
// count usages of each ng-template
291291
for (let [key, tmpl] of visitor.templates) {
292292
const escapeKey = escapeRegExp(key.slice(1));
293-
const regex = new RegExp(`[^a-zA-Z0-9-<]${escapeKey}\\W`, 'gm');
293+
const regex = new RegExp(`[^a-zA-Z0-9-<\']${escapeKey}\\W`, 'gm');
294294
const matches = template.match(regex);
295295
tmpl.count = matches?.length ?? 0;
296296
tmpl.generateContents(template);

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3559,6 +3559,46 @@ describe('control flow migration', () => {
35593559
expect(content).toBe(result);
35603560
});
35613561

3562+
it('should migrate template with ngIfThen and remove template', async () => {
3563+
writeFile('/comp.ts', `
3564+
import {Component} from '@angular/core';
3565+
import {NgIf} from '@angular/common';
3566+
3567+
@Component({
3568+
selector: 'declare-comp',
3569+
templateUrl: 'comp.html',
3570+
})
3571+
class DeclareComp {}
3572+
`);
3573+
3574+
writeFile('/comp.html', [
3575+
`<ng-template`,
3576+
` [ngIf]="mode === 'foo'"`,
3577+
` [ngIfThen]="foo"`,
3578+
` [ngIfElse]="bar"`,
3579+
`></ng-template>`,
3580+
`<ng-template #foo>`,
3581+
` Foo`,
3582+
`</ng-template>`,
3583+
`<ng-template #bar>`,
3584+
` Bar`,
3585+
`</ng-template>`,
3586+
].join('\n'));
3587+
3588+
await runMigration();
3589+
const content = tree.readContent('/comp.html');
3590+
3591+
const result = [
3592+
`@if (mode === 'foo') {`,
3593+
` Foo`,
3594+
`} @else {`,
3595+
` Bar`,
3596+
`}\n`,
3597+
].join('\n');
3598+
3599+
expect(content).toBe(result);
3600+
});
3601+
35623602
it('should move templates after they were migrated to new syntax', async () => {
35633603
writeFile('/comp.ts', `
35643604
import {Component} from '@angular/core';

0 commit comments

Comments
 (0)