Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(migrations): CF Migration - ensure bound ngIfElse cases ignore line breaks #53435

Closed
wants to merge 1 commit into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -153,9 +153,9 @@ function buildBoundIfElseBlock(etm: ElementToMigrate, tmpl: string, offset: numb
} else if (aliases.length === 1) {
condition += `; as ${aliases[0]}`;
}
const elsePlaceholder = `θ${etm.elseAttr!.value}δ`;
const elsePlaceholder = `θ${etm.elseAttr!.value.trim()}δ`;
if (etm.thenAttr !== undefined) {
const thenPlaceholder = `θ${etm.thenAttr!.value}δ`;
const thenPlaceholder = `θ${etm.thenAttr!.value.trim()}δ`;
return buildIfThenElseBlock(etm, tmpl, condition, thenPlaceholder, elsePlaceholder, offset);
}
return buildIfElseBlock(etm, tmpl, condition, elsePlaceholder, offset);
Expand Down
33 changes: 33 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Expand Up @@ -4201,6 +4201,39 @@ describe('control flow migration', () => {
`}\n`,
].join('\n'));
});

it('should trim newlines in ngIf conditions', 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]="customClearTemplate"`,
` [ngIfElse]="`,
` isSidebarV3 || variant === 'v3' ? clearTemplateV3 : clearTemplate`,
` "`,
`></ng-template>`,
].join('\n'));

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

expect(content).toBe([
`@if (customClearTemplate) {`,
`} @else {`,
` <ng-template [ngTemplateOutlet]="isSidebarV3 || variant === 'v3' ? clearTemplateV3 : clearTemplate"></ng-template>`,
`}`,
].join('\n'));
});
});

describe('formatting', () => {
Expand Down