Skip to content

Commit

Permalink
fix(migrations): Fix cf migration regular expression to include under…
Browse files Browse the repository at this point in the history
…scores (#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
  • Loading branch information
thePunderWoman authored and alxhub committed Feb 21, 2024
1 parent ef94bb6 commit bb57d34
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -652,19 +652,19 @@ export function formatTemplate(tmpl: string, templateType: string): string {

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

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

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

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

const lines = tmpl.split('\n');
const formatted = [];
Expand Down
36 changes: 36 additions & 0 deletions packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4932,6 +4932,42 @@ describe('control flow migration', () => {

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

it('should handle dom nodes with underscores mixed in', 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', [
`<div *ngIf="show">show</div>`,
`<a-very-long-component-name-that-has_underscores-too`,
` [selected]="selected | async "`,
`>`,
`</a-very-long-component-name-that-has_underscores-too>`,
].join('\n'));

await runMigration();
const actual = tree.readContent('/comp.html');
const expected = [
`@if (show) {`,
` <div>show</div>`,
`}`,
`<a-very-long-component-name-that-has_underscores-too`,
` [selected]="selected | async "`,
` >`,
`</a-very-long-component-name-that-has_underscores-too>`,
].join('\n');

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

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

0 comments on commit bb57d34

Please sign in to comment.