Skip to content

Commit

Permalink
fix(migrations): properly handle ngIfThen cases in CF migration (#53256)
Browse files Browse the repository at this point in the history
The migration was handling bound casees of [ngIfThenElse] and also needed [ngIfThen].

fixes: #53254

PR Close #53256
  • Loading branch information
thePunderWoman authored and pkozlowski-opensource committed Nov 29, 2023
1 parent c22af72 commit 6ae4088
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ts from 'typescript';
export const ngtemplate = 'ng-template';
export const boundngifelse = '[ngIfElse]';
export const boundngifthenelse = '[ngIfThenElse]';
export const boundngifthen = '[ngIfThen]';
export const nakedngfor = 'ngFor';

function allFormsOf(selector: string): string[] {
Expand Down Expand Up @@ -315,7 +316,8 @@ export class ElementCollector extends RecursiveVisitor {
for (const attr of el.attrs) {
if (this._attributes.includes(attr.name)) {
const elseAttr = el.attrs.find(x => x.name === boundngifelse);
const thenAttr = el.attrs.find(x => x.name === boundngifthenelse);
const thenAttr =
el.attrs.find(x => x.name === boundngifthenelse || x.name === boundngifthen);
const forAttrs = attr.name === nakedngfor ? this.getForAttrs(el) : undefined;
this.elements.push(new ElementToMigrate(el, attr, elseAttr, thenAttr, forAttrs));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export function getTemplates(template: string): Map<string, Template> {
// count usages of each ng-template
for (let [key, tmpl] of visitor.templates) {
const escapeKey = escapeRegExp(key.slice(1));
const regex = new RegExp(`[^a-zA-Z0-9-<]${escapeKey}\\W`, 'gm');
const regex = new RegExp(`[^a-zA-Z0-9-<\']${escapeKey}\\W`, 'gm');
const matches = template.match(regex);
tmpl.count = matches?.length ?? 0;
tmpl.generateContents(template);
Expand Down
40 changes: 40 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,46 @@ describe('control flow migration', () => {
expect(content).toBe(result);
});

it('should migrate template with ngIfThen and remove template', 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-template`,
` [ngIf]="mode === 'foo'"`,
` [ngIfThen]="foo"`,
` [ngIfElse]="bar"`,
`></ng-template>`,
`<ng-template #foo>`,
` Foo`,
`</ng-template>`,
`<ng-template #bar>`,
` Bar`,
`</ng-template>`,
].join('\n'));

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

const result = [
`@if (mode === 'foo') {`,
` Foo`,
`} @else {`,
` Bar`,
`}\n`,
].join('\n');

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

it('should move templates after they were migrated to new syntax', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
Expand Down

0 comments on commit 6ae4088

Please sign in to comment.