Skip to content

Commit ac61c41

Browse files
committed
fix(@schematics/angular): ast utils - handle NgModule with no newlines
Exposed in Google where the formatting is different. Check for null before using a regex match result.
1 parent 248d531 commit ac61c41

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: packages/schematics/angular/utility/ast-utils.ts

+1-1
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}]`;

Diff for: packages/schematics/angular/utility/ast-utils_spec.ts

+15
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+
fit('should handle Ngodule 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
});

0 commit comments

Comments
 (0)