Skip to content

Commit b9163f3

Browse files
MeAkibclydin
authored andcommitted
fix(@schematics/angular): use the generated class name when declaring in an NgModule
With `addTypeToClassName: false`, the component and directive schematics name the class without the type, but the NgModule declaration was added with the type appended (e.g. `FooComponent`), so the module no longer compiled. Honour `addTypeToClassName` when computing the declared symbol. Fixes #34447
1 parent 8aa16b6 commit b9163f3

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎packages/schematics/angular/component/index_spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,25 @@ describe('Component Schematic', () => {
510510
expect(appModule).toMatch(/import { FooComponent } from '.\/foo\/foo.component'/);
511511
});
512512

513+
it('should declare the class name without the type when addTypeToClassName is false', async () => {
514+
const options = {
515+
...defaultNonStandaloneOptions,
516+
module: 'app-module.ts',
517+
addTypeToClassName: false,
518+
export: true,
519+
};
520+
521+
const tree = await schematicRunner.runSchematic('component', options, appTree);
522+
const component = tree.readContent('/projects/baz/src/app/foo/foo.component.ts');
523+
const appModule = tree.readContent('/projects/baz/src/app/app-module.ts');
524+
525+
expect(component).toContain('export class Foo {');
526+
expect(appModule).toMatch(/import { Foo } from '.\/foo\/foo.component'/);
527+
expect(appModule).toMatch(/declarations: \[\n\s*App,\n\s*Foo\n/);
528+
expect(appModule).toMatch(/exports: \[\n(\s*) {2}Foo\n/);
529+
expect(appModule).not.toContain('FooComponent');
530+
});
531+
513532
it('should respect the sourceRoot value', async () => {
514533
const config = JSON.parse(appTree.readContent('/angular.json'));
515534
config.projects.baz.sourceRoot = 'projects/baz/custom';

‎packages/schematics/angular/directive/index_spec.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,23 @@ describe('Directive Schematic', () => {
255255
expect(appModule).toMatch(/import { Foo } from '.\/foo'/);
256256
});
257257

258+
it('should declare the class name without the type when addTypeToClassName is false', async () => {
259+
const options = {
260+
...defaultNonStandaloneOptions,
261+
module: 'app-module.ts',
262+
type: 'directive',
263+
addTypeToClassName: false,
264+
};
265+
266+
const tree = await schematicRunner.runSchematic('directive', options, appTree);
267+
const directive = tree.readContent('/projects/baz/src/app/foo.directive.ts');
268+
const appModule = tree.readContent('/projects/baz/src/app/app-module.ts');
269+
270+
expect(directive).toContain('export class Foo {');
271+
expect(appModule).toMatch(/import { Foo } from '.\/foo.directive'/);
272+
expect(appModule).not.toContain('FooDirective');
273+
});
274+
258275
it('should fail if specified module does not exist', async () => {
259276
const options = {
260277
...defaultNonStandaloneOptions,

‎packages/schematics/angular/utility/add-declaration-to-ng-module.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export interface DeclarationToNgModuleOptions {
2020
export?: boolean;
2121
type: string;
2222
typeSeparator?: '.' | '-';
23+
24+
/** Whether the type is part of the class name. Defaults to `true`. */
25+
addTypeToClassName?: boolean;
2326
skipImport?: boolean;
2427
standalone?: boolean;
2528
}
@@ -44,7 +47,8 @@ export function addDeclarationToNgModule(options: DeclarationToNgModuleOptions):
4447

4548
const importPath = buildRelativePath(modulePath, filePath);
4649
const classifiedName =
47-
strings.classify(options.name) + (options.type ? strings.classify(options.type) : '');
50+
strings.classify(options.name) +
51+
(options.type && options.addTypeToClassName !== false ? strings.classify(options.type) : '');
4852
const changes = addDeclarationToModule(source, modulePath, classifiedName, importPath);
4953

5054
if (options.export) {

0 commit comments

Comments
 (0)