Skip to content

Commit aa285c5

Browse files
crisbetoatscott
authored andcommitted
fix(migrations): account for let declarations in control flow migration (#59861)
Fixes that the control flow migration wasn't accounting for `@let` when determining which symbols are used. PR Close #59861
1 parent 1119f85 commit aa285c5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
Attribute,
1111
Block,
1212
Element,
13+
LetDeclaration,
1314
ParseTreeResult,
1415
RecursiveVisitor,
1516
Text,
@@ -399,6 +400,13 @@ export class CommonCollector extends RecursiveVisitor {
399400
}
400401
}
401402

403+
override visitLetDeclaration(decl: LetDeclaration): void {
404+
if (this.hasPipes(decl.value)) {
405+
this.count++;
406+
}
407+
super.visitLetDeclaration(decl, null);
408+
}
409+
402410
private hasDirectives(input: string): boolean {
403411
return commonModuleDirectives.has(input);
404412
}

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6512,6 +6512,39 @@ describe('control flow migration', () => {
65126512

65136513
expect(actual).toBe(expected);
65146514
});
6515+
6516+
it('should not remove common module if symbols are used inside @let', async () => {
6517+
writeFile(
6518+
'/comp.ts',
6519+
[
6520+
`import {CommonModule} from '@angular/common';`,
6521+
`import {Component} from '@angular/core';\n`,
6522+
`@Component({`,
6523+
` imports: [CommonModule],`,
6524+
` template: \`@let foo = 123 | date; <span *ngIf="foo">{{foo}}</span>\``,
6525+
`})`,
6526+
`class Comp {`,
6527+
` toggle = false;`,
6528+
`}`,
6529+
].join('\n'),
6530+
);
6531+
6532+
await runMigration();
6533+
const actual = tree.readContent('/comp.ts');
6534+
const expected = [
6535+
`import {CommonModule} from '@angular/common';`,
6536+
`import {Component} from '@angular/core';\n`,
6537+
`@Component({`,
6538+
` imports: [CommonModule],`,
6539+
` template: \`@let foo = 123 | date; @if (foo) {<span>{{foo}}</span>}\``,
6540+
`})`,
6541+
`class Comp {`,
6542+
` toggle = false;`,
6543+
`}`,
6544+
].join('\n');
6545+
6546+
expect(actual).toBe(expected);
6547+
});
65156548
});
65166549

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

0 commit comments

Comments
 (0)