Skip to content

fix: remove IonicModule for migrated standalone components #8

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 1 commit into from
Oct 10, 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
Expand Up @@ -74,11 +74,13 @@ describe("migrateComponents", () => {

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

@Component({
selector: 'my-component',
templateUrl: './my-component.component.html',
standalone: true
standalone: true,
imports: [IonicModule]
})
export class MyComponent { }
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getAngularComponentDecorator,
isAngularComponentClass,
isAngularComponentStandalone,
removeImportFromComponentDecorator,
} from "../../utils/angular-utils";
import { IONIC_COMPONENTS } from "../../utils/ionic-utils";
import {
Expand All @@ -26,6 +27,7 @@ import {
import {
addImportToClass,
getOrCreateConstructor,
removeImportFromClass,
} from "../../utils/typescript-utils";
import { saveFileChanges } from "../../utils/log-utils";

Expand Down Expand Up @@ -115,6 +117,8 @@ async function migrateAngularComponentClass(
if (isAngularComponentStandalone(sourceFile)) {
const componentClassName = kebabCaseToPascalCase(ionicComponent);
addImportToComponentDecorator(sourceFile, componentClassName);
removeImportFromComponentDecorator(sourceFile, 'IonicModule');
removeImportFromClass(sourceFile, 'IonicModule', '@ionic/angular');
addImportToClass(
sourceFile,
componentClassName,
Expand Down
23 changes: 23 additions & 0 deletions packages/cli/src/angular/utils/angular-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,29 @@ export function addImportToComponentDecorator(
sourceFile.formatText();
}

/**
* Removes an import from the imports array in the Component decorator.
* @param sourceFile The source file to remove the import from.
* @param importName The name of the import to remove.
*/
export function removeImportFromComponentDecorator(
sourceFile: SourceFile,
importName: string,
) {
if (!isAngularComponentStandalone(sourceFile)) {
console.warn(
"[Ionic Dev] Cannot remove import from component decorator. Component is not standalone.",
);
return;
}

const componentDecorator = getAngularComponentDecorator(sourceFile)!;

deleteFromDecoratorArgArray(componentDecorator, "imports", importName);

sourceFile.formatText();
}

/**
* Adds a new import to the imports array in the NgModule decorator.
* @param sourceFile The source file to add the import to.
Expand Down
59 changes: 58 additions & 1 deletion packages/cli/src/angular/utils/typescript-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it, expect } from "vitest";
import { Project } from "ts-morph";
import { dedent } from "ts-dedent";

import { getOrCreateConstructor, addImportToClass } from "./typescript-utils";
import { getOrCreateConstructor, addImportToClass, removeImportFromClass } from "./typescript-utils";

describe("getOrCreateConstructor", () => {
it("should return the existing constructor", () => {
Expand Down Expand Up @@ -109,3 +109,60 @@ describe("addImportToClass", () => {
);
});
});

describe("removeImportFromClass", () => {

it("should remove an import from a class", () => {
const sourceFileContent = `
import { Component } from "@angular/core";
export class Foo { }
`;

const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("foo.ts", sourceFileContent);

removeImportFromClass(sourceFile, "Component", "@angular/core");

expect(dedent(sourceFile.getText())).toBe(
dedent(`
export class Foo { }
`),
);
});

it("should remove an import from an existing import declaration", () => {
const sourceFileContent = `
import { Injectable, Component } from "@angular/core";
export class Foo { }
`;

const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("foo.ts", sourceFileContent);

removeImportFromClass(sourceFile, "Component", "@angular/core");

expect(dedent(sourceFile.getText())).toBe(
dedent(`
import { Injectable } from "@angular/core";
export class Foo { }
`),
);
});

it("should do nothing if the source file does not have a class", () => {
const sourceFileContent = `
export function foo() { }
`;

const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("foo.ts", sourceFileContent);

removeImportFromClass(sourceFile, "Component", "@angular/core");

expect(dedent(sourceFile.getText())).toBe(
dedent(`
export function foo() { }
`),
);
});
});
38 changes: 38 additions & 0 deletions packages/cli/src/angular/utils/typescript-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,41 @@ export function addImportToClass(
addImport(sourceFile, importName, moduleSpecifier);
}
}

export function removeImportFromClass(
sourceFile: SourceFile,
importName: string | string[],
moduleSpecifier: string,
) {
const removeImport = (
sourceFile: SourceFile,
importName: string,
moduleSpecifier: string,
) => {
const importDeclaration = sourceFile.getImportDeclaration(moduleSpecifier);

if (!importDeclaration) {
return;
}

const importSpecifier = importDeclaration
.getNamedImports()
.find((n) => n.getName() === importName);

if (importSpecifier) {
importSpecifier.remove();
}

if (importDeclaration.getNamedImports().length === 0) {
importDeclaration.remove();
}
};

if (Array.isArray(importName)) {
importName.forEach((name) => {
removeImport(sourceFile, name, moduleSpecifier);
});
} else {
removeImport(sourceFile, importName, moduleSpecifier);
}
}