Skip to content

Commit 1fb8506

Browse files
authored
Merge pull request #15 from ionic-team/sp/updates
fix: remove duplicate imports when migrating standalone components
2 parents f768724 + d601f44 commit 1fb8506

File tree

7 files changed

+106
-16
lines changed

7 files changed

+106
-16
lines changed

apps/angular/ionic-angular-modules/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@angular/platform-browser": "^16.0.0",
1919
"@angular/platform-browser-dynamic": "^16.0.0",
2020
"@angular/router": "^16.0.0",
21-
"@ionic/angular": "^7.0.0",
21+
"@ionic/angular": "^7.5.0",
2222
"ionicons": "^7.0.0",
2323
"rxjs": "~7.8.0",
2424
"tslib": "^2.3.0",

apps/angular/ionic-angular-standalone/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@capacitor/haptics": "5.0.6",
2727
"@capacitor/keyboard": "5.0.6",
2828
"@capacitor/status-bar": "5.0.6",
29-
"@ionic/angular": "^7.0.0",
29+
"@ionic/angular": "^7.5.0",
3030
"ionicons": "^7.0.0",
3131
"rxjs": "~7.8.0",
3232
"tslib": "^2.3.0",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<ion-header [translucent]="true">
2+
<ion-toolbar>
3+
<ion-title>view-child</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">view-child</ion-title>
11+
</ion-toolbar>
12+
</ion-header>
13+
</ion-content>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Component, OnInit, ViewChild } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { FormsModule } from '@angular/forms';
4+
import { IonContent, IonicModule } from '@ionic/angular';
5+
6+
@Component({
7+
selector: 'app-view-child',
8+
templateUrl: './view-child.page.html',
9+
standalone: true,
10+
imports: [IonicModule, CommonModule, FormsModule],
11+
})
12+
export class ViewChildPage implements OnInit {
13+
/**
14+
* Referencing the template's ion-content results in a double call.
15+
*/
16+
@ViewChild(IonContent)
17+
content!: IonContent;
18+
19+
constructor() {}
20+
21+
ngOnInit() {}
22+
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,49 @@ describe("migrateComponents", () => {
167167
);
168168
});
169169

170+
it("should remove duplicate imports from existing declarations", async () => {
171+
const project = new Project({ useInMemoryFileSystem: true });
172+
173+
const component = `
174+
import { Component, ViewChild } from "@angular/core";
175+
import { IonContent, IonicModule } from "@ionic/angular";
176+
177+
@Component({
178+
selector: 'my-component',
179+
template: '<ion-content></ion-content>',
180+
standalone: true,
181+
imports: [IonicModule]
182+
})
183+
export class MyComponent {
184+
@ViewChild(IonContent) content!: IonContent;
185+
}
186+
`;
187+
188+
const componentSourceFile = project.createSourceFile(
189+
"foo.component.ts",
190+
dedent(component),
191+
);
192+
193+
await migrateComponents(project, { dryRun: false });
194+
195+
expect(dedent(componentSourceFile.getText())).toBe(
196+
dedent(`
197+
import { Component, ViewChild } from "@angular/core";
198+
import { IonContent } from "@ionic/angular/standalone";
199+
200+
@Component({
201+
selector: 'my-component',
202+
template: '<ion-content></ion-content>',
203+
standalone: true,
204+
imports: [IonContent]
205+
})
206+
export class MyComponent {
207+
@ViewChild(IonContent) content!: IonContent;
208+
}
209+
`),
210+
);
211+
});
212+
170213
describe("hyperlinks", () => {
171214
it("should detect and import routerLink used in the template", async () => {
172215
const project = new Project({ useInMemoryFileSystem: true });

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ async function migrateAngularComponentClass(
154154
componentClassName,
155155
"@ionic/angular/standalone",
156156
);
157+
/**
158+
* This removes the import from the class, if it is present.
159+
* An example where it may exist is when the developer has
160+
* a @ViewChild decorator that references an ionic component.
161+
*/
162+
removeImportFromClass(sourceFile, componentClassName, "@ionic/angular");
157163
} else if (ngModuleSourceFile) {
158164
const componentClassName = kebabCaseToPascalCase(ionicComponent);
159165

pnpm-lock.yaml

Lines changed: 20 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)