Skip to content

Commit 6feaaea

Browse files
wagnermacielmmalerba
authored andcommitted
feat(material/schematics): add template migration to schematic (#24563)
* create ComponentMigrator interface to store the different types of migrations for each component * refactor individual migrations to work with ComponentMigrator * add missing unit test for addAttribute * create TemplateMigrator to unify the interface for template migrations
1 parent 0108de5 commit 6feaaea

File tree

6 files changed

+118
-27
lines changed

6 files changed

+118
-27
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import {Schema} from './schema';
1111
import {DevkitFileSystem, UpdateProject, findStylesheetFiles} from '@angular/cdk/schematics';
1212
import {ThemingStylesMigration} from './rules/theming-styles';
1313
import {TemplateMigration} from './rules/template-migration';
14-
import {MIGRATORS} from './rules';
14+
import {ComponentMigrator, MIGRATORS} from './rules';
1515
import {dirname} from 'path';
16-
import {StyleMigrator} from './rules/style-migrator';
1716

1817
/** Groups of components that must be migrated together. */
1918
const migrationGroups = [
@@ -62,7 +61,7 @@ export default function (options: Schema): Rule {
6261
console.log('Migrating:', [...componentsToMigrate]);
6362
console.log('Directory:', migrationDir);
6463

65-
const migrators: StyleMigrator[] = [];
64+
const migrators: ComponentMigrator[] = [];
6665
for (let i = 0; i < MIGRATORS.length; i++) {
6766
if (componentsToMigrate.has(MIGRATORS[i].component)) {
6867
migrators.push(MIGRATORS[i]);

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

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,62 @@ import {SlideToggleStylesMigrator} from './components/slide-toggle/slide-toggle-
1919
import {SliderStylesMigrator} from './components/slider/slider-styles';
2020
import {TableStylesMigrator} from './components/table/table-styles';
2121
import {StyleMigrator} from './style-migrator';
22+
import {TemplateMigrator} from './template-migrator';
2223

23-
export const MIGRATORS: StyleMigrator[] = [
24-
new ButtonStylesMigrator(),
25-
new CardStylesMigrator(),
26-
new CheckboxStylesMigrator(),
27-
new ChipsStylesMigrator(),
28-
new DialogStylesMigrator(),
29-
new PaginatorStylesMigrator(),
30-
new ProgressBarStylesMigrator(),
31-
new ProgressSpinnerStylesMigrator(),
32-
new RadioStylesMigrator(),
33-
new SlideToggleStylesMigrator(),
34-
new SliderStylesMigrator(),
35-
new TableStylesMigrator(),
24+
/** Contains the migrators to migrate a single component. */
25+
export interface ComponentMigrator {
26+
component: string;
27+
styles: StyleMigrator;
28+
template?: TemplateMigrator;
29+
}
30+
31+
export const MIGRATORS: ComponentMigrator[] = [
32+
{
33+
component: 'button',
34+
styles: new ButtonStylesMigrator(),
35+
},
36+
{
37+
component: 'card',
38+
styles: new CardStylesMigrator(),
39+
},
40+
{
41+
component: 'checkbox',
42+
styles: new CheckboxStylesMigrator(),
43+
},
44+
{
45+
component: 'chips',
46+
styles: new ChipsStylesMigrator(),
47+
},
48+
{
49+
component: 'dialog',
50+
styles: new DialogStylesMigrator(),
51+
},
52+
{
53+
component: 'paginator',
54+
styles: new PaginatorStylesMigrator(),
55+
},
56+
{
57+
component: 'progress-bar',
58+
styles: new ProgressBarStylesMigrator(),
59+
},
60+
{
61+
component: 'progress-spinner',
62+
styles: new ProgressSpinnerStylesMigrator(),
63+
},
64+
{
65+
component: 'radio',
66+
styles: new RadioStylesMigrator(),
67+
},
68+
{
69+
component: 'slide-toggle',
70+
styles: new SlideToggleStylesMigrator(),
71+
},
72+
{
73+
component: 'slider',
74+
styles: new SliderStylesMigrator(),
75+
},
76+
{
77+
component: 'table',
78+
styles: new TableStylesMigrator(),
79+
},
3680
];

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

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

99
import {Migration, ResolvedResource} from '@angular/cdk/schematics';
1010
import {SchematicContext} from '@angular-devkit/schematics';
11-
import {StyleMigrator} from './style-migrator';
1211
import {visitElements, parseTemplate} from './tree-traversal';
12+
import {ComponentMigrator} from '.';
1313

14-
export class TemplateMigration extends Migration<StyleMigrator[], SchematicContext> {
14+
export class TemplateMigration extends Migration<ComponentMigrator[], SchematicContext> {
1515
enabled = true;
1616

1717
override visitTemplate(template: ResolvedResource) {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 * as compiler from '@angular/compiler';
10+
11+
export abstract class TemplateMigrator {
12+
/** The name of the component that this migration handles. */
13+
abstract component: string;
14+
15+
/** The tag name to be updated in the template. */
16+
abstract tagName: string;
17+
18+
/**
19+
* Updates the start tag of the given node in the html template.
20+
*
21+
* @param template The html content to be updated.
22+
* @param node The Element node to be updated.
23+
* @returns The updated template.
24+
*/
25+
updateEndTag(template: string, node: compiler.TmplAstElement): string {
26+
return template;
27+
}
28+
29+
/**
30+
* Updates the end tag of the given node in the html template.
31+
*
32+
* @param template The html content to be updated.
33+
* @param node The Element node to be updated.
34+
* @returns The updated template.
35+
*/
36+
updateStartTag(template: string, node: compiler.TmplAstElement): string {
37+
return template;
38+
}
39+
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
import {Migration, ResolvedResource} from '@angular/cdk/schematics';
1010
import {SchematicContext} from '@angular-devkit/schematics';
11-
import {StyleMigrator} from './style-migrator';
1211
import * as postcss from 'postcss';
1312
import * as scss from 'postcss-scss';
13+
import {ComponentMigrator} from '.';
1414

15-
export class ThemingStylesMigration extends Migration<StyleMigrator[], SchematicContext> {
15+
export class ThemingStylesMigration extends Migration<ComponentMigrator[], SchematicContext> {
1616
enabled = true;
1717
namespace: string;
1818

@@ -40,13 +40,13 @@ export class ThemingStylesMigration extends Migration<StyleMigrator[], Schematic
4040

4141
atIncludeHandler(atRule: postcss.AtRule) {
4242
const migrator = this.upgradeData.find(m => {
43-
return m.isLegacyMixin(this.namespace, atRule);
43+
return m.styles.isLegacyMixin(this.namespace, atRule);
4444
});
4545
if (migrator) {
46-
migrator.replaceMixin(this.namespace, atRule);
46+
migrator.styles.replaceMixin(this.namespace, atRule);
4747
} else if (atRule.params.includes('all-component-themes') && atRule.parent) {
4848
this.upgradeData.forEach(m => {
49-
m?.addNewMixinsAfterNode(this.namespace, atRule);
49+
m?.styles.addNewMixinsAfterNode(this.namespace, atRule);
5050
});
5151
}
5252
}
@@ -56,15 +56,15 @@ export class ThemingStylesMigration extends Migration<StyleMigrator[], Schematic
5656
let isDeprecatedSelector;
5757

5858
const migrator = this.upgradeData.find(m => {
59-
isLegacySelector = m.isLegacySelector(rule);
60-
isDeprecatedSelector = m.isDeprecatedSelector(rule);
59+
isLegacySelector = m.styles.isLegacySelector(rule);
60+
isDeprecatedSelector = m.styles.isDeprecatedSelector(rule);
6161
return isLegacySelector || isDeprecatedSelector;
6262
});
6363

6464
if (isLegacySelector) {
65-
migrator?.replaceLegacySelector(rule);
65+
migrator?.styles.replaceLegacySelector(rule);
6666
} else if (isDeprecatedSelector) {
67-
migrator?.addDeprecatedSelectorComment(rule);
67+
migrator?.styles.addDeprecatedSelectorComment(rule);
6868
}
6969
}
7070
}

src/material/schematics/ng-generate/mdc-migration/rules/tree-traversal.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,14 @@ describe('#visitElements', () => {
109109
'<a attr="val"><b attr="val"></b><c attr="val"></c></a>',
110110
);
111111
});
112+
113+
it('should handle adding multiple attrs to a single element', async () => {
114+
let html = '<a></a>';
115+
visitElements(parseTemplate(html).nodes, undefined, node => {
116+
html = addAttribute(html, node, 'attr1', 'val1');
117+
html = addAttribute(html, node, 'attr2', 'val2');
118+
});
119+
expect(html).toBe('<a attr2="val2" attr1="val1"></a>');
120+
});
112121
});
113122
});

0 commit comments

Comments
 (0)