Skip to content

Commit 2ab67ea

Browse files
authored
Merge pull request #16 from ionic-team/sp/angular-control-syntax
fix: detect ionic components within *ngIf expressions
2 parents 1fb8506 + 04c4bef commit 2ab67ea

File tree

5 files changed

+117
-9
lines changed

5 files changed

+117
-9
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<ion-header [translucent]="true">
2+
<ion-toolbar>
3+
<ion-title>*ngIf Usage</ion-title>
4+
</ion-toolbar>
5+
</ion-header>
6+
7+
<ion-content [fullscreen]="true">
8+
<ion-header collapse="condense">
9+
<ion-toolbar>
10+
<ion-title size="large">*ngIf Usage</ion-title>
11+
<ion-buttons *ngIf="isVisible">
12+
<ion-button>Toggle</ion-button>
13+
</ion-buttons>
14+
</ion-toolbar>
15+
</ion-header>
16+
</ion-content>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Component } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { FormsModule } from '@angular/forms';
4+
import { IonicModule } from '@ionic/angular';
5+
6+
@Component({
7+
selector: 'app-control-syntax',
8+
templateUrl: './control-syntax.page.html',
9+
standalone: true,
10+
imports: [IonicModule, CommonModule, FormsModule],
11+
})
12+
export class ControlSyntaxPage {
13+
isVisible = true;
14+
}

apps/angular/ionic-angular-standalone/src/app/view-child/view-child.page.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ import { IonContent, IonicModule } from '@ionic/angular';
1010
imports: [IonicModule, CommonModule, FormsModule],
1111
})
1212
export class ViewChildPage implements OnInit {
13-
/**
14-
* Referencing the template's ion-content results in a double call.
15-
*/
1613
@ViewChild(IonContent)
1714
content!: IonContent;
1815

19-
constructor() {}
16+
constructor() { }
2017

21-
ngOnInit() {}
18+
ngOnInit() { }
2219
}

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)