Skip to content

Commit b0897c0

Browse files
alan-agius4vikerman
authored andcommitted
fix(@schematics/angular): better error message when finding only routing modules (#11994)
Closes #11961
1 parent 41d2087 commit b0897c0

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

packages/schematics/angular/utility/find-module.ts

+16-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
2828

2929
if (!options.module) {
3030
const pathToCheck = (options.path || '')
31-
+ (options.flat ? '' : '/' + strings.dasherize(options.name));
31+
+ (options.flat ? '' : '/' + strings.dasherize(options.name));
3232

3333
return normalize(findModule(host, pathToCheck));
3434
} else {
@@ -59,21 +59,30 @@ export function findModule(host: Tree, generateDir: string): Path {
5959
const moduleRe = /\.module\.ts$/;
6060
const routingModuleRe = /-routing\.module\.ts/;
6161

62+
let foundRoutingModule = false;
63+
6264
while (dir) {
63-
const matches = dir.subfiles.filter(p => moduleRe.test(p) && !routingModuleRe.test(p));
65+
const allMatches = dir.subfiles.filter(p => moduleRe.test(p));
66+
const filteredMatches = allMatches.filter(p => !routingModuleRe.test(p));
67+
68+
foundRoutingModule = foundRoutingModule || allMatches.length !== filteredMatches.length;
6469

65-
if (matches.length == 1) {
66-
return join(dir.path, matches[0]);
67-
} else if (matches.length > 1) {
70+
if (filteredMatches.length == 1) {
71+
return join(dir.path, filteredMatches[0]);
72+
} else if (filteredMatches.length > 1) {
6873
throw new Error('More than one module matches. Use skip-import option to skip importing '
6974
+ 'the component into the closest module.');
7075
}
7176

7277
dir = dir.parent;
7378
}
7479

75-
throw new Error('Could not find an NgModule. Use the skip-import '
76-
+ 'option to skip importing in NgModule.');
80+
const errorMsg = foundRoutingModule ? 'Could not find a non Routing NgModule.'
81+
+ '\nModules with suffix \'-routing.module\' are strictly reserved for routing.'
82+
+ '\nUse the skip-import option to skip importing in NgModule.'
83+
: 'Could not find an NgModule. Use the skip-import option to skip importing in NgModule.';
84+
85+
throw new Error(errorMsg);
7786
}
7887

7988
/**

packages/schematics/angular/utility/find-module_spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ describe('find-module', () => {
5353
}
5454
});
5555

56+
it('should throw if only routing modules were found', () => {
57+
host = new EmptyTree();
58+
host.create('/foo/src/app/anything-routing.module.ts', 'anything routing module');
59+
60+
try {
61+
findModule(host, 'foo/src/app/anything-routing');
62+
throw new Error('Succeeded, should have failed');
63+
} catch (err) {
64+
expect(err.message).toMatch(/Could not find a non Routing NgModule/);
65+
}
66+
});
67+
5668
it('should throw if two modules found', () => {
5769
try {
5870
host = new EmptyTree();

0 commit comments

Comments
 (0)