Skip to content

Commit 96ab999

Browse files
thePunderWomanalxhub
authored andcommitted
fix(migrations): CF Migration - ensure bound ngIfElse cases ignore line breaks (#53435)
When using ternaries or other expressions in bound if / else cases, it is possible that line breaks could end up affecting template replacement. fixes: #53428 PR Close #53435
1 parent c4d6dad commit 96ab999

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ function buildBoundIfElseBlock(etm: ElementToMigrate, tmpl: string, offset: numb
153153
} else if (aliases.length === 1) {
154154
condition += `; as ${aliases[0]}`;
155155
}
156-
const elsePlaceholder = ${etm.elseAttr!.value}δ`;
156+
const elsePlaceholder = ${etm.elseAttr!.value.trim()}δ`;
157157
if (etm.thenAttr !== undefined) {
158-
const thenPlaceholder = ${etm.thenAttr!.value}δ`;
158+
const thenPlaceholder = ${etm.thenAttr!.value.trim()}δ`;
159159
return buildIfThenElseBlock(etm, tmpl, condition, thenPlaceholder, elsePlaceholder, offset);
160160
}
161161
return buildIfElseBlock(etm, tmpl, condition, elsePlaceholder, offset);

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,6 +4201,39 @@ describe('control flow migration', () => {
42014201
`}\n`,
42024202
].join('\n'));
42034203
});
4204+
4205+
it('should trim newlines in ngIf conditions', async () => {
4206+
writeFile('/comp.ts', `
4207+
import {Component} from '@angular/core';
4208+
import {NgIf} from '@angular/common';
4209+
4210+
@Component({
4211+
templateUrl: './comp.html'
4212+
})
4213+
class Comp {
4214+
show = false;
4215+
}
4216+
`);
4217+
4218+
writeFile('/comp.html', [
4219+
`<ng-template`,
4220+
` [ngIf]="customClearTemplate"`,
4221+
` [ngIfElse]="`,
4222+
` isSidebarV3 || variant === 'v3' ? clearTemplateV3 : clearTemplate`,
4223+
` "`,
4224+
`></ng-template>`,
4225+
].join('\n'));
4226+
4227+
await runMigration();
4228+
const content = tree.readContent('/comp.html');
4229+
4230+
expect(content).toBe([
4231+
`@if (customClearTemplate) {`,
4232+
`} @else {`,
4233+
` <ng-template [ngTemplateOutlet]="isSidebarV3 || variant === 'v3' ? clearTemplateV3 : clearTemplate"></ng-template>`,
4234+
`}`,
4235+
].join('\n'));
4236+
});
42044237
});
42054238

42064239
describe('formatting', () => {

0 commit comments

Comments
 (0)