Skip to content

Commit f6ff503

Browse files
committed
refactor: update schematics to work with new UpdateBuffer (#26504)
* refactor: update schematics to work with new UpdateBuffer This change is needed for the upcoming changes in Angular CLI version 16 where the default `UpdateBuffer` is replaced with the `UpdateBuffer2`. This commits changes how certain updates are applied to avoid overlapping changes which would otherwise result in broken output due to the offset would be out of sync. * ci: add test step to test schematics with `UpdateBuffer2` This commit adds a new step in CI to tests schematics using the `UpdateBuffer2` which will be the default and only `UpdateBuffer` in Angular CLI vertsion 16. (cherry picked from commit c4414ad)
1 parent 05df9ba commit f6ff503

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,9 @@ jobs:
582582
# Setup the components repository to use the MDC snapshot builds.
583583
# Run project tests with the MDC canary builds.
584584
- run: bazel test --build_tag_filters=-docs-package,-e2e --test_tag_filters=-e2e --build_tests_only -- src/...
585+
# The below tests should be removed when consuming Angular CLI version 16 which should contain
586+
# https://github.com/angular/angular-cli/pull/24211
587+
- run: bazel test //src/cdk/schematics/... //src/material/schematics/... --build_tests_only --test_env=NG_UPDATE_BUFFER_V2=1
585588
- *slack_notify_on_failure
586589

587590
# ----------------------------------------------------------------------------------------

src/material/schematics/ng-generate/mdc-migration/rules/ts-migration/runtime-migration.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
2222
private _stylesMigration: ThemingStylesMigration;
2323
private _templateMigration: TemplateMigration;
2424
private _hasPossibleTemplateMigrations = true;
25+
private _updates: {offset: number; update: () => void}[] | undefined;
2526

2627
override visitNode(node: ts.Node): void {
2728
try {
@@ -337,7 +338,18 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
337338
const start = oldNode.getStart();
338339
const width = oldNode.getWidth();
339340

340-
this.fileSystem.edit(filePath).remove(start, width).insertRight(start, newNodeText);
341+
this._updates ??= [];
342+
this._updates.push({
343+
offset: start,
344+
update: () =>
345+
this.fileSystem.edit(filePath).remove(start, width).insertRight(start, newNodeText),
346+
});
347+
}
348+
349+
override postAnalysis(): void {
350+
// Apply the updates in reverse offset order this ensures that there are no
351+
// overlapping changes which would caused a change in offsets.
352+
this._updates?.sort((a, b) => b.offset - a.offset).forEach(({update}) => update());
341353
}
342354

343355
/** Checks whether a specifier identifier is referring to an imported symbol. */

src/material/schematics/ng-update/migrations/legacy-components-v15/index.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {extname} from 'path';
2323

2424
export class LegacyComponentsMigration extends Migration<null> {
2525
enabled = this.targetVersion === TargetVersion.V15;
26+
private _updates: {offset: number; update: () => void}[] | undefined;
2627

2728
override visitStylesheet(stylesheet: ResolvedResource): void {
2829
const extension = extname(stylesheet.filePath).toLowerCase();
@@ -429,6 +430,17 @@ export class LegacyComponentsMigration extends Migration<null> {
429430
str: {old: string; new: string},
430431
): void {
431432
const index = this.fileSystem.read(filePath)!.indexOf(str.old, offset);
432-
this.fileSystem.edit(filePath).remove(index, str.old.length).insertRight(index, str.new);
433+
this._updates ??= [];
434+
this._updates.push({
435+
offset: index,
436+
update: () =>
437+
this.fileSystem.edit(filePath).remove(index, str.old.length).insertRight(index, str.new),
438+
});
439+
}
440+
441+
override postAnalysis(): void {
442+
// Apply the updates in reverse offset order this ensures that there are no
443+
// overlapping changes which would caused a change in offsets.
444+
this._updates?.sort((a, b) => b.offset - a.offset).forEach(({update}) => update());
433445
}
434446
}

0 commit comments

Comments
 (0)