Skip to content

Commit 5c0da5a

Browse files
crisbetommalerba
authored andcommitted
fix(material/schematics): only migrate unprefixed variables when there is an import
Changes the migration logic so that variables without a `mat-` or `cdk-` prefix are only migrated if there is an explicit Material import. This aims to prevent accidental renames for variables that we don't own. Also adds a few variables that were previously skipped. (cherry picked from commit d5f4523)
1 parent bd72e4f commit 5c0da5a

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

src/cdk/schematics/ng-update/find-stylesheets.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ const STYLESHEET_REGEX = /.*\.(css|scss)/;
1515
/**
1616
* Finds stylesheets in the given directory from within the specified tree.
1717
* @param tree Devkit tree where stylesheet files can be found in.
18-
* @param startDirectory Optional start directory where stylesheets should be searched in. This can be
19-
* useful if only stylesheets within a given folder are relevant (to avoid unnecessary iterations).
18+
* @param startDirectory Optional start directory where stylesheets should be searched in.
19+
* This can be useful if only stylesheets within a given folder are relevant (to avoid
20+
* unnecessary iterations).
2021
*/
2122
export function findStylesheetFiles(tree: Tree, startDirectory: string = '/'): string[] {
2223
const result: string[] = [];

src/material/schematics/ng-update/migrations/theming-api-v12/config.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ export const removedMaterialVariables: Record<string, string> = {
169169
'mat-tree-node-height': '48px',
170170
'mat-tree-node-minimum-height': '24px',
171171
'mat-tree-node-maximum-height': '48px',
172+
};
173+
174+
/**
175+
* Material variables **without a `mat-` prefix** that have been removed from the public API
176+
* and which should be replaced with their values. These should be migrated only when there's a
177+
* Material import, because their names could conflict with other variables in the user's app.
178+
*/
179+
export const unprefixedRemovedVariables: Record<string, string> = {
172180
'z-index-fab': '20',
173181
'z-index-drawer': '100',
174182
'ease-in-out-curve-function': 'cubic-bezier(0.35, 0, 0.25, 1)',
@@ -183,5 +191,21 @@ export const removedMaterialVariables: Record<string, string> = {
183191
'swift-ease-in-out': 'all 500ms cubic-bezier(0.35, 0, 0.25, 1)',
184192
'swift-linear-duration': '80ms',
185193
'swift-linear-timing-function': 'linear',
186-
'swift-linear': 'all 80ms linear'
194+
'swift-linear': 'all 80ms linear',
195+
'black-87-opacity': 'rgba(black, 0.87)',
196+
'white-87-opacity': 'rgba(white, 0.87)',
197+
'black-12-opacity': 'rgba(black, 0.12)',
198+
'white-12-opacity': 'rgba(white, 0.12)',
199+
'black-6-opacity': 'rgba(black, 0.06)',
200+
'white-6-opacity': 'rgba(white, 0.06)',
201+
'dark-primary-text': 'rgba(black, 0.87)',
202+
'dark-secondary-text': 'rgba(black, 0.54)',
203+
'dark-disabled-text': 'rgba(black, 0.38)',
204+
'dark-dividers': 'rgba(black, 0.12)',
205+
'dark-focused': 'rgba(black, 0.12)',
206+
'light-primary-text': 'white',
207+
'light-secondary-text': 'rgba(white, 0.7)',
208+
'light-disabled-text': 'rgba(white, 0.5)',
209+
'light-dividers': 'rgba(white, 0.12)',
210+
'light-focused': 'rgba(white, 0.12)',
187211
};

src/material/schematics/ng-update/migrations/theming-api-v12/migration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
materialVariables,
1313
cdkMixins,
1414
cdkVariables,
15-
removedMaterialVariables
15+
removedMaterialVariables,
16+
unprefixedRemovedVariables
1617
} from './config';
1718

1819
/**
@@ -47,6 +48,7 @@ export function migrateFileContent(content: string,
4748
// imported transitively so we can always drop the old imports. We also assume that imports
4849
// to the new entry points have been added already.
4950
if (materialResults.imports.length) {
51+
content = replaceRemovedVariables(content, unprefixedRemovedVariables);
5052
content = removeStrings(content, materialResults.imports);
5153
}
5254

src/material/schematics/ng-update/test-cases/v12/misc/theming-api-v12.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,40 @@ describe('v12 theming API migration', () => {
623623
]);
624624
});
625625

626+
it('should only migrate unprefixed variables if there is a theming import', async () => {
627+
const otherTheme = join(PROJECT_PATH, 'other-theme.scss');
628+
629+
writeLines(THEME_PATH, [
630+
`@import '~@angular/material/theming';`,
631+
``,
632+
`.my-button {`,
633+
`z-index: $z-index-fab;`,
634+
`}`
635+
]);
636+
637+
writeLines(otherTheme, [
638+
`@import 're-exports-material-symbols';`,
639+
``,
640+
`.my-drawer {`,
641+
`z-index: $z-index-drawer;`,
642+
`}`
643+
]);
644+
645+
await runMigration();
646+
647+
expect(splitFile(THEME_PATH)).toEqual([
648+
`.my-button {`,
649+
`z-index: 20;`,
650+
`}`
651+
]);
652+
653+
expect(splitFile(otherTheme)).toEqual([
654+
`@import 're-exports-material-symbols';`,
655+
``,
656+
`.my-drawer {`,
657+
`z-index: $z-index-drawer;`,
658+
`}`
659+
]);
660+
});
661+
626662
});

0 commit comments

Comments
 (0)