Skip to content

Commit 46d8c85

Browse files
committed
fix(material/schematics): add missing material/core transforms
1 parent d53e53b commit 46d8c85

File tree

8 files changed

+71
-22
lines changed

8 files changed

+71
-22
lines changed

integration/mdc-migration/golden/src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {MatSnackBarModule} from '@angular/material/snack-bar';
2525
import {MatTableModule} from '@angular/material/table';
2626
import {MatTabsModule} from '@angular/material/tabs';
2727
import {MatTooltipModule} from '@angular/material/tooltip';
28+
import {MatOptionModule, VERSION} from '@angular/material/core';
2829
import {AutocompleteComponent} from './components/autocomplete/autocomplete.component';
2930
import {ButtonComponent} from './components/button/button.component';
3031
import {CardComponent} from './components/card/card.component';
@@ -94,9 +95,12 @@ import {TooltipComponent} from './components/tooltip/tooltip.component';
9495
MatTableModule,
9596
MatTabsModule,
9697
MatTooltipModule,
98+
MatOptionModule,
9799
ReactiveFormsModule,
98100
],
99101
providers: [],
100102
bootstrap: [AppComponent],
101103
})
102-
export class AppModule {}
104+
export class AppModule {
105+
version = VERSION;
106+
}

integration/mdc-migration/golden/src/styles.scss

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// Include the common styles for Angular Material. We include this here so that you only
88
// have to load a single css file for Angular Material in your app.
99
// Be sure that you only ever include this mixin once!
10-
@include mat.all-component-typographies();
11-
@include mat.legacy-core();
10+
@include mat.all-component-typographies(mat.define-typography-config());
11+
@include mat.core();
1212

1313
// Define the palettes for your theme using the Material Design palettes available in palette.scss
1414
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
@@ -26,9 +26,11 @@ $sample-project-theme: mat.define-light-theme((
2626
primary: $sample-project-primary,
2727
accent: $sample-project-accent,
2828
warn: $sample-project-warn,
29-
)
29+
),
30+
typography: mat.define-typography-config(),
3031
));
3132

33+
@include mat.core-theme($sample-project-theme);
3234
@include mat.autocomplete-theme($sample-project-theme);
3335
@include mat.button-theme($sample-project-theme);
3436
@include mat.fab-theme($sample-project-theme);
@@ -52,6 +54,8 @@ $sample-project-theme: mat.define-light-theme((
5254
@include mat.table-theme($sample-project-theme);
5355
@include mat.tabs-theme($sample-project-theme);
5456
@include mat.tooltip-theme($sample-project-theme);
57+
@include mat.option-theme($sample-project-theme);
58+
@include mat.optgroup-theme($sample-project-theme);
5559

5660
/* You can add global styles to this file, and also import other style files */
5761

integration/mdc-migration/sample-project/src/app/app.module.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {MatLegacySnackBarModule as MatSnackBarModule} from '@angular/material/le
2525
import {MatLegacyTableModule as MatTableModule} from '@angular/material/legacy-table';
2626
import {MatLegacyTabsModule as MatTabsModule} from '@angular/material/legacy-tabs';
2727
import {MatLegacyTooltipModule as MatTooltipModule} from '@angular/material/legacy-tooltip';
28+
import {MatLegacyOptionModule, LEGACY_VERSION} from '@angular/material/legacy-core';
2829
import {AutocompleteComponent} from './components/autocomplete/autocomplete.component';
2930
import {ButtonComponent} from './components/button/button.component';
3031
import {CardComponent} from './components/card/card.component';
@@ -94,9 +95,12 @@ import {TooltipComponent} from './components/tooltip/tooltip.component';
9495
MatTableModule,
9596
MatTabsModule,
9697
MatTooltipModule,
98+
MatLegacyOptionModule,
9799
ReactiveFormsModule,
98100
],
99101
providers: [],
100102
bootstrap: [AppComponent],
101103
})
102-
export class AppModule {}
104+
export class AppModule {
105+
version = LEGACY_VERSION;
106+
}

integration/mdc-migration/sample-project/src/styles.scss

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// Include the common styles for Angular Material. We include this here so that you only
88
// have to load a single css file for Angular Material in your app.
99
// Be sure that you only ever include this mixin once!
10-
@include mat.all-legacy-component-typographies();
10+
@include mat.all-legacy-component-typographies(mat.define-legacy-typography-config());
1111
@include mat.legacy-core();
1212

