Skip to content

Commit 8f0d03b

Browse files
alan-agius4Keen Yee Liau
authored andcommitted
fix(@schematics/angular): addSymbolToNgModuleMetadata metadata without indent
At the moment, at least a single whitespace is required as otherwise the following error `Cannot read property '0' of null ` will be thrown Fixes #12950
1 parent a2aba28 commit 8f0d03b

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,8 @@ export function addSymbolToNgModuleMetadata(
455455
position = node.getEnd();
456456
// Get the indentation of the last element, if any.
457457
const text = node.getFullText(source);
458-
if (text.match('^\r?\r?\n')) {
459-
toInsert = `,${text.match(/^\r?\n\s+/)[0]}${metadataField}: [${symbolName}]`;
458+
if (text.match(/^\r?\r?\n/)) {
459+
toInsert = `,${text.match(/^\r?\n\s*/)[0]}${metadataField}: [${symbolName}]`;
460460
} else {
461461
toInsert = `, ${metadataField}: [${symbolName}]`;
462462
}
@@ -469,7 +469,7 @@ export function addSymbolToNgModuleMetadata(
469469
// Get the indentation of the last element, if any.
470470
const text = node.getFullText(source);
471471
if (text.match(/^\r?\n/)) {
472-
toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName}`;
472+
toInsert = `,${text.match(/^\r?\n(\r?)\s*/)[0]}${symbolName}`;
473473
} else {
474474
toInsert = `, ${symbolName}`;
475475
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { HostTree } from '@angular-devkit/schematics';
1111
import * as ts from 'typescript';
1212
import { Change, InsertChange } from '../utility/change';
1313
import { getFileContent } from '../utility/test';
14-
import { addExportToModule, addSymbolToNgModuleMetadata } from './ast-utils';
14+
import {
15+
addDeclarationToModule,
16+
addExportToModule,
17+
addSymbolToNgModuleMetadata,
18+
} from './ast-utils';
1519

1620

1721
function getTsSource(path: string, content: string): ts.SourceFile {
@@ -73,6 +77,15 @@ describe('ast utils', () => {
7377
expect(output).toMatch(/exports: \[FooComponent\]/);
7478
});
7579

80+
it('should add declarations to module if not indented', () => {
81+
moduleContent = tags.stripIndents`${moduleContent}`;
82+
const source = getTsSource(modulePath, moduleContent);
83+
const changes = addDeclarationToModule(source, modulePath, 'FooComponent', './foo.component');
84+
const output = applyChanges(modulePath, moduleContent, changes);
85+
expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/);
86+
expect(output).toMatch(/declarations: \[\nAppComponent,\nFooComponent\n\]/);
87+
});
88+
7689
it('should add metadata', () => {
7790
const source = getTsSource(modulePath, moduleContent);
7891
const changes = addSymbolToNgModuleMetadata(source, modulePath, 'imports', 'HelloWorld');

0 commit comments

Comments
 (0)