Skip to content

Commit

Permalink
fix(@schematics/angular): allow dash in selector before a number
Browse files Browse the repository at this point in the history
This commit updates the validator regexp to allow a dash before a number.

Closes #25164

(cherry picked from commit c5827d4)
  • Loading branch information
clydin authored and alan-agius4 committed Oct 26, 2023
1 parent c49947d commit 2b91472
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/schematics/angular/component/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ describe('Component Schematic', () => {
).toBeRejectedWithError('Selector "app-1-one" is invalid.');
});

it('should allow dash in selector before a number', async () => {
const options = { ...defaultOptions, name: 'one-1' };

const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
expect(content).toMatch(/selector: 'app-one-1'/);
});

it('should allow dash in selector before a number and with a custom prefix', async () => {
const options = { ...defaultOptions, name: 'one-1', prefix: 'pre' };

const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/one-1/one-1.component.ts');
expect(content).toMatch(/selector: 'pre-one-1'/);
});

it('should allow dash in selector before a number and without a prefix', async () => {
const options = { ...defaultOptions, name: 'one-2', selector: 'one-2' };

const tree = await schematicRunner.runSchematic('component', options, appTree);
const content = tree.readContent('/projects/bar/src/app/one-2/one-2.component.ts');
expect(content).toMatch(/selector: 'one-2'/);
});

it('should use the default project prefix if none is passed', async () => {
const options = { ...defaultOptions, prefix: undefined };

Expand Down
3 changes: 2 additions & 1 deletion packages/schematics/angular/utility/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { SchematicsException } from '@angular-devkit/schematics';

// Must start with a letter, and must contain only alphanumeric characters or dashes.
// When adding a dash the segment after the dash must also start with a letter.
export const htmlSelectorRe = /^[a-zA-Z][.0-9a-zA-Z]*(:?-[a-zA-Z][.0-9a-zA-Z]*)*$/;
export const htmlSelectorRe =
/^[a-zA-Z][.0-9a-zA-Z]*((:?-[0-9]+)*|(:?-[a-zA-Z][.0-9a-zA-Z]*(:?-[0-9]+)*)*)$/;

// See: https://github.com/tc39/proposal-regexp-unicode-property-escapes/blob/fe6d07fad74cd0192d154966baa1e95e7cda78a1/README.md#other-examples
const ecmaIdentifierNameRegExp = /^(?:[$_\p{ID_Start}])(?:[$_\u200C\u200D\p{ID_Continue}])*$/u;
Expand Down

0 comments on commit 2b91472

Please sign in to comment.