Skip to content

Commit

Permalink
fix(@schematics/angular): add providers into providers metadata but n…
Browse files Browse the repository at this point in the history
…ot inner Object with ut. (#13081)
  • Loading branch information
bsdfzzzy authored and vikerman committed Nov 30, 2018
1 parent 0692cac commit 822d6a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/schematics/angular/utility/ast-utils.ts
Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions packages/schematics/angular/utility/ast-utils_spec.ts
Expand Up @@ -14,6 +14,7 @@ import { getFileContent } from '../utility/test';
import {
addDeclarationToModule,
addExportToModule,
addProviderToModule,
addSymbolToNgModuleMetadata,
} from './ast-utils';

Expand Down Expand Up @@ -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*\]/);
});
});

0 comments on commit 822d6a0

Please sign in to comment.