Skip to content

Commit 040282d

Browse files
committed
fix(@schematics/angular): generate interceptors with a dash type separator
To align with the updated style guide, Angular v20 will generate interceptors with file extension `interceptor` type prefixed with a `-` separator instead of a `.` by default. Projects will automatically use this naming convention. Projects can however opt-out by setting the `typeSeparator` option to `.` for the interceptor schematic. This can be done as a default in the `angular.json` or directly on the commandline via `--type-separator=.` when executing `ng generate`. As an example, `example.interceptor.ts` will now be named `example-interceptor.ts`. The TypeScript declaration will continue to contain `Interceptor` such as with `ExampleInterceptor`.
1 parent ea1143d commit 040282d

7 files changed

+38
-10
lines changed

Diff for: packages/schematics/angular/interceptor/class-files/__name@dasherize__.interceptor.spec.ts.template renamed to packages/schematics/angular/interceptor/class-files/__name@dasherize____typeSeparator__interceptor.spec.ts.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TestBed } from '@angular/core/testing';
22

3-
import { <%= classify(name) %>Interceptor } from './<%= dasherize(name) %>.interceptor';
3+
import { <%= classify(name) %>Interceptor } from './<%= dasherize(name) %><%= typeSeparator %>interceptor';
44

55
describe('<%= classify(name) %>Interceptor', () => {
66
beforeEach(() => TestBed.configureTestingModule({

Diff for: packages/schematics/angular/interceptor/functional-files/__name@dasherize__.interceptor.spec.ts.template renamed to packages/schematics/angular/interceptor/functional-files/__name@dasherize____typeSeparator__interceptor.spec.ts.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TestBed } from '@angular/core/testing';
22
import { HttpInterceptorFn } from '@angular/common/http';
33

4-
import { <%= camelize(name) %>Interceptor } from './<%= dasherize(name) %>.interceptor';
4+
import { <%= camelize(name) %>Interceptor } from './<%= dasherize(name) %><%= typeSeparator %>interceptor';
55

66
describe('<%= camelize(name) %>Interceptor', () => {
77
const interceptor: HttpInterceptorFn = (req, next) =>

Diff for: packages/schematics/angular/interceptor/index_spec.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,31 @@ describe('Interceptor Schematic', () => {
4646

4747
const tree = await schematicRunner.runSchematic('interceptor', options, appTree);
4848

49+
const files = tree.files;
50+
expect(files).toContain('/projects/bar/src/app/foo/foo-interceptor.spec.ts');
51+
expect(files).toContain('/projects/bar/src/app/foo/foo-interceptor.ts');
52+
});
53+
54+
it('should use a `.` type separator when specified', async () => {
55+
const options = { ...defaultOptions, typeSeparator: '.' };
56+
57+
const tree = await schematicRunner.runSchematic('interceptor', options, appTree);
4958
const files = tree.files;
5059
expect(files).toContain('/projects/bar/src/app/foo/foo.interceptor.spec.ts');
5160
expect(files).toContain('/projects/bar/src/app/foo/foo.interceptor.ts');
61+
const specContent = tree.readContent('/projects/bar/src/app/foo/foo.interceptor.spec.ts');
62+
expect(specContent).toContain(`'./foo.interceptor'`);
63+
});
64+
65+
it('should use a `-` type separator when specified', async () => {
66+
const options = { ...defaultOptions, typeSeparator: '-' };
67+
68+
const tree = await schematicRunner.runSchematic('interceptor', options, appTree);
69+
const files = tree.files;
70+
expect(files).toContain('/projects/bar/src/app/foo/foo-interceptor.spec.ts');
71+
expect(files).toContain('/projects/bar/src/app/foo/foo-interceptor.ts');
72+
const specContent = tree.readContent('/projects/bar/src/app/foo/foo-interceptor.spec.ts');
73+
expect(specContent).toContain(`'./foo-interceptor'`);
5274
});
5375

5476
it('should respect the skipTests flag', async () => {
@@ -57,8 +79,8 @@ describe('Interceptor Schematic', () => {
5779
const tree = await schematicRunner.runSchematic('interceptor', options, appTree);
5880

5981
const files = tree.files;
60-
expect(files).toContain('/projects/bar/src/app/foo/foo.interceptor.ts');
61-
expect(files).not.toContain('/projects/bar/src/app/foo/foo.interceptor.spec.ts');
82+
expect(files).toContain('/projects/bar/src/app/foo/foo-interceptor.ts');
83+
expect(files).not.toContain('/projects/bar/src/app/foo/foo-interceptor.spec.ts');
6284
});
6385

6486
it('should respect the sourceRoot value', async () => {
@@ -67,7 +89,7 @@ describe('Interceptor Schematic', () => {
6789
appTree.overwrite('/angular.json', JSON.stringify(config, null, 2));
6890
appTree = await schematicRunner.runSchematic('interceptor', defaultOptions, appTree);
6991

70-
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo.interceptor.ts');
92+
expect(appTree.files).toContain('/projects/bar/custom/app/foo/foo-interceptor.ts');
7193
});
7294

7395
it('should create a functional interceptor', async () => {
@@ -77,7 +99,7 @@ describe('Interceptor Schematic', () => {
7799
appTree,
78100
);
79101

80-
const fileString = tree.readContent('/projects/bar/src/app/foo/foo.interceptor.ts');
102+
const fileString = tree.readContent('/projects/bar/src/app/foo/foo-interceptor.ts');
81103
expect(fileString).toContain(
82104
'export const fooInterceptor: HttpInterceptorFn = (req, next) => {',
83105
);
@@ -90,7 +112,7 @@ describe('Interceptor Schematic', () => {
90112
appTree,
91113
);
92114

93-
const fileString = tree.readContent('/projects/bar/src/app/foo/foo.interceptor.spec.ts');
115+
const fileString = tree.readContent('/projects/bar/src/app/foo/foo-interceptor.spec.ts');
94116
expect(fileString).toContain('const interceptor: HttpInterceptorFn = (req, next) => ');
95117
expect(fileString).toContain('TestBed.runInInjectionContext(() => fooInterceptor(req, next));');
96118
});

Diff for: packages/schematics/angular/interceptor/schema.json

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
"type": "boolean",
4646
"description": "Creates the interceptor as a function `HttpInterceptorFn` instead of a class. Functional interceptors can be simpler for basic scenarios.",
4747
"default": true
48+
},
49+
"typeSeparator": {
50+
"type": "string",
51+
"default": "-",
52+
"enum": ["-", "."],
53+
"description": "The separator character to use before the type within the generated file's name. For example, if you set the option to `.`, the file will be named `example.interceptor.ts`."
4854
}
4955
},
5056
"required": ["name", "project"]

Diff for: tests/legacy-cli/e2e/tests/generate/interceptor/interceptor-basic.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export default function () {
77
const interceptorDir = join('src', 'app');
88

99
return (
10-
ng('generate', 'interceptor', 'test-interceptor')
10+
ng('generate', 'interceptor', 'test')
1111
.then(() => expectFileToExist(interceptorDir))
12-
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.interceptor.ts')))
13-
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.interceptor.spec.ts')))
12+
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.ts')))
13+
.then(() => expectFileToExist(join(interceptorDir, 'test-interceptor.spec.ts')))
1414

1515
// Try to run the unit tests.
1616
.then(() => ng('test', '--watch=false'))

0 commit comments

Comments
 (0)