Skip to content

Commit e5a990a

Browse files
authored
feat(material/schematics): impl card template migration (#24566)
* implement TemplateMigrator for mat-card * add unit tests for card template migrations * implement migration updates in TemplateMigration * added CardTemplateMigrator to list of MIGRATORS
1 parent dbade7d commit e5a990a

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
2+
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3+
import {createNewTestRunner, migrateComponents, TEMPLATE_FILE} from '../test-setup-helper';
4+
5+
describe('card template migrator', () => {
6+
let runner: SchematicTestRunner;
7+
let cliAppTree: UnitTestTree;
8+
9+
async function runMigrationTest(oldFileContent: string, newFileContent: string) {
10+
cliAppTree.overwrite(TEMPLATE_FILE, oldFileContent);
11+
const tree = await migrateComponents(['card'], runner, cliAppTree);
12+
expect(tree.readContent(TEMPLATE_FILE)).toBe(newFileContent);
13+
}
14+
15+
beforeEach(async () => {
16+
runner = createNewTestRunner();
17+
cliAppTree = patchDevkitTreeToExposeTypeScript(await createTestApp(runner));
18+
});
19+
20+
it('should not update other elements', async () => {
21+
await runMigrationTest('<mat-button></mat-button>', '<mat-button></mat-button>');
22+
});
23+
24+
it('should update single', async () => {
25+
await runMigrationTest('<mat-card></mat-card>', '<mat-card appearance="outlined"></mat-card>');
26+
});
27+
28+
it('should update multiple same-line unnested', async () => {
29+
await runMigrationTest(
30+
'<mat-card></mat-card><mat-card></mat-card>',
31+
'<mat-card appearance="outlined"></mat-card><mat-card appearance="outlined"></mat-card>',
32+
);
33+
});
34+
35+
it('should update multiple same-line nested', async () => {
36+
await runMigrationTest(
37+
'<mat-card><mat-card></mat-card></mat-card>',
38+
'<mat-card appearance="outlined"><mat-card appearance="outlined"></mat-card></mat-card>',
39+
);
40+
});
41+
42+
it('should update multiple same-line nested and unnested', async () => {
43+
await runMigrationTest(
44+
'<mat-card><mat-card></mat-card><mat-card></mat-card></mat-card>',
45+
'<mat-card appearance="outlined"><mat-card appearance="outlined"></mat-card><mat-card appearance="outlined"></mat-card></mat-card>',
46+
);
47+
});
48+
49+
it('should update multiple multi-line unnested', async () => {
50+
await runMigrationTest(
51+
`
52+
<mat-card></mat-card>
53+
<mat-card></mat-card>
54+
`,
55+
`
56+
<mat-card appearance="outlined"></mat-card>
57+
<mat-card appearance="outlined"></mat-card>
58+
`,
59+
);
60+
});
61+
62+
it('should update multiple multi-line nested', async () => {
63+
await runMigrationTest(
64+
`
65+
<mat-card>
66+
<mat-card></mat-card>
67+
</mat-card>
68+
`,
69+
`
70+
<mat-card appearance="outlined">
71+
<mat-card appearance="outlined"></mat-card>
72+
</mat-card>
73+
`,
74+
);
75+
});
76+
77+
it('should update multiple multi-line nested and unnested', async () => {
78+
await runMigrationTest(
79+
`
80+
<mat-card>
81+
<mat-card></mat-card>
82+
<mat-card></mat-card>
83+
</mat-card>
84+
`,
85+
`
86+
<mat-card appearance="outlined">
87+
<mat-card appearance="outlined"></mat-card>
88+
<mat-card appearance="outlined"></mat-card>
89+
</mat-card>
90+
`,
91+
);
92+
});
93+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {TmplAstElement} from '@angular/compiler';
10+
import {TemplateMigrator} from '../../template-migrator';
11+
import {addAttribute} from '../../tree-traversal';
12+
13+
export class CardTemplateMigrator extends TemplateMigrator {
14+
component = 'card';
15+
tagName = 'mat-card';
16+
17+
override updateStartTag(template: string, node: TmplAstElement): string {
18+
if (node.name !== this.tagName) {
19+
return template;
20+
}
21+
return addAttribute(template, node, 'appearance', 'outlined');
22+
}
23+
}

src/material/schematics/ng-generate/mdc-migration/rules/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import {ButtonStylesMigrator} from './components/button/button-styles';
1010
import {CardStylesMigrator} from './components/card/card-styles';
11+
import {CardTemplateMigrator} from './components/card/card-template';
1112
import {CheckboxStylesMigrator} from './components/checkbox/checkbox-styles';
1213
import {ChipsStylesMigrator} from './components/chips/chips-styles';
1314
import {DialogStylesMigrator} from './components/dialog/dialog-styles';
@@ -36,6 +37,7 @@ export const MIGRATORS: ComponentMigrator[] = [
3637
{
3738
component: 'card',
3839
styles: new CardStylesMigrator(),
40+
template: new CardTemplateMigrator(),
3941
},
4042
{
4143
component: 'checkbox',

src/material/schematics/ng-generate/mdc-migration/rules/template-migration.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,21 @@ export class TemplateMigration extends Migration<ComponentMigrator[], SchematicC
1616

1717
override visitTemplate(template: ResolvedResource) {
1818
const ast = parseTemplate(template.content, template.filePath);
19+
const migrators = this.upgradeData.filter(m => m.template).map(m => m.template!);
1920

20-
visitElements(ast.nodes, node => {
21-
// TODO(wagnermaciel): implement the migration updates.
22-
});
21+
visitElements(
22+
ast.nodes,
23+
node => {
24+
migrators.forEach(m => {
25+
template.content = m.updateEndTag(template.content, node);
26+
});
27+
},
28+
node => {
29+
migrators.forEach(m => {
30+
template.content = m.updateStartTag(template.content, node);
31+
});
32+
},
33+
);
2334

2435
this.fileSystem.overwrite(template.filePath, template.content);
2536
}

0 commit comments

Comments
 (0)