Skip to content

feat(): support icon name of conditional #38

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
Jan 25, 2024
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 @@ -167,6 +167,53 @@ describe("migrateComponents", () => {
);
});

it("should detect and import icons in conditional used in the template", async () => {
const project = new Project({ useInMemoryFileSystem: true });

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

@Component({
selector: 'my-component',
template: \`<ion-icon [name]="isLogo ? 'logo-ionic' : 'alert'"></ion-icon>\`,
standalone: true
})
export class MyComponent {
isLogo = true;
}
`;

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

await migrateComponents(project, { dryRun: false });

expect(dedent(componentSourceFile.getText())).toBe(
dedent(`
import { Component } from "@angular/core";
import { addIcons } from "ionicons";
import { logoIonic, alert } from "ionicons/icons";
import { IonIcon } from "@ionic/angular/standalone";

@Component({
selector: 'my-component',
template: \`<ion-icon [name]="isLogo ? 'logo-ionic' : 'alert'"></ion-icon>\`,
standalone: true,
imports: [IonIcon]
})
export class MyComponent {
isLogo = true;

constructor() {
addIcons({ logoIonic, alert });
}
}
`),
);
});

it("should remove duplicate imports from existing declarations", async () => {
const project = new Project({ useInMemoryFileSystem: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,38 @@ function detectIonicComponentsAndIcons(htmlAsString: string, filePath: string) {
*/
const iconNameMatch = skippedIcon.match(iconNameRegex);

const deepGetIconConditional = (
ast: typeof boundNameAttribute.value.ast,
icons: string[],
): string[] => {
if (ast.trueExp.type === "LiteralPrimitive") {
if (!ionIcons.includes(ast.trueExp.value)) {
ionIcons.push(ast.trueExp.value);
}
} else if (ast.trueExp.type === "Conditional") {
deepGetIconConditional(ast.trueExp, icons);
} else {
skippedIconsHtml.push(skippedIcon);
}

if (ast.falseExp.type === "LiteralPrimitive") {
if (!ionIcons.includes(ast.falseExp.value)) {
ionIcons.push(ast.falseExp.value);
}
} else if (ast.falseExp.type === "Conditional") {
deepGetIconConditional(ast.falseExp, icons);
} else {
skippedIconsHtml.push(skippedIcon);
}
return icons;
};

if (iconNameMatch) {
if (!ionIcons.includes(iconNameMatch[1])) {
ionIcons.push(iconNameMatch[1]);
}
} else if (boundNameAttribute.value.ast.type === "Conditional") {
deepGetIconConditional(boundNameAttribute.value.ast, ionIcons);
} else {
// IonIcon name is a calculated value from a variable or function.
// We can't determine the value of the name at this time.
Expand Down