fix(core): missing-injectable migration should handle forwardRef (#33286)

Currently the migration is unable to migrate instances where
the provider definition uses `forwardRef`. Since this is a
common pattern, we should support that from within the migration.

The solution to the problem is adding a foreign function resolver
to the `PartialEvaluator`. This basically matches the usage of
the static evaluation that is used by the ngtsc annotations.

PR Close #33286
This commit is contained in:
Paul Gschwendtner
2019-10-21 15:45:33 +02:00
committed by Andrew Kushnir
parent 4b81bb5c97
commit eeecbf28e4
3 changed files with 35 additions and 3 deletions

View File

@ -48,6 +48,10 @@ describe('Missing injectable migration', () => {
// Switch into the temporary directory path. This allows us to run
// the schematic against our custom unit test tree.
shx.cd(tmpDirPath);
writeFile('/node_modules/@angular/core/index.d.ts', `
export declare function forwardRef(fn: Function);
`);
});
afterEach(() => {
@ -135,6 +139,24 @@ describe('Missing injectable migration', () => {
.toContain(`{ ${type}, Injectable } from '@angular/core`);
});
it(`should migrate object literal provider with forwardRef in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}, forwardRef} from '@angular/core';
@${type}({${propName}: [{provide: forwardRef(() => MyService)}]})
export class TestClass {}
export class MyService {}
`);
await runMigration();
expect(warnOutput.length).toBe(0);
expect(tree.readContent('/index.ts')).toMatch(/@Injectable\(\)\s+export class MyService/);
expect(tree.readContent('/index.ts'))
.toContain(`{ ${type}, forwardRef, Injectable } from '@angular/core`);
});
it(`should not migrate object literal provider with "useValue" in ${type}`, async() => {
writeFile('/index.ts', `
import {${type}} from '@angular/core';