Skip to content

Commit 38baf24

Browse files
authored
refactor(material/schematics): accept extra symbols for sass api migration (#22658)
Adding support for extra symbols to migrate lets us deal with symbols that we only want to migrate inside Google.
1 parent 14ee548 commit 38baf24

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ interface DetectImportResult {
2222
namespaces: string[];
2323
}
2424

25+
/** Addition mixin and function names that can be updated when invoking migration directly. */
26+
interface ExtraSymbols {
27+
mixins?: Record<string, string>;
28+
functions?: Record<string, string>;
29+
}
30+
2531
/**
2632
* Migrates the content of a file to the new theming API. Note that this migration is using plain
2733
* string manipulation, rather than the AST from PostCSS and the schematics string manipulation
@@ -40,13 +46,15 @@ export function migrateFileContent(content: string,
4046
oldMaterialPrefix: string,
4147
oldCdkPrefix: string,
4248
newMaterialImportPath: string,
43-
newCdkImportPath: string): string {
49+
newCdkImportPath: string,
50+
extraMaterialSymbols: ExtraSymbols = {}): string {
4451
const materialResults = detectImports(content, oldMaterialPrefix);
4552
const cdkResults = detectImports(content, oldCdkPrefix);
4653

4754
// Try to migrate the symbols even if there are no imports. This is used
4855
// to cover the case where the Components symbols were used transitively.
49-
content = migrateMaterialSymbols(content, newMaterialImportPath, materialResults);
56+
content = migrateMaterialSymbols(
57+
content, newMaterialImportPath, materialResults, extraMaterialSymbols);
5058
content = migrateCdkSymbols(content, newCdkImportPath, cdkResults);
5159
content = replaceRemovedVariables(content, removedMaterialVariables);
5260

@@ -103,16 +111,19 @@ function detectImports(content: string, prefix: string): DetectImportResult {
103111

104112
/** Migrates the Material symbols in a file. */
105113
function migrateMaterialSymbols(content: string, importPath: string,
106-
detectedImports: DetectImportResult): string {
114+
detectedImports: DetectImportResult,
115+
extraMaterialSymbols: ExtraSymbols = {}): string {
107116
const initialContent = content;
108117
const namespace = 'mat';
118+
const mixinsToUpdate = {...materialMixins, ...extraMaterialSymbols.mixins};
119+
const functionsToUpdate = {...materialFunctions, ...extraMaterialSymbols.functions};
109120

110121
// Migrate the mixins.
111-
content = renameSymbols(content, materialMixins, detectedImports.namespaces, mixinKeyFormatter,
122+
content = renameSymbols(content, mixinsToUpdate, detectedImports.namespaces, mixinKeyFormatter,
112123
getMixinValueFormatter(namespace));
113124

114125
// Migrate the functions.
115-
content = renameSymbols(content, materialFunctions, detectedImports.namespaces,
126+
content = renameSymbols(content, functionsToUpdate, detectedImports.namespaces,
116127
functionKeyFormatter, getFunctionValueFormatter(namespace));
117128

118129
// Migrate the variables.

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {UnitTestTree} from '@angular-devkit/schematics/testing';
22
import {createTestCaseSetup} from '@angular/cdk/schematics/testing';
3+
import {migrateFileContent} from '@angular/material/schematics/ng-update/migrations/theming-api-v12/migration';
34
import {join} from 'path';
45
import {MIGRATION_PATH} from '../../../../paths';
56

@@ -692,5 +693,27 @@ describe('v12 theming API migration', () => {
692693
]);
693694
});
694695

695-
696+
it('should migrate extra given mixins and functions', () => {
697+
const originalContent = [
698+
`@import '~@angular/material/theming';`,
699+
`$something: mat-mdc-typography-config();`,
700+
`@include mat-mdc-button-theme();`,
701+
].join('\n');
702+
703+
const migratedContent = migrateFileContent(
704+
originalContent,
705+
'~@angular/material/',
706+
'~@angular/cdk/',
707+
'~@angular/material',
708+
'~@angular/cdk', {
709+
mixins: {'mat-mdc-button-theme': 'mdc-button-theme'},
710+
functions: {'mat-mdc-typography-config': 'mdc-typography-config'},
711+
});
712+
713+
expect(migratedContent).toBe([
714+
`@use '~@angular/material' as mat;`,
715+
`$something: mat.mdc-typography-config();`,
716+
`@include mat.mdc-button-theme();`,
717+
].join('\n'));
718+
});
696719
});

0 commit comments

Comments
 (0)