Skip to content

Commit a116a64

Browse files
cexbrayatpkozlowski-opensource
authored andcommitted
refactor(migrations): useFactory in provide-initializer migration (angular#58493)
The following provider: ```ts { provide: APP_INITIALIZER, useFactory: (initService: InitService) => { return () => initService.init() }, deps: [InitService], multi: true } ``` was migrated into: ```ts provideAppInitializer(() => { return ((initService: InitService) => { return () => initService.init() })(inject(InitService)); }) ``` This doesn't compile because there is an extra function: ``` ✘ [ERROR] TS2345: Argument of type '() => () => void' is not assignable to parameter of type '() => void | Observable<unknown> | Promise<unknown>'. Type '() => void' is not assignable to type 'void | Observable<unknown> | Promise<unknown>'. [plugin angular-compiler] src/app/app.config.ts:7:26: 7 │ ...ovideAppInitializer(() => { return ((initService: InitService) => { ``` It now migrates the provider into: ```ts provideAppInitializer(((initService: InitService) => { return () => initService.init() })(inject(InitService))) ``` PR Close angular#58493
1 parent 536ee2b commit a116a64

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

packages/core/schematics/migrations/provide-initializer/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function tryParseProviderExpression(node: ts.Node): ProviderInfo | undefined {
167167
return {
168168
...info,
169169
importInject: deps.length > 0,
170-
initializerCode: `() => { return (${useFactory.getText()})(${args.join(', ')}); }`,
170+
initializerCode: `(${useFactory.getText()})(${args.join(', ')})`,
171171
};
172172
}
173173

packages/core/schematics/test/provide_initializer_spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ describe('Provide initializer migration', () => {
105105
}];
106106
`);
107107

108-
expect(content).toContain(`const providers = [provideAppInitializer(() => { return (() => {
108+
expect(content).toContain(`const providers = [provideAppInitializer((() => {
109109
const service = inject(Service);
110110
return () => service.init();
111-
})(); })];`);
111+
})())];`);
112112
});
113113

114114
it('should transform APP_INITIALIZER + useExisting into provideAppInitializer', async () => {
@@ -144,9 +144,9 @@ describe('Provide initializer migration', () => {
144144

145145
expect(content).toContain(`import { inject, provideAppInitializer } from '@angular/core';`);
146146
expect(content).toContain(
147-
`const providers = [provideAppInitializer(() => { return ((a: ServiceA, b: ServiceB) => {
147+
`const providers = [provideAppInitializer(((a: ServiceA, b: ServiceB) => {
148148
return () => a.init();
149-
})(inject(ServiceA), inject(ServiceB)); })];`,
149+
})(inject(ServiceA), inject(ServiceB)))];`,
150150
);
151151
});
152152

0 commit comments

Comments
 (0)