1313
// Define the palettes for your theme using the Material Design palettes available in palette.scss
@@ -26,9 +26,11 @@ $sample-project-theme: mat.define-light-theme((
2626
primary: $sample-project-primary,
2727
accent: $sample-project-accent,
2828
warn: $sample-project-warn,
29-
)
29+
),
30+
typography: mat.define-legacy-typography-config(),
3031
));
3132

33+
@include mat.legacy-core-theme($sample-project-theme);
3234
@include mat.legacy-autocomplete-theme($sample-project-theme);
3335
@include mat.legacy-button-theme($sample-project-theme);
3436
@include mat.legacy-card-theme($sample-project-theme);
@@ -49,6 +51,8 @@ $sample-project-theme: mat.define-light-theme((
4951
@include mat.legacy-table-theme($sample-project-theme);
5052
@include mat.legacy-tabs-theme($sample-project-theme);
5153
@include mat.legacy-tooltip-theme($sample-project-theme);
54+
@include mat.legacy-option-theme($sample-project-theme);
55+
@include mat.legacy-optgroup-theme($sample-project-theme);
5256

5357
/* You can add global styles to this file, and also import other style files */
5458

src/material/schematics/ng-generate/mdc-migration/rules/components/multiple-components-styles.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ describe('multiple component styles', () => {
6666
`
6767
@use '@angular/material' as mat;
6868
$theme: ();
69+
@include mat.legacy-core();
6970
@include mat.all-legacy-component-themes($sample-project-themes);
7071
@include mat.all-legacy-component-colors($sample-colors);
7172
@include mat.all-legacy-component-typographies($sample-typographies);
7273
`,
7374
`
7475
@use '@angular/material' as mat;
7576
$theme: ();
77+
@include mat.core();
7678
@include mat.all-component-themes($sample-project-themes);
7779
@include mat.all-component-colors($sample-colors);
7880
@include mat.all-component-typographies($sample-typographies);
@@ -86,13 +88,17 @@ describe('multiple component styles', () => {
8688
`
8789
@use '@angular/material' as mat;
8890
$theme: ();
91+
@include mat.legacy-core();
8992
@include mat.all-legacy-component-themes($sample-project-themes);
9093
@include mat.all-legacy-component-colors($sample-colors);
9194
@include mat.all-legacy-component-typographies($sample-typographies);
9295
`,
9396
`
9497
@use '@angular/material' as mat;
9598
$theme: ();
99+
/* TODO(mdc-migration): Remove legacy-core once all legacy components are migrated */
100+
@include mat.legacy-core();
101+
@include mat.core();
96102
/* TODO(mdc-migration): Remove all-legacy-component-themes once all legacy components are migrated */
97103
@include mat.all-legacy-component-themes($sample-project-themes);
98104
@include mat.all-component-themes($sample-project-themes);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ export class OptionStylesMigrator extends StyleMigrator {
2626
old: 'legacy-option-typography',
2727
new: ['option-typography'],
2828
},
29+
{
30+
old: 'legacy-core-theme',
31+
new: ['core-theme'],
32+
},
33+
{
34+
old: 'legacy-core-color',
35+
new: ['core-color'],
36+
},
37+
{
38+
old: 'legacy-core-typography',
39+
new: ['core-typography'],
40+
},
2941
];
3042

3143
classChanges: ClassNameChange[] = [

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import * as postcss from 'postcss';
1212
import * as scss from 'postcss-scss';
1313
import {ComponentMigrator, MIGRATORS} from '.';
1414

15-
const ALL_LEGACY_COMPONENTS_MIXIN_NAME = '(?:\\.)(.*)(?:\\()';
15+
const COMPONENTS_MIXIN_NAME = /\.([^(;]*)/;
1616

1717
export class ThemingStylesMigration extends Migration<ComponentMigrator[], SchematicContext> {
1818
enabled = true;
1919
namespace: string;
2020

2121
override visitStylesheet(stylesheet: ResolvedResource) {
22+
const migratedContent = this.migrate(stylesheet.content, stylesheet.filePath).replace(
23+
new RegExp(`${this.namespace}.define-legacy-typography-config\\(`, 'g'),
24+
`${this.namespace}.define-typography-config(`,
25+
);
2226
this.fileSystem
2327
.edit(stylesheet.filePath)
2428
.remove(stylesheet.start, stylesheet.content.length)
25-
.insertRight(stylesheet.start, this.migrate(stylesheet.content, stylesheet.filePath));
29+
.insertRight(stylesheet.start, migratedContent);
2630
}
2731

2832
migrate(styles: string, filename: string): string {
@@ -61,21 +65,27 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
6165
if (mixinChange) {
6266
replaceAtRuleWithMultiple(atRule, mixinChange.old, mixinChange.new);
6367
}
64-
} else if (atRule.params.includes('all-legacy-component') && atRule.parent) {
68+
} else if (atRule.parent && this.isCrossCuttingMixin(atRule.params)) {
6569
if (this.isPartialMigration()) {
6670
// the second element of the result from match is the matching text
67-
const mixinName = atRule.params.match(ALL_LEGACY_COMPONENTS_MIXIN_NAME)![1];
68-
const comment =
69-
'TODO(mdc-migration): Remove ' + mixinName + ' once all legacy components are migrated';
71+
const mixinName = atRule.params.match(COMPONENTS_MIXIN_NAME)![1];
72+
const comment = `TODO(mdc-migration): Remove ${mixinName} once all legacy components are migrated`;
7073
if (!addLegacyCommentForPartialMigrations(atRule, comment)) {
71-
// same all-legacy-component mixin already replaced, nothing to do here
74+
// same mixin already replaced, nothing to do here
7275
return;
7376
}
7477
}
75-
replaceAllComponentsMixin(atRule);
78+
replaceCrossCuttingMixin(atRule, this.namespace);
7679
}
7780
}
7881

82+
isCrossCuttingMixin(mixinText: string) {
83+
return [
84+
`${this.namespace}\\.all-legacy-component-`,
85+
`${this.namespace}\\.legacy-core([^-]|$)`,
86+
].some(r => new RegExp(r).test(mixinText));
87+
}
88+
7989
isPartialMigration() {
8090
return this.upgradeData.length !== MIGRATORS.length;
8191
}
@@ -157,7 +167,7 @@ function addLegacyCommentForPartialMigrations(
157167
/**
158168
* Adds comment before postcss rule or at rule node
159169
*
160-
* @param rule a postcss rule.
170+
* @param node a postcss rule.
161171
* @param comment the text content for the comment
162172
*/
163173
function addCommentBeforeNode(node: postcss.Rule | postcss.AtRule, comment: string): void {
@@ -174,15 +184,18 @@ function addCommentBeforeNode(node: postcss.Rule | postcss.AtRule, comment: stri
174184
}
175185

176186
/**
177-
* Replaces mixin prefixed with `all-legacy-component` to the MDC equivalent.
187+
* Replaces a cross-cutting mixin that affects multiple components with the MDC equivalent.
178188
*
179-
* @param allComponentThemesNode a all-components-theme mixin node
189+
* @param atRule A mixin inclusion node
190+
* @param namespace The @angular/material namespace
180191
*/
181-
function replaceAllComponentsMixin(allComponentNode: postcss.AtRule) {
182-
allComponentNode.cloneBefore({
183-
params: allComponentNode.params.replace('all-legacy-component', 'all-component'),
192+
function replaceCrossCuttingMixin(atRule: postcss.AtRule, namespace: string) {
193+
atRule.cloneBefore({
194+
params: atRule.params
195+
.replace(`${namespace}.all-legacy-component`, `${namespace}.all-component`)
196+
.replace(`${namespace}.legacy-core`, `${namespace}.core`),
184197
});
185-
allComponentNode.remove();
198+
atRule.remove();
186199
}
187200

188201
/**

src/material/schematics/ng-update/test-cases/v15/legacy-components-v15.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ describe('v15 legacy components migration', () => {
286286
`@use '@angular/material' as mat;`,
287287
`@include mat.core();`,
288288
`@include mat.core(mat.define-typography-config());`,
289+
`@include mat.core-theme(())`,
289290
],
290291
new: [
291292
`@use '@angular/material' as mat;`,
@@ -299,6 +300,7 @@ describe('v15 legacy components migration', () => {
299300
`// If you add typography styles elsewhere, you may want to remove this.`,
300301
`@include mat.all-legacy-component-typographies(mat.define-legacy-typography-config());`,
301302
`@include mat.legacy-core();`,
303+
`@include mat.legacy-core-theme(())`,
302304
],
303305
});
304306
});

0 commit comments

Comments
 (0)