Skip to content

Commit 0029097

Browse files
authored
fix(material/schematics): don't insert duplicate @use statements in themingApi (#22755)
1 parent babf80f commit 0029097

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ function renameSymbols(content: string,
207207
/** Inserts an `@use` statement in a string. */
208208
function insertUseStatement(content: string, importPath: string, importsToIgnore: string[],
209209
namespace: string): string {
210+
// If the content already has the `@use` import, we don't need to add anything.
211+
const alreadyImportedPattern = new RegExp(`@use +['"]${importPath}['"]`, 'g');
212+
if (alreadyImportedPattern.test(content)) {
213+
return content;
214+
}
215+
210216
// We want to find the first import that isn't in the list of ignored imports or find nothing,
211217
// because the imports being replaced might be the only ones in the file and they can be further
212218
// 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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,4 +737,21 @@ describe('v12 theming API migration', () => {
737737
`@include mat.core();`,
738738
]);
739739
});
740+
741+
it('should not add duplicate @use statements', async () => {
742+
writeLines(THEME_PATH, [
743+
`@use '~@angular/material' as mat;`,
744+
`@import '~@angular/material/theming';`,
745+
`$something: mat.$red-palette;`,
746+
`$another: $mat-pink;`,
747+
]);
748+
749+
await runMigration();
750+
751+
expect(splitFile(THEME_PATH)).toEqual([
752+
`@use '~@angular/material' as mat;`,
753+
`$something: mat.$red-palette;`,
754+
`$another: mat.$pink-palette;`,
755+
]);
756+
});
740757
});

0 commit comments

Comments
 (0)