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
This commit is contained in:
Jessica Janiuk 2024-07-12 11:46:28 -04:00 committed by Andrew Scott
parent 38b93201c5
commit 0ea6a4a361
2 changed files with 64 additions and 1 deletions

View file

@ -6,7 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Attribute, Element, ParseTreeResult, RecursiveVisitor, Text} from '@angular/compiler';
import {
Attribute,
Block,
Element,
ParseTreeResult,
RecursiveVisitor,
Text,
} from '@angular/compiler';
import ts from 'typescript';
import {lookupIdentifiersInSourceFile} from './identifier-lookup';
@ -377,6 +384,14 @@ export class CommonCollector extends RecursiveVisitor {
super.visitElement(el, null);
}
override visitBlock(ast: Block): void {
for (const blockParam of ast.parameters) {
if (this.hasPipes(blockParam.expression)) {
this.count++;
}
}
}
override visitText(ast: Text) {
if (this.hasPipes(ast.value)) {
this.count++;

View file

@ -6243,6 +6243,54 @@ describe('control flow migration', () => {
expect(actual).toBe(expected);
});
it('should not remove common module when second run of migration and common module symbols are found', async () => {
writeFile(
'/comp.ts',
[
`import {Component} from '@angular/core';`,
`import {CommonModule} from '@angular/common';\n`,
`@Component({`,
` standalone: true`,
` selector: 'example-cmp',`,
` templateUrl: './comp.html',`,
` imports: [CommonModule],`,
`})`,
`export class ExampleCmp {`,
`}`,
].join('\n'),
);
writeFile(
'/comp.html',
[
`<div>`,
` @if (state$ | async; as state) {`,
` <div>`,
` <span>Content here {{state}}</span>`,
` </div>`,
` }`,
`</div>`,
].join('\n'),
);
await runMigration();
const actual = tree.readContent('/comp.ts');
const expected = [
`import {Component} from '@angular/core';`,
`import {CommonModule} from '@angular/common';\n`,
`@Component({`,
` standalone: true`,
` selector: 'example-cmp',`,
` templateUrl: './comp.html',`,
` imports: [CommonModule],`,
`})`,
`export class ExampleCmp {`,
`}`,
].join('\n');
expect(actual).toBe(expected);
});
it('should not remove imports when mismatch in counts', async () => {
writeFile(
'/comp.ts',