Skip to content

Commit 2f9d94b

Browse files
crisbetodylhunn
authored andcommitted
fix(migrations): account for variables in imports initializer (#55081)
Fixes that the control flow migration was throwing an error if the `imports` of a component are initialized to an identifier. Fixes #55080. PR Close #55081
1 parent ee76001 commit 2f9d94b

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,16 @@ function updateImportClause(clause: ts.ImportClause, removeCommonModule: boolean
8484
function updateClassImports(
8585
propAssignment: ts.PropertyAssignment, removeCommonModule: boolean): string|null {
8686
const printer = ts.createPrinter();
87-
const importList = propAssignment.initializer as ts.ArrayLiteralExpression;
87+
const importList = propAssignment.initializer;
88+
89+
// Can't change non-array literals.
90+
if (!ts.isArrayLiteralExpression(importList)) {
91+
return null;
92+
}
93+
8894
const removals = removeCommonModule ? importWithCommonRemovals : importRemovals;
89-
const elements = importList.elements.filter(el => !removals.includes(el.getText()));
95+
const elements =
96+
importList.elements.filter(el => !ts.isIdentifier(el) || !removals.includes(el.text));
9097
if (elements.length === importList.elements.length) {
9198
// nothing changed
9299
return null;

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5442,6 +5442,71 @@ describe('control flow migration', () => {
54425442

54435443
expect(actual).toBe(expected);
54445444
});
5445+
5446+
it('should not modify `imports` initialized to a variable reference', async () => {
5447+
writeFile('/comp.ts', [
5448+
`import {Component} from '@angular/core';`,
5449+
`import {CommonModule} from '@angular/common';\n`,
5450+
`const IMPORTS = [CommonModule];\n`,
5451+
`@Component({`,
5452+
` imports: IMPORTS,`,
5453+
` template: '<span *ngIf="show">Content here</span>',`,
5454+
`})`,
5455+
`class Comp {`,
5456+
` show = false;`,
5457+
`}`,
5458+
].join('\n'));
5459+
5460+
await runMigration();
5461+
const actual = tree.readContent('/comp.ts');
5462+
const expected = [
5463+
`import {Component} from '@angular/core';`,
5464+
`import {CommonModule} from '@angular/common';\n`,
5465+
`const IMPORTS = [CommonModule];\n`,
5466+
`@Component({`,
5467+
` imports: IMPORTS,`,
5468+
` template: '@if (show) {<span>Content here</span>}',`,
5469+
`})`,
5470+
`class Comp {`,
5471+
` show = false;`,
5472+
`}`,
5473+
].join('\n');
5474+
5475+
expect(actual).toBe(expected);
5476+
});
5477+
5478+
it('should handle spread elements in the `imports` array', async () => {
5479+
writeFile('/comp.ts', [
5480+
`import {Component} from '@angular/core';`,
5481+
`import {CommonModule} from '@angular/common';\n`,
5482+
`const BEFORE = [];\n`,
5483+
`const AFTER = [];\n`,
5484+
`@Component({`,
5485+
` imports: [...BEFORE, CommonModule, ...AFTER],`,
5486+
` template: '<span *ngIf="show">Content here</span>',`,
5487+
`})`,
5488+
`class Comp {`,
5489+
` show = false;`,
5490+
`}`,
5491+
].join('\n'));
5492+
5493+
await runMigration();
5494+
const actual = tree.readContent('/comp.ts');
5495+
const expected = [
5496+
`import {Component} from '@angular/core';\n\n`,
5497+
`const BEFORE = [];\n`,
5498+
`const AFTER = [];\n`,
5499+
`@Component({`,
5500+
` imports: [...BEFORE, ...AFTER],`,
5501+
` template: '@if (show) {<span>Content here</span>}',`,
5502+
`})`,
5503+
`class Comp {`,
5504+
` show = false;`,
5505+
`}`,
5506+
].join('\n');
5507+
5508+
expect(actual).toBe(expected);
5509+
});
54455510
});
54465511

54475512
describe('no migration needed', () => {

0 commit comments

Comments
 (0)