Skip to content

Commit

Permalink
fix(@schematics/angular): addSymbolToNgModuleMetadata metadata with…
Browse files Browse the repository at this point in the history
…out 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
  • Loading branch information
alan-agius4 authored and kyliau committed Nov 14, 2018
1 parent c6cdf71 commit 76cb92f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/schematics/angular/utility/ast-utils.ts
Expand Up @@ -455,8 +455,8 @@ export function addSymbolToNgModuleMetadata(
position = node.getEnd();
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.match('^\r?\r?\n')) {
toInsert = `,${text.match(/^\r?\n\s+/)[0]}${metadataField}: [${symbolName}]`;
if (text.match(/^\r?\r?\n/)) {
toInsert = `,${text.match(/^\r?\n\s*/)[0]}${metadataField}: [${symbolName}]`;
} else {
toInsert = `, ${metadataField}: [${symbolName}]`;
}
Expand All @@ -469,7 +469,7 @@ export function addSymbolToNgModuleMetadata(
// Get the indentation of the last element, if any.
const text = node.getFullText(source);
if (text.match(/^\r?\n/)) {
toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName}`;
toInsert = `,${text.match(/^\r?\n(\r?)\s*/)[0]}${symbolName}`;
} else {
toInsert = `, ${symbolName}`;
}
Expand Down
15 changes: 14 additions & 1 deletion packages/schematics/angular/utility/ast-utils_spec.ts
Expand Up @@ -11,7 +11,11 @@ import { HostTree } from '@angular-devkit/schematics';
import * as ts from 'typescript';
import { Change, InsertChange } from '../utility/change';
import { getFileContent } from '../utility/test';
import { addExportToModule, addSymbolToNgModuleMetadata } from './ast-utils';
import {
addDeclarationToModule,
addExportToModule,
addSymbolToNgModuleMetadata,
} from './ast-utils';


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

it('should add declarations to module if not indented', () => {
moduleContent = tags.stripIndents`${moduleContent}`;
const source = getTsSource(modulePath, moduleContent);
const changes = addDeclarationToModule(source, modulePath, 'FooComponent', './foo.component');
const output = applyChanges(modulePath, moduleContent, changes);
expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/);
expect(output).toMatch(/declarations: \[\nAppComponent,\nFooComponent\n\]/);
});

it('should add metadata', () => {
const source = getTsSource(modulePath, moduleContent);
const changes = addSymbolToNgModuleMetadata(source, modulePath, 'imports', 'HelloWorld');
Expand Down

0 comments on commit 76cb92f

Please sign in to comment.