Skip to content

Commit 42d3ddd

Browse files
thePunderWomanalxhub
authored andcommitted
fix(migrations): Fix cf migration regular expression to include underscores (#54533)
In rare cases people may use an underscore in their component names, which was not accounted for in the formatting portion of the migration. fixes: #54532 PR Close #54533
1 parent 9ec2224 commit 42d3ddd

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,19 +652,19 @@ export function formatTemplate(tmpl: string, templateType: string): string {
652652

653653
// matches closing of an html element
654654
// </element>
655-
const closeElRegex = /\s*<\/([a-zA-Z0-9\-]+)*>/;
655+
const closeElRegex = /\s*<\/([a-zA-Z0-9\-_]+)*>/;
656656

657657
// matches closing of a self closing html element when the element is on multiple lines
658658
// [binding]="value" />
659-
const closeMultiLineElRegex = /^\s*([a-zA-Z0-9\-\[\]]+)?=?"?([^<]+)?"?\s?\/>$/;
659+
const closeMultiLineElRegex = /^\s*([a-zA-Z0-9\-_\[\]]+)?=?"?([^<]+)?"?\s?\/>$/;
660660

661661
// matches closing of a self closing html element when the element is on multiple lines
662662
// with no / in the closing: [binding]="value">
663-
const closeSelfClosingMultiLineRegex = /^\s*([a-zA-Z0-9\-\[\]]+)?=?"?([^\/<]+)?"?\s?>$/;
663+
const closeSelfClosingMultiLineRegex = /^\s*([a-zA-Z0-9\-_\[\]]+)?=?"?([^\/<]+)?"?\s?>$/;
664664

665665
// matches an open and close of an html element on a single line with no breaks
666666
// <div>blah</div>
667-
const singleLineElRegex = /\s*<([a-zA-Z0-9]+)(?![^>]*\/>)[^>]*>.*<\/([a-zA-Z0-9\-]+)*>/;
667+
const singleLineElRegex = /\s*<([a-zA-Z0-9]+)(?![^>]*\/>)[^>]*>.*<\/([a-zA-Z0-9\-_]+)*>/;
668668

669669
const lines = tmpl.split('\n');
670670
const formatted = [];

packages/core/schematics/test/control_flow_migration_spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4932,6 +4932,42 @@ describe('control flow migration', () => {
49324932

49334933
expect(actual).toBe(expected);
49344934
});
4935+
4936+
it('should handle dom nodes with underscores mixed in', async () => {
4937+
writeFile('/comp.ts', `
4938+
import {Component} from '@angular/core';
4939+
import {NgIf} from '@angular/common';
4940+
4941+
@Component({
4942+
templateUrl: './comp.html'
4943+
})
4944+
class Comp {
4945+
show = false;
4946+
}
4947+
`);
4948+
4949+
writeFile('/comp.html', [
4950+
`<div *ngIf="show">show</div>`,
4951+
`<a-very-long-component-name-that-has_underscores-too`,
4952+
` [selected]="selected | async "`,
4953+
`>`,
4954+
`</a-very-long-component-name-that-has_underscores-too>`,
4955+
].join('\n'));
4956+
4957+
await runMigration();
4958+
const actual = tree.readContent('/comp.html');
4959+
const expected = [
4960+
`@if (show) {`,
4961+
` <div>show</div>`,
4962+
`}`,
4963+
`<a-very-long-component-name-that-has_underscores-too`,
4964+
` [selected]="selected | async "`,
4965+
` >`,
4966+
`</a-very-long-component-name-that-has_underscores-too>`,
4967+
].join('\n');
4968+
4969+
expect(actual).toBe(expected);
4970+
});
49354971
});
49364972

49374973
describe('imports', () => {

0 commit comments

Comments
 (0)