Skip to content

Commit 0569118

Browse files
committed
fix(material/schematics): migrate legacy typography hierarchy usages in CSS
Adds a migration that will fix usages of the legacy typography hierarchy mixin and CSS classes. (cherry picked from commit 7f72e73)
1 parent 9cdb4db commit 0569118

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {Schema} from './schema';
2222
import {TemplateMigration} from './rules/template-migration';
2323
import {ThemingStylesMigration} from './rules/theming-styles';
2424
import {TypographyHierarchyTemplateMigrator} from './rules/components/typography-hierarchy/typography-hierarchy-template';
25+
import {TypographyHierarchyStylesMigrator} from './rules/components/typography-hierarchy/typography-hierarchy-styles';
2526

2627
/** Groups of components that must be migrated together. */
2728
const migrationGroups = [
@@ -48,7 +49,7 @@ const migrationGroups = [
4849
const TYPOGRAPHY_HIERARCHY_MIGRATOR: ComponentMigrator = {
4950
component: 'typography-hierarchy',
5051
template: new TypographyHierarchyTemplateMigrator(),
51-
styles: null!, // TODO
52+
styles: new TypographyHierarchyStylesMigrator(),
5253
};
5354

5455
function getComponentsToMigrate(requested: string[]): Set<string> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
2+
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3+
import {createNewTestRunner, migrateComponents, THEME_FILE} from '../test-setup-helper';
4+
5+
describe('typography hierarchy styles migrator', () => {
6+
let runner: SchematicTestRunner;
7+
let cliAppTree: UnitTestTree;
8+
9+
async function runMigrationTest(oldFileContent: string, newFileContent: string) {
10+
cliAppTree.create(THEME_FILE, oldFileContent);
11+
const tree = await migrateComponents([], runner, cliAppTree);
12+
expect(tree.readContent(THEME_FILE)).toBe(newFileContent);
13+
}
14+
15+
beforeEach(async () => {
16+
runner = createNewTestRunner();
17+
cliAppTree = patchDevkitTreeToExposeTypeScript(await createTestApp(runner));
18+
});
19+
20+
it('should replace the legacy hierarchy mixin with the non-legacy version', async () => {
21+
await runMigrationTest(
22+
`
23+
@use '@angular/material' as mat;
24+
$config: ();
25+
@include mat.legacy-typography-hierarchy($config);
26+
`,
27+
`
28+
@use '@angular/material' as mat;
29+
$config: ();
30+
@include mat.typography-hierarchy($config);
31+
`,
32+
);
33+
});
34+
35+
it('should use the correct namespace', async () => {
36+
await runMigrationTest(
37+
`
38+
@use '@angular/material' as arbitrary;
39+
$config: ();
40+
@include arbitrary.legacy-typography-hierarchy($config);
41+
`,
42+
`
43+
@use '@angular/material' as arbitrary;
44+
$config: ();
45+
@include arbitrary.typography-hierarchy($config);
46+
`,
47+
);
48+
});
49+
50+
it('should replace multiple legacy hierarchy mixin usages', async () => {
51+
await runMigrationTest(
52+
`
53+
@use '@angular/material' as mat;
54+
$config: ();
55+
$other-config: ();
56+
@include mat.legacy-typography-hierarchy($config);
57+
@include mat.legacy-typography-hierarchy($other-config);
58+
`,
59+
`
60+
@use '@angular/material' as mat;
61+
$config: ();
62+
$other-config: ();
63+
@include mat.typography-hierarchy($config);
64+
@include mat.typography-hierarchy($other-config);
65+
`,
66+
);
67+
});
68+
69+
it('should replace usages of the legacy typography hierarchy classes', async () => {
70+
await runMigrationTest(
71+
`
72+
.mat-display-4 {
73+
color: red;
74+
}
75+
76+
.mat-display-3 {
77+
color: red;
78+
}
79+
80+
.mat-display-2 {
81+
color: red;
82+
}
83+
84+
.mat-display-1 {
85+
color: red;
86+
}
87+
88+
.mat-headline {
89+
color: red;
90+
}
91+
92+
.mat-title {
93+
color: red;
94+
}
95+
96+
.mat-subheading-2 {
97+
color: red;
98+
}
99+
100+
.mat-body-2 {
101+
color: red;
102+
}
103+
104+
.mat-subheading-1 {
105+
color: red;
106+
}
107+
108+
.mat-body-1 {
109+
color: red;
110+
}
111+
`,
112+
`
113+
.mat-headline-1 {
114+
color: red;
115+
}
116+
117+
.mat-headline-2 {
118+
color: red;
119+
}
120+
121+
.mat-headline-3 {
122+
color: red;
123+
}
124+
125+
.mat-headline-4 {
126+
color: red;
127+
}
128+
129+
.mat-headline-5 {
130+
color: red;
131+
}
132+
133+
.mat-headline-6 {
134+
color: red;
135+
}
136+
137+
.mat-subtitle-1 {
138+
color: red;
139+
}
140+
141+
.mat-subtitle-2 {
142+
color: red;
143+
}
144+
145+
.mat-body-1 {
146+
color: red;
147+
}
148+
149+
.mat-body-2 {
150+
color: red;
151+
}
152+
`,
153+
);
154+
});
155+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 {ClassNameChange, StyleMigrator} from '../../style-migrator';
10+
import {RENAMED_TYPOGRAPHY_CLASSES} from './constants';
11+
12+
export class TypographyHierarchyStylesMigrator extends StyleMigrator {
13+
component = 'typography-hierarchy';
14+
deprecatedPrefixes = [];
15+
classChanges: ClassNameChange[] = [];
16+
mixinChanges = [
17+
{
18+
old: 'legacy-typography-hierarchy',
19+
new: ['typography-hierarchy'],
20+
checkForDuplicates: false,
21+
},
22+
];
23+
24+
constructor() {
25+
super();
26+
27+
RENAMED_TYPOGRAPHY_CLASSES.forEach((newClass, oldClass) => {
28+
this.classChanges.push({new: '.' + newClass, old: '.' + oldClass});
29+
});
30+
}
31+
}

0 commit comments

Comments
 (0)