Skip to content

Commit 23f35e6

Browse files
authored
feat(angular): generate directives without a .directive/Directive suffix/type (#31236)
- Update generators to generate directives without the `.directive`/`Directive` suffix/type by default for Angular v20 - Keep the ability to generate directives with the `.directive`/`Directive` suffix/type by providing the `type` option for the `@nx/angular:directive` and `@nx/angular:scam-directive` generators - When the workspace uses a version lower than v20, the generators will still generate directives with the `.directive`/`Directive` suffix/type by default Note: a migration will be provided in a separate PR so existing workspaces continue generating directives with the `.directive`/`Directive` suffix/type.
1 parent 3b14976 commit 23f35e6

File tree

12 files changed

+317
-133
lines changed

12 files changed

+317
-133
lines changed

docs/generated/packages/angular/generators/directive.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
"command": "nx g @nx/angular:directive mylib/src/lib/foo.directive.ts"
1616
},
1717
{
18-
"description": "Generate a directive without providing the file extension. It results in the directive `FooDirective` at `mylib/src/lib/foo.directive.ts`",
18+
"description": "Generate a directive without providing the file extension. It results in the directive `Foo` at `mylib/src/lib/foo.ts`",
1919
"command": "nx g @nx/angular:directive mylib/src/lib/foo"
2020
},
2121
{
22-
"description": "Generate a directive with the exported symbol different from the file name. It results in the directive `CustomDirective` at `mylib/src/lib/foo.directive.ts`",
22+
"description": "Generate a directive with a given type/suffix. It results in the directive `FooDirective` at `mylib/src/lib/foo.directive.ts`",
23+
"command": "nx g @nx/angular:directive mylib/src/lib/foo --type=directive"
24+
},
25+
{
26+
"description": "Generate a directive with the exported symbol different from the file name. It results in the directive `Custom` at `mylib/src/lib/foo.ts`",
2327
"command": "nx g @nx/angular:directive mylib/src/lib/foo --name=custom"
2428
}
2529
],
@@ -73,6 +77,10 @@
7377
"default": false,
7478
"description": "The declaring NgModule exports this directive."
7579
},
80+
"type": {
81+
"type": "string",
82+
"description": "Append a custom type to the directive's filename. It defaults to 'directive' for Angular versions below v20. For Angular v20 and above, no type is appended unless specified."
83+
},
7684
"skipFormat": {
7785
"type": "boolean",
7886
"default": false,

docs/generated/packages/angular/generators/scam-directive.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
"command": "nx g @nx/angular:scam-directive mylib/src/lib/foo.directive.ts"
1414
},
1515
{
16-
"description": "Generate a directive without providing the file extension. It results in the directive `FooDirective` at `mylib/src/lib/foo.directive.ts`",
16+
"description": "Generate a directive without providing the file extension. It results in the directive `Foo` at `mylib/src/lib/foo.ts`",
1717
"command": "nx g @nx/angular:scam-directive mylib/src/lib/foo"
1818
},
1919
{
20-
"description": "Generate a directive with the exported symbol different from the file name. It results in the directive `CustomDirective` at `mylib/src/lib/foo.directive.ts`",
20+
"description": "Generate a directive with a given type/suffix. It results in the directive `FooDirective` at `mylib/src/lib/foo.directive.ts`",
21+
"command": "nx g @nx/angular:scam-directive mylib/src/lib/foo --type=directive"
22+
},
23+
{
24+
"description": "Generate a directive with the exported symbol different from the file name. It results in the directive `Custom` at `mylib/src/lib/foo.ts`",
2125
"command": "nx g @nx/angular:scam-directive mylib/src/lib/foo --name=custom"
2226
}
2327
],
@@ -65,6 +69,10 @@
6569
"default": true,
6670
"x-priority": "important"
6771
},
72+
"type": {
73+
"type": "string",
74+
"description": "Append a custom type to the directive's filename. It defaults to 'directive' for Angular versions below v20. For Angular v20 and above, no type is appended unless specified."
75+
},
6876
"skipFormat": {
6977
"description": "Skip formatting files.",
7078
"type": "boolean",

packages/angular/src/generators/directive/__snapshots__/directive.spec.ts.snap

+25-25
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
exports[`directive generator --no-standalone should export the directive correctly when directory is nested deeper 1`] = `
44
"import { NgModule } from '@angular/core';
5-
import { TestDirective } from './my-directives/test/test.directive';
5+
import { Test } from './my-directives/test/test';
66
@NgModule({
77
imports: [],
8-
declarations: [TestDirective],
9-
exports: [TestDirective],
8+
declarations: [Test],
9+
exports: [Test],
1010
})
1111
export class TestModule {}
1212
"
@@ -19,18 +19,18 @@ exports[`directive generator --no-standalone should generate a directive with te
1919
selector: '[test]',
2020
standalone: false,
2121
})
22-
export class TestDirective {
22+
export class Test {
2323
constructor() {}
2424
}
2525
"
2626
`;
2727

2828
exports[`directive generator --no-standalone should generate a directive with test files and attach to the NgModule automatically 2`] = `
29-
"import { TestDirective } from './test.directive';
29+
"import { Test } from './test';
3030
31-
describe('TestDirective', () => {
31+
describe('Test', () => {
3232
it('should create an instance', () => {
33-
const directive = new TestDirective();
33+
const directive = new Test();
3434
expect(directive).toBeTruthy();
3535
});
3636
});
@@ -39,10 +39,10 @@ describe('TestDirective', () => {
3939

4040
exports[`directive generator --no-standalone should generate a directive with test files and attach to the NgModule automatically 3`] = `
4141
"import { NgModule } from '@angular/core';
42-
import { TestDirective } from './test.directive';
42+
import { Test } from './test';
4343
@NgModule({
4444
imports: [],
45-
declarations: [TestDirective],
45+
declarations: [Test],
4646
exports: [],
4747
})
4848
export class TestModule {}
@@ -55,10 +55,10 @@ exports[`directive generator --no-standalone should import the directive correct
5555

5656
exports[`directive generator --no-standalone should import the directive correctly 3`] = `
5757
"import { NgModule } from '@angular/core';
58-
import { TestDirective } from './test.directive';
58+
import { Test } from './test';
5959
@NgModule({
6060
imports: [],
61-
declarations: [TestDirective],
61+
declarations: [Test],
6262
exports: [],
6363
})
6464
export class TestModule {}
@@ -72,18 +72,18 @@ exports[`directive generator --no-standalone should import the directive correct
7272
selector: '[test]',
7373
standalone: false
7474
})
75-
export class TestDirective {
75+
export class Test {
7676
constructor() {}
7777
}
7878
"
7979
`;
8080

8181
exports[`directive generator --no-standalone should import the directive correctly when directory is nested deeper 2`] = `
82-
"import { TestDirective } from './test.directive';
82+
"import { Test } from './test';
8383
84-
describe('TestDirective', () => {
84+
describe('Test', () => {
8585
it('should create an instance', () => {
86-
const directive = new TestDirective();
86+
const directive = new Test();
8787
expect(directive).toBeTruthy();
8888
});
8989
});
@@ -92,10 +92,10 @@ describe('TestDirective', () => {
9292

9393
exports[`directive generator --no-standalone should import the directive correctly when directory is nested deeper 3`] = `
9494
"import { NgModule } from '@angular/core';
95-
import { TestDirective } from './my-directives/test/test.directive';
95+
import { Test } from './my-directives/test/test';
9696
@NgModule({
9797
imports: [],
98-
declarations: [TestDirective],
98+
declarations: [Test],
9999
exports: [],
100100
})
101101
export class TestModule {}
@@ -108,18 +108,18 @@ exports[`directive generator should generate correctly 1`] = `
108108
@Directive({
109109
selector: '[test]',
110110
})
111-
export class TestDirective {
111+
export class Test {
112112
constructor() {}
113113
}
114114
"
115115
`;
116116

117117
exports[`directive generator should generate correctly 2`] = `
118-
"import { TestDirective } from './test.directive';
118+
"import { Test } from './test';
119119
120-
describe('TestDirective', () => {
120+
describe('Test', () => {
121121
it('should create an instance', () => {
122-
const directive = new TestDirective();
122+
const directive = new Test();
123123
expect(directive).toBeTruthy();
124124
});
125125
});
@@ -132,18 +132,18 @@ exports[`directive generator should handle path with file extension 1`] = `
132132
@Directive({
133133
selector: '[test]',
134134
})
135-
export class TestDirective {
135+
export class Test {
136136
constructor() {}
137137
}
138138
"
139139
`;
140140

141141
exports[`directive generator should handle path with file extension 2`] = `
142-
"import { TestDirective } from './test.directive';
142+
"import { Test } from './test.directive';
143143
144-
describe('TestDirective', () => {
144+
describe('Test', () => {
145145
it('should create an instance', () => {
146-
const directive = new TestDirective();
146+
const directive = new Test();
147147
expect(directive).toBeTruthy();
148148
});
149149
});

0 commit comments

Comments
 (0)