Skip to content

Commit b666d2c

Browse files
thePunderWomanatscott
authored andcommitted
fix(migrations): fix common module removal (#56968)
This fixes the case that common module is removed on a second run of the migration. We were not looking at block parameters for common module usage. PR Close #56968
1 parent 634d55a commit b666d2c

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {Attribute, Element, ParseTreeResult, RecursiveVisitor, Text} from '@angular/compiler';
9+
import {
10+
Attribute,
11+
Block,
12+
Element,
13+
ParseTreeResult,
14+
RecursiveVisitor,
15+
Text,
16+
} from '@angular/compiler';
1017
import ts from 'typescript';
1118

1219
import {lookupIdentifiersInSourceFile} from './identifier-lookup';
@@ -377,6 +384,14 @@ export class CommonCollector extends RecursiveVisitor {
377384
super.visitElement(el, null);
378385
}
379386

387+
override visitBlock(ast: Block): void {
388+
for (const blockParam of ast.parameters) {
389+
if (this.hasPipes(blockParam.expression)) {
390+
this.count++;
391+
}
392+
}
393+
}
394+
380395
override visitText(ast: Text) {
381396
if (this.hasPipes(ast.value)) {
382397
this.count++;

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6243,6 +6243,54 @@ describe('control flow migration', () => {
62436243
expect(actual).toBe(expected);
62446244
});
62456245

6246+
it('should not remove common module when second run of migration and common module symbols are found', async () => {
6247+
writeFile(
6248+
'/comp.ts',
6249+
[
6250+
`import {Component} from '@angular/core';`,
6251+
`import {CommonModule} from '@angular/common';\n`,
6252+
`@Component({`,
6253+
` standalone: true`,
6254+
` selector: 'example-cmp',`,
6255+
` templateUrl: './comp.html',`,
6256+
` imports: [CommonModule],`,
6257+
`})`,
6258+
`export class ExampleCmp {`,
6259+
`}`,
6260+
].join('\n'),
6261+
);
6262+
6263+
writeFile(
6264+
'/comp.html',
6265+
[
6266+
`<div>`,
6267+
` @if (state$ | async; as state) {`,
6268+
` <div>`,
6269+
` <span>Content here {{state}}</span>`,
6270+
` </div>`,
6271+
` }`,
6272+
`</div>`,
6273+
].join('\n'),
6274+
);
6275+
6276+
await runMigration();
6277+
const actual = tree.readContent('/comp.ts');
6278+
const expected = [
6279+
`import {Component} from '@angular/core';`,
6280+
`import {CommonModule} from '@angular/common';\n`,
6281+
`@Component({`,
6282+
` standalone: true`,
6283+
` selector: 'example-cmp',`,
6284+
` templateUrl: './comp.html',`,
6285+
` imports: [CommonModule],`,
6286+
`})`,
6287+
`export class ExampleCmp {`,
6288+
`}`,
6289+
].join('\n');
6290+
6291+
expect(actual).toBe(expected);
6292+
});
6293+
62466294
it('should not remove imports when mismatch in counts', async () => {
62476295
writeFile(
62486296
'/comp.ts',

0 commit comments

Comments
 (0)