Skip to content

Commit

Permalink
fix(@schematics/angular): add validation for component and directive …
Browse files Browse the repository at this point in the history
…class name

(cherry picked from commit 6dbfc77)
  • Loading branch information
harshlakhara authored and alan-agius4 committed Oct 4, 2024
1 parent d21d1e6 commit b522002
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/schematics/angular/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
import { addDeclarationToNgModule } from '../utility/add-declaration-to-ng-module';
import { findModuleFromOptions } from '../utility/find-module';
import { parseName } from '../utility/parse-name';
import { validateHtmlSelector } from '../utility/validation';
import { validateClassName, validateHtmlSelector } from '../utility/validation';
import { buildDefaultPath, getWorkspace } from '../utility/workspace';
import { Schema as ComponentOptions, Style } from './schema';

Expand Down Expand Up @@ -62,6 +62,7 @@ export default function (options: ComponentOptions): Rule {
options.selector || buildSelector(options, (project && project.prefix) || '');

validateHtmlSelector(options.selector);
validateClassName(strings.classify(options.name));

const skipStyleFile = options.inlineStyle || options.style === Style.None;
const templateSource = apply(url('./files'), [
Expand Down
8 changes: 8 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,14 @@ describe('Component Schematic', () => {
).toBeRejectedWithError('Selector "app-1-one" is invalid.');
});

it('should error when class name contains invalid characters', async () => {
const options = { ...defaultOptions, name: '404' };

await expectAsync(
schematicRunner.runSchematic('component', options, appTree),
).toBeRejectedWithError('Class name "404" is invalid.');
});

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

Expand Down
3 changes: 2 additions & 1 deletion packages/schematics/angular/directive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { addDeclarationToNgModule } from '../utility/add-declaration-to-ng-module';
import { findModuleFromOptions } from '../utility/find-module';
import { parseName } from '../utility/parse-name';
import { validateHtmlSelector } from '../utility/validation';
import { validateClassName, validateHtmlSelector } from '../utility/validation';
import { buildDefaultPath, getWorkspace } from '../utility/workspace';
import { Schema as DirectiveOptions } from './schema';

Expand Down Expand Up @@ -58,6 +58,7 @@ export default function (options: DirectiveOptions): Rule {
options.selector = options.selector || buildSelector(options, project.prefix || '');

validateHtmlSelector(options.selector);
validateClassName(strings.classify(options.name));

const templateSource = apply(url('./files'), [
options.skipTests ? filter((path) => !path.endsWith('.spec.ts.template')) : noop(),
Expand Down
8 changes: 8 additions & 0 deletions packages/schematics/angular/directive/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ describe('Directive Schematic', () => {
expect(directiveContent).toContain('class FooDirective');
});

it('should error when class name contains invalid characters', async () => {
const options = { ...defaultOptions, name: '404' };

await expectAsync(
schematicRunner.runSchematic('component', options, appTree),
).toBeRejectedWithError('Class name "404" is invalid.');
});

describe('standalone=false', () => {
const defaultNonStandaloneOptions: DirectiveOptions = {
...defaultOptions,
Expand Down

0 comments on commit b522002

Please sign in to comment.