Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Attribute,
Block,
Element,
LetDeclaration,
ParseTreeResult,
RecursiveVisitor,
Text,
Expand Down Expand Up @@ -390,6 +391,7 @@ export class CommonCollector extends RecursiveVisitor {
this.count++;
}
}
super.visitBlock(ast, null);
}

override visitText(ast: Text) {
Expand All @@ -398,6 +400,13 @@ export class CommonCollector extends RecursiveVisitor {
}
}

override visitLetDeclaration(decl: LetDeclaration): void {
if (this.hasPipes(decl.value)) {
this.count++;
}
super.visitLetDeclaration(decl, null);
}

private hasDirectives(input: string): boolean {
return commonModuleDirectives.has(input);
}
Expand Down
66 changes: 66 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6479,6 +6479,72 @@ describe('control flow migration', () => {

expect(actual).toBe(expected);
});

it('should not remove common module if symbols are used inside new control flow', async () => {
writeFile(
'/comp.ts',
[
`import {CommonModule} from '@angular/common';`,
`import {Component} from '@angular/core';\n`,
`@Component({`,
` imports: [CommonModule],`,
` template: \`@if (toggle) {<div>{{ d | date }}</div>} <span *ngIf="toggle">hi</span>\``,
`})`,
`class Comp {`,
` toggle = false;`,
`}`,
].join('\n'),
);

await runMigration();
const actual = tree.readContent('/comp.ts');
const expected = [
`import {CommonModule} from '@angular/common';`,
`import {Component} from '@angular/core';\n`,
`@Component({`,
` imports: [CommonModule],`,
` template: \`@if (toggle) {<div>{{ d | date }}</div>} @if (toggle) {<span>hi</span>}\``,
`})`,
`class Comp {`,
` toggle = false;`,
`}`,
].join('\n');

expect(actual).toBe(expected);
});

it('should not remove common module if symbols are used inside @let', async () => {
writeFile(
'/comp.ts',
[
`import {CommonModule} from '@angular/common';`,
`import {Component} from '@angular/core';\n`,
`@Component({`,
` imports: [CommonModule],`,
` template: \`@let foo = 123 | date; <span *ngIf="foo">{{foo}}</span>\``,
`})`,
`class Comp {`,
` toggle = false;`,
`}`,
].join('\n'),
);

await runMigration();
const actual = tree.readContent('/comp.ts');
const expected = [
`import {CommonModule} from '@angular/common';`,
`import {Component} from '@angular/core';\n`,
`@Component({`,
` imports: [CommonModule],`,
` template: \`@let foo = 123 | date; @if (foo) {<span>{{foo}}</span>}\``,
`})`,
`class Comp {`,
` toggle = false;`,
`}`,
].join('\n');

expect(actual).toBe(expected);
});
});

describe('no migration needed', () => {
Expand Down