From 96b3520f5a33551efb60b09796caa53ac1c02006 Mon Sep 17 00:00:00 2001 From: zyzhao Date: Thu, 29 Nov 2018 10:25:46 +0800 Subject: [PATCH] fix(@schematics/angular): add providers into providers metadata but not inner Object with ut. --- .../schematics/angular/utility/ast-utils.ts | 8 +++--- .../angular/utility/ast-utils_spec.ts | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/schematics/angular/utility/ast-utils.ts b/packages/schematics/angular/utility/ast-utils.ts index e81f85919149..46ea7c441110 100644 --- a/packages/schematics/angular/utility/ast-utils.ts +++ b/packages/schematics/angular/utility/ast-utils.ts @@ -449,16 +449,14 @@ export function addSymbolToNgModuleMetadata( const expr = node as ts.ObjectLiteralExpression; if (expr.properties.length == 0) { position = expr.getEnd() - 1; - toInsert = ` ${metadataField}: [${symbolName}]\n`; + toInsert = ` ${symbolName}\n`; } else { - node = expr.properties[expr.properties.length - 1]; - 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}]`; + toInsert = `,${text.match(/^\r?\n\s*/)[0]}${symbolName}`; } else { - toInsert = `, ${metadataField}: [${symbolName}]`; + toInsert = `, ${symbolName}`; } } } else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) { diff --git a/packages/schematics/angular/utility/ast-utils_spec.ts b/packages/schematics/angular/utility/ast-utils_spec.ts index 21bd96d6e764..2aee6a258d7e 100644 --- a/packages/schematics/angular/utility/ast-utils_spec.ts +++ b/packages/schematics/angular/utility/ast-utils_spec.ts @@ -14,6 +14,7 @@ import { getFileContent } from '../utility/test'; import { addDeclarationToModule, addExportToModule, + addProviderToModule, addSymbolToNgModuleMetadata, } from './ast-utils'; @@ -179,4 +180,29 @@ describe('ast utils', () => { expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/); expect(output).toMatch(/exports: \[FooComponent\]/); }); + + it('should add into providers metadata in new line ', () => { + const moduleContent = ` + import { BrowserModule } from '@angular/platform-browser'; + import { NgModule } from '@angular/core'; + + @NgModule({ + imports: [BrowserModule], + declarations: [], + providers: [ + { + provide: HTTP_INTERCEPTORS, + useClass: AuthInterceptor, + multi: true + } + ] + }) + export class AppModule { } + `; + const source = getTsSource(modulePath, moduleContent); + const changes = addProviderToModule(source, modulePath, 'LogService', './log.service'); + const output = applyChanges(modulePath, moduleContent, changes); + expect(output).toMatch(/import { LogService } from '.\/log.service';/); + expect(output).toMatch(/\},\r?\n\s*LogService\r?\n\s*\]/); + }); });