Skip to content

Commit

Permalink
fix(material/schematics): theming API migration not working with CRLF…
Browse files Browse the repository at this point in the history
… line endings (#29171)

Fixes that the migration didn't account for CRLF line endings when figuring out the Sass namespace.

Fixes #29147.

(cherry picked from commit c9e1d4a)
  • Loading branch information
crisbeto committed Jun 3, 2024
1 parent 81e3e8e commit 8d44ed9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function extractNamespaceFromUseStatement(fullImport: string): string {
function getNamespaces(moduleName: string, content: string): string[] {
const namespaces = new Set<string>();
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))) {
Expand Down
42 changes: 42 additions & 0 deletions src/material/schematics/ng-update/test-cases/m2-theming.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);`,
]);
});
});

0 comments on commit 8d44ed9

Please sign in to comment.