Skip to content

Commit

Permalink
fix(material/schematics): error if stylesheet contains syntax errors
Browse files Browse the repository at this point in the history
Fixes that the `mat.core` migration was breaking the whole update process if a stylesheet has syntax errors.

Fixes #30115.

(cherry picked from commit c891926)
  • Loading branch information
crisbeto committed Dec 2, 2024
1 parent 0ed9869 commit 5b3350a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
25 changes: 16 additions & 9 deletions src/material/schematics/ng-update/migrations/mat-core-removal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ export class MatCoreMigration extends Migration<UpgradeData, DevkitContext> {
}

override visitStylesheet(stylesheet: ResolvedResource): void {
const processor = new postcss.Processor([
{
postcssPlugin: 'mat-core-removal-v19-plugin',
AtRule: {
use: node => this._getNamespace(node),
include: node => this._handleAtInclude(node, stylesheet.filePath),
try {
const processor = new postcss.Processor([
{
postcssPlugin: 'mat-core-removal-v19-plugin',
AtRule: {
use: node => this._getNamespace(node),
include: node => this._handleAtInclude(node, stylesheet.filePath),
},
},
},
]);
processor.process(stylesheet.content, {syntax: scss}).sync();
]);
processor.process(stylesheet.content, {syntax: scss}).sync();
} catch (e) {
this.logger.warn(
`Failed to migrate usages of mat.core in ${stylesheet.filePath} due to error:`,
);
this.logger.warn(e + '');
}
}

/** Handles updating the at-include rules of uses of the core mixin. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {MIGRATION_PATH} from '../../paths';
const PROJECT_ROOT_DIR = '/projects/cdk-testing';
const THEME_FILE_PATH = join(PROJECT_ROOT_DIR, 'src/theme.scss');

describe('v15 legacy components migration', () => {
describe('v19 mat.core migration', () => {
let tree: UnitTestTree;

/** Writes multiple lines to a file. */
Expand Down Expand Up @@ -73,5 +73,12 @@ describe('v15 legacy components migration', () => {
],
});
});

it('should not break if there is an invalid syntax', async () => {
await runSassMigrationTest('', {
old: [`@use '@angular/material' as mat;`, `.foo { content: '; }`],
new: [`@use '@angular/material' as mat;`, `.foo { content: '; }`],
});
});
});
});

0 comments on commit 5b3350a

Please sign in to comment.