Skip to content

fix: detect ionic components within *ngIf expressions #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<ion-header [translucent]="true">
<ion-toolbar>
<ion-title>*ngIf Usage</ion-title>
</ion-toolbar>
</ion-header>

<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">*ngIf Usage</ion-title>
<ion-buttons *ngIf="isVisible">
<ion-button>Toggle</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
</ion-content>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { IonicModule } from '@ionic/angular';

@Component({
selector: 'app-control-syntax',
templateUrl: './control-syntax.page.html',
standalone: true,
imports: [IonicModule, CommonModule, FormsModule],
})
export class ControlSyntaxPage {
isVisible = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import { IonContent, IonicModule } from '@ionic/angular';
imports: [IonicModule, CommonModule, FormsModule],
})
export class ViewChildPage implements OnInit {
/**
* Referencing the template's ion-content results in a double call.
*/
@ViewChild(IonContent)
content!: IonContent;

constructor() {}
constructor() { }

ngOnInit() {}
ngOnInit() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,85 @@ describe("migrateComponents", () => {
);
});

it("should detect Ionic components within *ngIf expressions", () => {
const project = new Project({ useInMemoryFileSystem: true });

const component = `
import { Component } from "@angular/core";

@Component({
selector: 'my-component',
template: \`
<ion-header [translucent]="true">
<ion-toolbar>
<ion-title>*ngIf Usage</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">*ngIf Usage</ion-title>
<ion-buttons *ngIf="isVisible">
<ion-button>Toggle</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<div *ngIf="isVisible">
<ion-label>Visible</ion-label>
</div>
</ion-content>
\`,
standalone: true
})
export class MyComponent {
isVisible = true;
}
`;

const componentSourceFile = project.createSourceFile(
"foo.component.ts",
dedent(component),
);

migrateComponents(project, { dryRun: false });

expect(dedent(componentSourceFile.getText())).toBe(
dedent(`
import { Component } from "@angular/core";
import { IonHeader, IonToolbar, IonTitle, IonContent, IonButtons, IonButton, IonLabel } from "@ionic/angular/standalone";

@Component({
selector: 'my-component',
template: \`
<ion-header [translucent]="true">
<ion-toolbar>
<ion-title>*ngIf Usage</ion-title>
</ion-toolbar>
</ion-header>
<ion-content [fullscreen]="true">
<ion-header collapse="condense">
<ion-toolbar>
<ion-title size="large">*ngIf Usage</ion-title>
<ion-buttons *ngIf="isVisible">
<ion-button>Toggle</ion-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<div *ngIf="isVisible">
<ion-label>Visible</ion-label>
</div>
</ion-content>
\`,
standalone: true,
imports: [IonHeader, IonToolbar, IonTitle, IonContent, IonButtons, IonButton, IonLabel]
})
export class MyComponent {
isVisible = true;
}
`),
);
});

describe("hyperlinks", () => {
it("should detect and import routerLink used in the template", async () => {
const project = new Project({ useInMemoryFileSystem: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,12 @@ function detectIonicComponentsAndIcons(htmlAsString: string, filePath: string) {
let hasRouterLink = false;

const recursivelyFindIonicComponents = (node: any) => {
if (node.type === "Element$1") {
if (IONIC_COMPONENTS.includes(node.name)) {
if (!ionicComponents.includes(node.name)) {
ionicComponents.push(node.name);
if (node.type === "Element$1" || node.type === "Template") {
const tagName = node.type === "Template" ? node.tagName : node.name;

if (IONIC_COMPONENTS.includes(tagName)) {
if (!ionicComponents.includes(tagName)) {
ionicComponents.push(tagName);
}

const routerLink =
Expand Down