Skip to content

feat(cli): support new control flow #35

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 3 commits into from
Jan 12, 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
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@ionic/angular-standalone-codemods",
"version": "0.0.7",
"dependencies": {
"@angular-eslint/template-parser": "^16.1.2",
"@angular-eslint/template-parser": "^17.1.0",
"@clack/core": "^0.3.3",
"@clack/prompts": "^0.7.0",
"@ionic/utils-terminal": "^2.3.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,4 +579,103 @@ describe("migrateComponents", () => {
);
});
});

it("should migrate components using inline templates with control flow", async () => {
const project = new Project({ useInMemoryFileSystem: true });

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

@Component({
selector: 'my-component',
template: \`
<ion-header>
<ion-toolbar>
<ion-title>My Component</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
@defer {
<ion-list>
@for (item of [0, 1, 2]; track item) {
<ion-item>
@if (1 === 1){ <ion-icon name="logo-ionic" slot="start"></ion-icon> }
@switch (flag) {
@case(0) {
<ion-label>My Item</ion-label>
}
@default {
<ion-label>Your Item</ion-label>
}
}
</ion-item>
}
</ion-list>
} @loading (after 100ms; minimum 1s) {
<ion-icon name="reload-outline" slot="start"></ion-icon>
}
</ion-content>
\`,
standalone: true
})
export class MyComponent { flag = 1 }
`;

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, reloadOutline } from "ionicons/icons";
import { IonHeader, IonToolbar, IonTitle, IonContent, IonList, IonItem, IonIcon, IonLabel } from "@ionic/angular/standalone";

@Component({
selector: 'my-component',
template: \`
<ion-header>
<ion-toolbar>
<ion-title>My Component</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
@defer {
<ion-list>
@for (item of [0, 1, 2]; track item) {
<ion-item>
@if (1 === 1){ <ion-icon name="logo-ionic" slot="start"></ion-icon> }
@switch (flag) {
@case(0) {
<ion-label>My Item</ion-label>
}
@default {
<ion-label>Your Item</ion-label>
}
}
</ion-item>
}
</ion-list>
} @loading (after 100ms; minimum 1s) {
<ion-icon name="reload-outline" slot="start"></ion-icon>
}
</ion-content>
\`,
standalone: true,
imports: [IonHeader, IonToolbar, IonTitle, IonContent, IonList, IonItem, IonIcon, IonLabel]
})
export class MyComponent {
flag = 1

constructor() {
addIcons({ logoIonic, reloadOutline });
}
}
`),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,38 @@ function detectIonicComponentsAndIcons(htmlAsString: string, filePath: string) {
recursivelyFindIonicComponents(childNode);
}
}
} else if (node.type === "IfBlock") {
for (const branch of node.branches) {
for (const childNode of branch.children) {
recursivelyFindIonicComponents(childNode);
}
}
} else if (node.type === "ForLoopBlock") {
for (const childNode of node.children) {
recursivelyFindIonicComponents(childNode);
}
} else if (node.type === "SwitchBlock") {
for (const c of node.cases) {
for (const childNode of c.children) {
recursivelyFindIonicComponents(childNode);
}
}
} else if (node.type === "DeferredBlock") {
if (node.children) {
for (const childNode of node.children) {
recursivelyFindIonicComponents(childNode);
}
}

for (const childKey of Object.keys(node)) {
if (node[childKey]?.children) {
for (const childNode of node[childKey].children) {
recursivelyFindIonicComponents(Object.assign(childNode, {
type: childNode.constructor.name
}));
}
}
}
}
};

Expand Down
17 changes: 11 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.