Skip to content

Commit 19d0b36

Browse files
authored
fix(material/schematics): files with inline resources overwritten by ng-generate (#25744)
The `ng generate` MDC migration schematics was assuming that `visitTemplate` and `visitStylesheet` will be called only on external resources and it was overwriting the entire file content which was breaking files that have components with inline resources. These changes edit the file only within the template/stylesheet span.
1 parent 9dbc4fd commit 19d0b36

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
22
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3-
import {createNewTestRunner, migrateComponents, THEME_FILE} from '../test-setup-helper';
3+
import {
4+
APP_MODULE_FILE,
5+
createNewTestRunner,
6+
migrateComponents,
7+
THEME_FILE,
8+
} from '../test-setup-helper';
49

510
describe('card styles', () => {
611
let runner: SchematicTestRunner;
@@ -215,5 +220,31 @@ describe('card styles', () => {
215220
`,
216221
);
217222
});
223+
224+
it('should migrate inline styles', async () => {
225+
const oldContent = `
226+
import {Component} from '@angular/core';
227+
228+
@Component({
229+
template: '',
230+
styles: ['.mat-card { color: red; }'],
231+
})
232+
export class MyComp {}
233+
`;
234+
235+
const newContent = `
236+
import {Component} from '@angular/core';
237+
238+
@Component({
239+
template: '',
240+
styles: ['.mat-mdc-card { color: red; }'],
241+
})
242+
export class MyComp {}
243+
`;
244+
245+
cliAppTree.overwrite(APP_MODULE_FILE, oldContent);
246+
const tree = await migrateComponents(['card'], runner, cliAppTree);
247+
expect(tree.readContent(APP_MODULE_FILE)).toBe(newContent);
248+
});
218249
});
219250
});

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import {createTestApp, patchDevkitTreeToExposeTypeScript} from '@angular/cdk/schematics/testing';
22
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing';
3-
import {createNewTestRunner, migrateComponents, TEMPLATE_FILE} from '../test-setup-helper';
3+
import {
4+
APP_MODULE_FILE,
5+
createNewTestRunner,
6+
migrateComponents,
7+
TEMPLATE_FILE,
8+
} from '../test-setup-helper';
49

510
describe('card template migrator', () => {
611
let runner: SchematicTestRunner;
@@ -110,4 +115,28 @@ describe('card template migrator', () => {
110115
`,
111116
);
112117
});
118+
119+
it('should migrate inline templates', async () => {
120+
const oldContent = `
121+
import {Component} from '@angular/core';
122+
123+
@Component({
124+
template: '<mat-card></mat-card>'
125+
})
126+
export class MyComp {}
127+
`;
128+
129+
const newContent = `
130+
import {Component} from '@angular/core';
131+
132+
@Component({
133+
template: '<mat-card appearance="outlined"></mat-card>'
134+
})
135+
export class MyComp {}
136+
`;
137+
138+
cliAppTree.overwrite(APP_MODULE_FILE, oldContent);
139+
const tree = await migrateComponents(['card'], runner, cliAppTree);
140+
expect(tree.readContent(APP_MODULE_FILE)).toBe(newContent);
141+
});
113142
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export class TemplateMigration extends Migration<ComponentMigrator[], SchematicC
1616
enabled = true;
1717

1818
override visitTemplate(template: ResolvedResource) {
19-
this.fileSystem.overwrite(template.filePath, this.migrate(template.content, template.filePath));
19+
this.fileSystem
20+
.edit(template.filePath)
21+
.remove(template.start, template.content.length)
22+
.insertRight(template.start, this.migrate(template.content, template.filePath));
2023
}
2124

2225
migrate(template: string, templateUrl?: string): string {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export class ThemingStylesMigration extends Migration<ComponentMigrator[], Schem
1717
namespace: string;
1818

1919
override visitStylesheet(stylesheet: ResolvedResource) {
20-
this.fileSystem.overwrite(stylesheet.filePath, this.migrate(stylesheet.content));
20+
this.fileSystem
21+
.edit(stylesheet.filePath)
22+
.remove(stylesheet.start, stylesheet.content.length)
23+
.insertRight(stylesheet.start, this.migrate(stylesheet.content));
2124
}
2225

2326
migrate(styles: string): string {

0 commit comments

Comments
 (0)