Skip to content

Commit

Permalink
fix(migrations): CF Migration fix missing alias for bound ngifs
Browse files Browse the repository at this point in the history
Empty aliases are considered the item in an ngFor, and ngIf was skipping that value.

fixes: #53291
  • Loading branch information
thePunderWoman committed Nov 30, 2023
1 parent 1940280 commit 17aabd1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ function migrateNgIf(etm: ElementToMigrate, tmpl: string, offset: number): Resul
function buildIfBlock(etm: ElementToMigrate, tmpl: string, offset: number): Result {
const aliasAttrs = etm.aliasAttrs!;
const aliases = [...aliasAttrs.aliases.keys()];
if (aliasAttrs.item) {
aliases.push(aliasAttrs.item);
}

// includes the mandatory semicolon before as
const lbString = etm.hasLineBreaks ? '\n' : '';
Expand Down Expand Up @@ -133,6 +136,9 @@ function buildStandardIfElseBlock(
function buildBoundIfElseBlock(etm: ElementToMigrate, tmpl: string, offset: number): Result {
const aliasAttrs = etm.aliasAttrs!;
const aliases = [...aliasAttrs.aliases.keys()];
if (aliasAttrs.item) {
aliases.push(aliasAttrs.item);
}

// includes the mandatory semicolon before as
let condition = etm.attr.value.replace(' as ', '; as ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export class ElementCollector extends RecursiveVisitor {
const aliases = new Map<string, string>();
let item = '';
for (const attr of el.attrs) {
debugger;
if (attr.name.startsWith('let-')) {
if (attr.value === '') {
// item
Expand Down
29 changes: 29 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,35 @@ describe('control flow migration', () => {
].join('\n'));
});

it('should migrate an if case as a binding with let variable with no value', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgIf} from '@angular/common';
@Component({
templateUrl: './comp.html'
})
class Comp {
show = false;
}
`);

writeFile('/comp.html', [
`<ng-template [ngIf]="viewModel$ | async" let-vm>`,
` {{vm | json}}`,
`</ng-template>`,
].join('\n'));

await runMigration();
const content = tree.readContent('/comp.html');

expect(content).toBe([
`@if (viewModel$ | async; as vm) {`,
` {{vm | json}}`,
`}`,
].join('\n'));
});

it('should migrate an if else case as bindings', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
Expand Down

0 comments on commit 17aabd1

Please sign in to comment.