Skip to content

Commit 04c4bef

Browse files
committed
fix: detect ionic components within *ngIf expressions
1 parent 601ccd6 commit 04c4bef

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

packages/cli/src/angular/migrations/standalone/0002-import-standalone-component.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,85 @@ describe("migrateComponents", () => {
210210
);
211211
});
212212

213+
it("should detect Ionic components within *ngIf expressions", () => {
214+
const project = new Project({ useInMemoryFileSystem: true });
215+
216+
const component = `
217+
import { Component } from "@angular/core";
218+
219+
@Component({
220+
selector: 'my-component',
221+
template: \`
222+
<ion-header [translucent]="true">
223+
<ion-toolbar>
224+
<ion-title>*ngIf Usage</ion-title>
225+
</ion-toolbar>
226+
</ion-header>
227+
<ion-content [fullscreen]="true">
228+
<ion-header collapse="condense">
229+
<ion-toolbar>
230+
<ion-title size="large">*ngIf Usage</ion-title>
231+
<ion-buttons *ngIf="isVisible">
232+
<ion-button>Toggle</ion-button>
233+
</ion-buttons>
234+
</ion-toolbar>
235+
</ion-header>
236+
<div *ngIf="isVisible">
237+
<ion-label>Visible</ion-label>
238+
</div>
239+
</ion-content>
240+
\`,
241+
standalone: true
242+
})
243+
export class MyComponent {
244+
isVisible = true;
245+
}
246+
`;
247+
248+
const componentSourceFile = project.createSourceFile(
249+
"foo.component.ts",
250+
dedent(component),
251+
);
252+
253+
migrateComponents(project, { dryRun: false });
254+
255+
expect(dedent(componentSourceFile.getText())).toBe(
256+
dedent(`
257+
import { Component } from "@angular/core";
258+
import { IonHeader, IonToolbar, IonTitle, IonContent, IonButtons, IonButton, IonLabel } from "@ionic/angular/standalone";
259+
260+
@Component({
261+
selector: 'my-component',
262+
template: \`
263+
<ion-header [translucent]="true">
264+
<ion-toolbar>
265+
<ion-title>*ngIf Usage</ion-title>
266+
</ion-toolbar>
267+
</ion-header>
268+
<ion-content [fullscreen]="true">
269+
<ion-header collapse="condense">
270+
<ion-toolbar>
271+
<ion-title size="large">*ngIf Usage</ion-title>
272+
<ion-buttons *ngIf="isVisible">
273+
<ion-button>Toggle</ion-button>
274+
</ion-buttons>
275+
</ion-toolbar>
276+
</ion-header>
277+
<div *ngIf="isVisible">
278+
<ion-label>Visible</ion-label>
279+
</div>
280+
</ion-content>
281+
\`,
282+
standalone: true,
283+
imports: [IonHeader, IonToolbar, IonTitle, IonContent, IonButtons, IonButton, IonLabel]
284+
})
285+
export class MyComponent {
286+
isVisible = true;
287+
}
288+
`),
289+
);
290+
});
291+
213292
describe("hyperlinks", () => {
214293
it("should detect and import routerLink used in the template", async () => {
215294
const project = new Project({ useInMemoryFileSystem: true });

packages/cli/src/angular/migrations/standalone/0002-import-standalone-component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,12 @@ function detectIonicComponentsAndIcons(htmlAsString: string, filePath: string) {
245245
let hasRouterLink = false;
246246

247247
const recursivelyFindIonicComponents = (node: any) => {
248-
if (node.type === "Element$1") {
249-
if (IONIC_COMPONENTS.includes(node.name)) {
250-
if (!ionicComponents.includes(node.name)) {
251-
ionicComponents.push(node.name);
248+
if (node.type === "Element$1" || node.type === "Template") {
249+
const tagName = node.type === "Template" ? node.tagName : node.name;
250+
251+
if (IONIC_COMPONENTS.includes(tagName)) {
252+
if (!ionicComponents.includes(tagName)) {
253+
ionicComponents.push(tagName);
252254
}
253255

254256
const routerLink =

0 commit comments

Comments
 (0)