Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update schematics to work with new UpdateBuffer #26504

Merged
merged 2 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ jobs:
# Setup the components repository to use the MDC snapshot builds.
# Run project tests with the MDC canary builds.
- run: bazel test --build_tag_filters=-docs-package,-e2e --test_tag_filters=-e2e --build_tests_only -- src/...
# The below tests should be removed when consuming Angular CLI version 16 which should contain
# https://github.com/angular/angular-cli/pull/24211
- run: bazel test //src/cdk/schematics/... //src/material/schematics/... --build_tests_only --test_env=NG_UPDATE_BUFFER_V2=1
- *slack_notify_on_failure

# ----------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class RuntimeCodeMigration extends Migration<ComponentMigrator[], Schemat
private _stylesMigration: ThemingStylesMigration;
private _templateMigration: TemplateMigration;
private _hasPossibleTemplateMigrations = true;
private _updates: {offset: number; update: () => void}[] | undefined;

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

this.fileSystem.edit(filePath).remove(start, width).insertRight(start, newNodeText);
this._updates ??= [];
this._updates.push({
offset: start,
update: () =>
this.fileSystem.edit(filePath).remove(start, width).insertRight(start, newNodeText),
});
}

override postAnalysis(): void {
// Apply the updates in reverse offset order this ensures that there are no
// overlapping changes which would caused a change in offsets.
this._updates?.sort((a, b) => b.offset - a.offset).forEach(({update}) => update());
}

/** Checks whether a specifier identifier is referring to an imported symbol. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {extname} from 'path';

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

override visitStylesheet(stylesheet: ResolvedResource): void {
const extension = extname(stylesheet.filePath).toLowerCase();
Expand Down Expand Up @@ -429,6 +430,17 @@ export class LegacyComponentsMigration extends Migration<null> {
str: {old: string; new: string},
): void {
const index = this.fileSystem.read(filePath)!.indexOf(str.old, offset);
this.fileSystem.edit(filePath).remove(index, str.old.length).insertRight(index, str.new);
this._updates ??= [];
this._updates.push({
offset: index,
update: () =>
this.fileSystem.edit(filePath).remove(index, str.old.length).insertRight(index, str.new),
});
}

override postAnalysis(): void {
// Apply the updates in reverse offset order this ensures that there are no
// overlapping changes which would caused a change in offsets.
this._updates?.sort((a, b) => b.offset - a.offset).forEach(({update}) => update());
}
}