Skip to content

Commit 79f7915

Browse files
thePunderWomanalxhub
authored andcommitted
fix(migrations): fix cf migration import removal when errors occur (#53502)
When migrating a component and the associated external template, if errors occur, the component should not remove the common module imports. This fix should allow the application to still build in that instance. PR Close #53502
1 parent e1d01c7 commit 79f7915

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

packages/core/schematics/ng-generate/control-flow-migration/migration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function migrateTemplate(
4040
migrated = formatTemplate(migrated, templateType);
4141
}
4242
file.removeCommonModule = canRemoveCommonModule(template);
43+
file.canRemoveImports = true;
4344

4445
// when migrating an external template, we have to pass back
4546
// whether it's safe to remove the CommonModule to the
@@ -48,6 +49,7 @@ export function migrateTemplate(
4849
analyzedFiles.has(file.sourceFilePath)) {
4950
const componentFile = analyzedFiles.get(file.sourceFilePath)!;
5051
componentFile.removeCommonModule = file.removeCommonModule;
52+
componentFile.canRemoveImports = file.canRemoveImports;
5153
}
5254

5355
errors = [
@@ -56,7 +58,7 @@ export function migrateTemplate(
5658
...switchResult.errors,
5759
...caseResult.errors,
5860
];
59-
} else {
61+
} else if (file.canRemoveImports) {
6062
migrated = removeImports(template, node, file.removeCommonModule);
6163
}
6264

packages/core/schematics/ng-generate/control-flow-migration/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ export class Template {
240240
export class AnalyzedFile {
241241
private ranges: Range[] = [];
242242
removeCommonModule = false;
243+
canRemoveImports = false;
243244
sourceFilePath: string = '';
244245

245246
/** Returns the ranges in the order in which they should be migrated. */

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4638,6 +4638,55 @@ describe('control flow migration', () => {
46384638
expect(actual).toBe(expected);
46394639
});
46404640

4641+
it('should not remove common module imports post migration if errors prevented migrating the external template file',
4642+
async () => {
4643+
writeFile('/comp.ts', [
4644+
`import {Component} from '@angular/core';`,
4645+
`import {NgIf} from '@angular/common';`,
4646+
`@Component({`,
4647+
` imports: [NgIf],`,
4648+
` templateUrl: './comp.html',`,
4649+
`})`,
4650+
`class Comp {`,
4651+
` toggle = false;`,
4652+
`}`,
4653+
].join('\n'));
4654+
4655+
writeFile('/comp.html', [
4656+
`<div>`,
4657+
` <span *ngIf="toggle; else elseTmpl">shrug</span>`,
4658+
`</div>`,
4659+
`<ng-template #elseTmpl>else content</ng-template>`,
4660+
`<ng-template #elseTmpl>different</ng-template>`,
4661+
].join('\n'));
4662+
4663+
await runMigration();
4664+
const actualCmp = tree.readContent('/comp.ts');
4665+
const expectedCmp = [
4666+
`import {Component} from '@angular/core';`,
4667+
`import {NgIf} from '@angular/common';`,
4668+
`@Component({`,
4669+
` imports: [NgIf],`,
4670+
` templateUrl: './comp.html',`,
4671+
`})`,
4672+
`class Comp {`,
4673+
` toggle = false;`,
4674+
`}`,
4675+
].join('\n');
4676+
const actualTemplate = tree.readContent('/comp.html');
4677+
4678+
const expectedTemplate = [
4679+
`<div>`,
4680+
` <span *ngIf="toggle; else elseTmpl">shrug</span>`,
4681+
`</div>`,
4682+
`<ng-template #elseTmpl>else content</ng-template>`,
4683+
`<ng-template #elseTmpl>different</ng-template>`,
4684+
].join('\n');
4685+
4686+
expect(actualCmp).toBe(expectedCmp);
4687+
expect(actualTemplate).toBe(expectedTemplate);
4688+
});
4689+
46414690
it('should not remove common module imports post migration if other items used', async () => {
46424691
writeFile('/comp.ts', [
46434692
`import {CommonModule} from '@angular/common';`,

0 commit comments

Comments
 (0)