Skip to content

Commit 4298f61

Browse files
Merge pull request #190 from angular/master
Added info & package.json script useful for devs
2 parents 46236d4 + 0cfbdbc commit 4298f61

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

packages/schematics/angular/utility/ast-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ export function addSymbolToNgModuleMetadata(
395395
// Get the indentation of the last element, if any.
396396
const text = node.getFullText(source);
397397
const matches = text.match(/^\r?\n\s*/);
398-
if (matches.length > 0) {
398+
if (matches && matches.length > 0) {
399399
toInsert = `,${matches[0]}${metadataField}: [${symbolName}]`;
400400
} else {
401401
toInsert = `, ${metadataField}: [${symbolName}]`;

packages/schematics/angular/utility/ast-utils_spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,19 @@ describe('ast utils', () => {
151151
const output = applyChanges(modulePath, moduleContent, changes || []);
152152
expect(output).toMatch(/imports: \[HelloWorld],\r?\n/m);
153153
});
154+
155+
it('should handle NgModule with no newlines', () => {
156+
const moduleContent = `
157+
import { BrowserModule } from '@angular/platform-browser';
158+
import { NgModule } from '@angular/core';
159+
160+
@NgModule({imports: [BrowserModule], declarations: []})
161+
export class AppModule { }
162+
`;
163+
const source = getTsSource(modulePath, moduleContent);
164+
const changes = addExportToModule(source, modulePath, 'FooComponent', './foo.component');
165+
const output = applyChanges(modulePath, moduleContent, changes);
166+
expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/);
167+
expect(output).toMatch(/exports: \[FooComponent\]/);
168+
});
154169
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface ModuleOptions {
2525
skipImport?: boolean;
2626
moduleExt?: string;
2727
routingModuleExt?: string;
28+
nameFormatter?: (str: string) => string;
2829
}
2930

3031
const MODULE_EXT = '.module.ts';
@@ -42,8 +43,9 @@ export function findModuleFromOptions(host: Tree, options: ModuleOptions): Path
4243
const routingModuleExt = options.routingModuleExt || ROUTING_MODULE_EXT;
4344

4445
if (!options.module) {
46+
options.nameFormatter = options.nameFormatter || strings.dasherize;
4547
const pathToCheck = (options.path || '')
46-
+ (options.flat ? '' : '/' + strings.dasherize(options.name));
48+
+ (options.flat ? '' : '/' + options.nameFormatter(options.name));
4749

4850
return normalize(findModule(host, pathToCheck, moduleExt, routingModuleExt));
4951
} else {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { Path } from '@angular-devkit/core';
8+
import { Path, strings } from '@angular-devkit/core';
99
import { EmptyTree, Tree } from '@angular-devkit/schematics';
1010
import { ModuleOptions, findModule, findModuleFromOptions } from './find-module';
1111

@@ -111,6 +111,14 @@ describe('find-module', () => {
111111
expect(modPath).toEqual('/projects/my-proj/src/app.module.ts' as Path);
112112
});
113113

114+
it('should find a module if nameFormatter is provided', () => {
115+
tree.create('/projects/my-proj/src/app_test.module.ts', '');
116+
options.path = '/projects/my-proj/src';
117+
options.nameFormatter = strings.underscore;
118+
const modPath = findModuleFromOptions(tree, options);
119+
expect(modPath).toEqual('/projects/my-proj/src/app_test.module.ts' as Path);
120+
});
121+
114122
it('should find a module in a sub dir', () => {
115123
tree.create('/projects/my-proj/src/admin/foo.module.ts', '');
116124
options.name = 'other/test';

0 commit comments

Comments
 (0)