From 07b5ead1f7338c5cfe8dad810a233e102b6a7a2b Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 3 Jun 2024 09:55:40 +0200 Subject: [PATCH] fix(material/schematics): theming API migration not working with CRLF line endings Fixes that the migration didn't account for CRLF line endings when figuring out the Sass namespace. Fixes #29147. --- .../migrations/m2-theming-v18/migration.ts | 2 +- .../ng-update/test-cases/m2-theming.spec.ts | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/material/schematics/ng-update/migrations/m2-theming-v18/migration.ts b/src/material/schematics/ng-update/migrations/m2-theming-v18/migration.ts index 9733303b654f..b548cdf56d4b 100644 --- a/src/material/schematics/ng-update/migrations/m2-theming-v18/migration.ts +++ b/src/material/schematics/ng-update/migrations/m2-theming-v18/migration.ts @@ -271,7 +271,7 @@ function extractNamespaceFromUseStatement(fullImport: string): string { function getNamespaces(moduleName: string, content: string): string[] { const namespaces = new Set(); const escapedName = moduleName.replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1'); - const pattern = new RegExp(`@use +['"]${escapedName}['"].*;?\n`, 'g'); + const pattern = new RegExp(`@use +['"]${escapedName}['"].*;?\\r?\\n`, 'g'); let match: RegExpExecArray | null = null; while ((match = pattern.exec(content))) { diff --git a/src/material/schematics/ng-update/test-cases/m2-theming.spec.ts b/src/material/schematics/ng-update/test-cases/m2-theming.spec.ts index 41990ef8aaf4..d34716714a42 100644 --- a/src/material/schematics/ng-update/test-cases/m2-theming.spec.ts +++ b/src/material/schematics/ng-update/test-cases/m2-theming.spec.ts @@ -309,4 +309,46 @@ describe('M2 theming migration', () => { `@include matx.something-not-theming-related();`, ]); }); + + it('should migrate usages of the M2 theming APIs in a file with CRLF endings', async () => { + const result = await setup( + [ + `@use '@angular/material' as mat;`, + + `$my-primary: mat.define-palette(mat.$indigo-palette, 500);`, + `$my-accent: mat.define-palette(mat.$pink-palette, A200, A100, A400);`, + `$my-warn: mat.define-palette(mat.$red-palette);`, + + `$my-theme: mat.define-light-theme((`, + ` color: (`, + ` primary: $my-primary,`, + ` accent: $my-accent,`, + ` warn: $my-warn,`, + ` ),`, + ` typography: mat.define-typography-config(),`, + ` density: 0,`, + `));`, + `@include mat.all-component-themes($my-theme);`, + ].join('\r\n'), + ); + + expect(result.split('\r\n')).toEqual([ + `@use '@angular/material' as mat;`, + + `$my-primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500);`, + `$my-accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400);`, + `$my-warn: mat.m2-define-palette(mat.$m2-red-palette);`, + + `$my-theme: mat.m2-define-light-theme((`, + ` color: (`, + ` primary: $my-primary,`, + ` accent: $my-accent,`, + ` warn: $my-warn,`, + ` ),`, + ` typography: mat.m2-define-typography-config(),`, + ` density: 0,`, + `));`, + `@include mat.all-component-themes($my-theme);`, + ]); + }); });