Skip to content

fix(@schematics/angular): findModuleFromOptions not handling proper… #13720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/schematics/angular/utility/find-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
join,
normalize,
relative,
strings,
} from '@angular-devkit/core';
import { DirEntry, Tree } from '@angular-devkit/schematics';

Expand All @@ -25,7 +24,6 @@ export interface ModuleOptions {
skipImport?: boolean;
moduleExt?: string;
routingModuleExt?: string;
nameFormatter?: (str: string) => string;
}

const MODULE_EXT = '.module.ts';
Expand All @@ -43,8 +41,7 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
const routingModuleExt = options.routingModuleExt || ROUTING_MODULE_EXT;

if (!options.module) {
options.nameFormatter = options.nameFormatter || strings.dasherize;
const pathToCheck = (options.path || '') + '/' + options.nameFormatter(options.name);
const pathToCheck = (options.path || '') + '/' + options.name;

return normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt));
} else {
Expand Down
18 changes: 13 additions & 5 deletions packages/schematics/angular/utility/find-module_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Path, strings } from '@angular-devkit/core';
import { Path } from '@angular-devkit/core';
import { EmptyTree, Tree } from '@angular-devkit/schematics';
import { ModuleOptions, findModule, findModuleFromOptions } from './find-module';

Expand Down Expand Up @@ -111,12 +111,20 @@ describe('find-module', () => {
expect(modPath).toEqual('/projects/my-proj/src/app.module.ts' as Path);
});

it('should find a module if nameFormatter is provided', () => {
Copy link
Collaborator Author

@alan-agius4 alan-agius4 Feb 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test was actually not testing the correct usecase, as nameFormatter was only used when name is provided

tree.create('/projects/my-proj/src/app_test.module.ts', '');
it('should find a module when name has underscore', () => {
tree.create('/projects/my-proj/src/feature_module/app_test.module.ts', '');
options.path = '/projects/my-proj/src';
options.nameFormatter = strings.underscore;
options.name = 'feature_module/new_component';
const modPath = findModuleFromOptions(tree, options);
expect(modPath).toEqual('/projects/my-proj/src/app_test.module.ts' as Path);
expect(modPath).toEqual('/projects/my-proj/src/feature_module/app_test.module.ts' as Path);
});

it('should find a module when name has uppercase', () => {
tree.create('/projects/my-proj/src/featureModule/appTest.module.ts', '');
options.path = '/projects/my-proj/src';
options.name = 'featureModule/newComponent';
const modPath = findModuleFromOptions(tree, options);
expect(modPath).toEqual('/projects/my-proj/src/featureModule/appTest.module.ts' as Path);
});

it('should find a module if flat is true', () => {
Expand Down