Skip to content

Commit 4f9cef4

Browse files
wagnermacielmmalerba
authored andcommitted
refactor(material/schematics): create shared migration utilities folder (#25106)
* move update file content updating logic to migration-utilities
1 parent 8b14771 commit 4f9cef4

File tree

8 files changed

+93
-21
lines changed

8 files changed

+93
-21
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
load("//tools:defaults.bzl", "jasmine_node_test", "spec_bundle", "ts_library")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ts_library(
6+
name = "migration-utilities",
7+
srcs = glob(
8+
["**/*.ts"],
9+
exclude = ["**/*.spec.ts"],
10+
),
11+
deps = [],
12+
)
13+
14+
ts_library(
15+
name = "unit_tests_lib",
16+
testonly = True,
17+
srcs = glob(["**/*.spec.ts"] + ["rules/components/test-setup-helper.ts"]),
18+
deps = [
19+
":migration-utilities",
20+
"@npm//@types/jasmine",
21+
],
22+
)
23+
24+
spec_bundle(
25+
name = "unit_tests_bundle",
26+
# Exclude the `node` devkit entry-point to avoid bundling native modules like
27+
# `chokidar` and `fsevents`. These rely on native bindings and break with ESBuild.
28+
external = ["@angular-devkit/core/node"],
29+
platform = "node",
30+
deps = [":unit_tests_lib"],
31+
)
32+
33+
jasmine_node_test(
34+
name = "unit_tests",
35+
deps = [":unit_tests_bundle"],
36+
)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {writeUpdates} from './index';
2+
3+
describe('migration-utilities', () => {
4+
describe('writeUpdates', () => {
5+
it('should call update functions in the correct order', () => {
6+
const fn1 = jasmine.createSpy().and.returnValue('1');
7+
const fn2 = jasmine.createSpy().and.returnValue('2');
8+
const fn3 = jasmine.createSpy().and.returnValue('3');
9+
10+
const result = writeUpdates('0', [
11+
{offset: 1, updateFn: fn3},
12+
{offset: 2, updateFn: fn2},
13+
{offset: 3, updateFn: fn1},
14+
]);
15+
16+
expect(fn1).toHaveBeenCalledOnceWith('0');
17+
expect(fn2).toHaveBeenCalledOnceWith('1');
18+
expect(fn3).toHaveBeenCalledOnceWith('2');
19+
expect(result).toBe('3');
20+
});
21+
});
22+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/** Stores the data needed to make a single update to a file. */
10+
export interface Update {
11+
/** The start index of the location of the update. */
12+
offset: number;
13+
14+
/** A function to be used to update the file content. */
15+
updateFn: (html: string) => string;
16+
}
17+
18+
/** Applies the updates to the given file content in reverse offset order. */
19+
export function writeUpdates(content: string, updates: Update[]): string {
20+
updates.sort((a, b) => b.offset - a.offset);
21+
updates.forEach(update => (content = update.updateFn(content)));
22+
return content;
23+
}

src/material/schematics/ng-generate/mdc-migration/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ ts_library(
1515
),
1616
deps = [
1717
"//src/cdk/schematics",
18+
"//src/material/schematics/migration-utilities",
1819
"@npm//@angular-devkit/schematics",
1920
"@npm//@angular/compiler",
2021
"@npm//@types/node",

src/material/schematics/ng-generate/mdc-migration/rules/components/card/card-template.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88

99
import * as compiler from '@angular/compiler';
10-
import {TemplateMigrator, Update} from '../../template-migrator';
10+
import {TemplateMigrator} from '../../template-migrator';
1111
import {addAttribute, visitElements} from '../../tree-traversal';
12+
import {Update} from '../../../../../migration-utilities';
1213

1314
export class CardTemplateMigrator extends TemplateMigrator {
1415
getUpdates(ast: compiler.ParsedTemplate): Update[] {
@@ -20,7 +21,7 @@ export class CardTemplateMigrator extends TemplateMigrator {
2021
}
2122

2223
updates.push({
23-
location: node.startSourceSpan.end,
24+
offset: node.startSourceSpan.start.offset,
2425
updateFn: html => addAttribute(html, node, 'appearance', 'outlined'),
2526
});
2627
});

src/material/schematics/ng-generate/mdc-migration/rules/components/chips/chips-template.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
*/
88

99
import * as compiler from '@angular/compiler';
10-
import {TemplateMigrator, Update} from '../../template-migrator';
10+
import {TemplateMigrator} from '../../template-migrator';
1111
import {replaceStartTag, replaceEndTag, visitElements} from '../../tree-traversal';
12+
import {Update} from '../../../../../migration-utilities';
1213

1314
/** Stores a mat-chip-list with the mat-chip elements nested within it. */
1415
interface ChipMap {
@@ -91,11 +92,11 @@ export class ChipsTemplateMigrator extends TemplateMigrator {
9192
private _buildTagUpdates(node: compiler.TmplAstElement, tagName: string): Update[] {
9293
return [
9394
{
94-
location: node.startSourceSpan.start,
95+
offset: node.startSourceSpan.start.offset,
9596
updateFn: html => replaceStartTag(html, node, tagName),
9697
},
9798
{
98-
location: node.endSourceSpan!.start,
99+
offset: node.endSourceSpan!.start.offset,
99100
updateFn: html => replaceEndTag(html, node, tagName),
100101
},
101102
];

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {Migration, ResolvedResource} from '@angular/cdk/schematics';
1010
import {SchematicContext} from '@angular-devkit/schematics';
1111
import {parseTemplate} from './tree-traversal';
1212
import {ComponentMigrator} from '.';
13-
import {Update} from './template-migrator';
13+
import {Update, writeUpdates} from '../../../migration-utilities';
1414

1515
export class TemplateMigration extends Migration<ComponentMigrator[], SchematicContext> {
1616
enabled = true;
@@ -22,11 +22,7 @@ export class TemplateMigration extends Migration<ComponentMigrator[], SchematicC
2222
const updates: Update[] = [];
2323
migrators.forEach(m => updates.push(...m.getUpdates(ast)));
2424

25-
updates.sort((a, b) => b.location.offset - a.location.offset);
26-
updates.forEach(update => {
27-
template.content = update.updateFn(template.content);
28-
});
29-
30-
this.fileSystem.overwrite(template.filePath, template.content);
25+
const content = writeUpdates(template.content, updates);
26+
this.fileSystem.overwrite(template.filePath, content);
3127
}
3228
}

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77
*/
88

99
import * as compiler from '@angular/compiler';
10-
11-
/** Stores the data needed to make a template update. */
12-
export interface Update {
13-
/** The location of the update. */
14-
location: compiler.ParseLocation;
15-
16-
/** A function to be used to update the template. */
17-
updateFn: (html: string) => string;
18-
}
10+
import {Update} from '../../../migration-utilities';
1911

2012
export abstract class TemplateMigrator {
2113
/** Returns the data needed to update the given node. */

0 commit comments

Comments
 (0)