Skip to content

Commit 822d6a0

Browse files
bsdfzzzyvikerman
authored andcommitted
fix(@schematics/angular): add providers into providers metadata but not inner Object with ut. (#13081)
1 parent 0692cac commit 822d6a0

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -449,16 +449,14 @@ export function addSymbolToNgModuleMetadata(
449449
const expr = node as ts.ObjectLiteralExpression;
450450
if (expr.properties.length == 0) {
451451
position = expr.getEnd() - 1;
452-
toInsert = ` ${metadataField}: [${symbolName}]\n`;
452+
toInsert = ` ${symbolName}\n`;
453453
} else {
454-
node = expr.properties[expr.properties.length - 1];
455-
position = node.getEnd();
456454
// Get the indentation of the last element, if any.
457455
const text = node.getFullText(source);
458456
if (text.match(/^\r?\r?\n/)) {
459-
toInsert = `,${text.match(/^\r?\n\s*/)[0]}${metadataField}: [${symbolName}]`;
457+
toInsert = `,${text.match(/^\r?\n\s*/)[0]}${symbolName}`;
460458
} else {
461-
toInsert = `, ${metadataField}: [${symbolName}]`;
459+
toInsert = `, ${symbolName}`;
462460
}
463461
}
464462
} else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {

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

+26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getFileContent } from '../utility/test';
1414
import {
1515
addDeclarationToModule,
1616
addExportToModule,
17+
addProviderToModule,
1718
addSymbolToNgModuleMetadata,
1819
} from './ast-utils';
1920

@@ -179,4 +180,29 @@ describe('ast utils', () => {
179180
expect(output).toMatch(/import { FooComponent } from '.\/foo.component';/);
180181
expect(output).toMatch(/exports: \[FooComponent\]/);
181182
});
183+
184+
it('should add into providers metadata in new line ', () => {
185+
const moduleContent = `
186+
import { BrowserModule } from '@angular/platform-browser';
187+
import { NgModule } from '@angular/core';
188+
189+
@NgModule({
190+
imports: [BrowserModule],
191+
declarations: [],
192+
providers: [
193+
{
194+
provide: HTTP_INTERCEPTORS,
195+
useClass: AuthInterceptor,
196+
multi: true
197+
}
198+
]
199+
})
200+
export class AppModule { }
201+
`;
202+
const source = getTsSource(modulePath, moduleContent);
203+
const changes = addProviderToModule(source, modulePath, 'LogService', './log.service');
204+
const output = applyChanges(modulePath, moduleContent, changes);
205+
expect(output).toMatch(/import { LogService } from '.\/log.service';/);
206+
expect(output).toMatch(/\},\r?\n\s*LogService\r?\n\s*\]/);
207+
});
182208
});

0 commit comments

Comments
 (0)