From 74685883ec16782d172a4505348eef6f2003a31b Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 14 Dec 2021 23:53:12 +0100 Subject: [PATCH] fix(material/schematics): don't replace variables contained within the names of other variables in theming API migration (#24021) Fixes that the theming API migration would replace the `$swift-ease-in` inside a variable like `$swift-ease-in-duration`, because the regex would partially match the name. Fixes #24013. --- .../ng-update/migrations/theming-api-v12/migration.ts | 2 +- .../test-cases/v12/misc/theming-api-v12.spec.ts | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/material/schematics/ng-update/migrations/theming-api-v12/migration.ts b/src/material/schematics/ng-update/migrations/theming-api-v12/migration.ts index a227fceffcb0..a168f93f920c 100644 --- a/src/material/schematics/ng-update/migrations/theming-api-v12/migration.ts +++ b/src/material/schematics/ng-update/migrations/theming-api-v12/migration.ts @@ -397,7 +397,7 @@ function replaceRemovedVariables(content: string, variables: Record { // Note that the pattern uses a negative lookahead to exclude // variable assignments, because they can't be migrated. - const regex = new RegExp(`\\$${escapeRegExp(variableName)}(?!\\s+:|:)`, 'g'); + const regex = new RegExp(`\\$${escapeRegExp(variableName)}(?!\\s+:|[-_a-zA-Z0-9:])`, 'g'); content = content.replace(regex, variables[variableName]); }); diff --git a/src/material/schematics/ng-update/test-cases/v12/misc/theming-api-v12.spec.ts b/src/material/schematics/ng-update/test-cases/v12/misc/theming-api-v12.spec.ts index 547450404437..33bc8656bf5c 100644 --- a/src/material/schematics/ng-update/test-cases/v12/misc/theming-api-v12.spec.ts +++ b/src/material/schematics/ng-update/test-cases/v12/misc/theming-api-v12.spec.ts @@ -591,6 +591,17 @@ describe('v12 theming API migration', () => { ]); }); + it('should not replace removed variables whose name overlaps with other variables', async () => { + writeLines(THEME_PATH, [ + `@import '@angular/material/theming';`, + `$swift-ease-in-duration: 300ms !default`, + ]); + + await runMigration(); + + expect(splitFile(THEME_PATH)).toEqual([`$swift-ease-in-duration: 300ms !default`]); + }); + it('should not replace assignments to removed variables', async () => { writeLines(THEME_PATH, [ `@import '@angular/material/theming';`,