@@ -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 = / \. m o d u l e \. t s $ / ;
6060 const routingModuleRe = / - r o u t i n g \. m o d u l e \. t s / ;
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/**
0 commit comments