Skip to content

Commit cfc90e0

Browse files
committed
fix(material/schematics): don't insert duplicate @use statements in themingApi
In the case where a file has mixed legacy and new theming API usage, the script would add an extra duplicate `@use` statement.
1 parent 94076af commit cfc90e0

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ function renameSymbols(content: string,
205205
/** Inserts an `@use` statement in a string. */
206206
function insertUseStatement(content: string, importPath: string, importsToIgnore: string[],
207207
namespace: string): string {
208+
// If the content already has the `@use` import, we don't need to add anything.
209+
if (content.includes(`@use '${importPath}'`)) {
210+
return content;
211+
}
212+
208213
// We want to find the first import that isn't in the list of ignored imports or find nothing,
209214
// because the imports being replaced might be the only ones in the file and they can be further
210215
// down. An easy way to do this is to replace the imports with a random character and run

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,4 +734,26 @@ describe('v12 theming API migration', () => {
734734
`@include mat.core();`,
735735
]);
736736
});
737+
738+
it('should not add duplicate @use statements', async () => {
739+
const originalContent = [
740+
`@use '~@angular/material' as mat;`,
741+
`@import '~@angular/material/theming';`,
742+
`$something: mat.$red-palette;`,
743+
`$another: $mat-pink;`,
744+
].join('\n');
745+
746+
const migratedContent = migrateFileContent(
747+
originalContent,
748+
'~@angular/material/',
749+
'~@angular/cdk/',
750+
'~@angular/material',
751+
'~@angular/cdk');
752+
753+
expect(migratedContent).toBe([
754+
`@use '~@angular/material' as mat;`,
755+
`$something: mat.$red-palette;`,
756+
`$another: mat.$pink-palette;`,
757+
].join('\n'));
758+
});
737759
});

0 commit comments

Comments
 (0)