fix(ivy): throw a better error when DI can't inject a ctor param (#33739) (#34340)

Occasionally a factory function needs to be generated for an "invalid"
constructor (one with parameters types which aren't injectable). Typically
this happens in JIT mode where understanding of parameters cannot be done in
the same "up-front" way that the AOT compiler can.

This commit changes the JIT compiler to generate a new `invalidFactoryDep`
call for each invalid parameter. This instruction will error at runtime if
called, indicating both the index of the invalid parameter as well as (via
the stack trace) the factory function which was generated for the type being
constructed.

Fixes #33637

PR Close #33739

PR Close #34340
This commit is contained in:
Alex Rickabaugh
2019-11-11 00:18:43 -08:00
committed by Andrew Kushnir
parent 4b95ba43a5
commit 676aca108f
11 changed files with 97 additions and 10 deletions

View File

@ -342,6 +342,54 @@ ivyEnabled && describe('render3 jit', () => {
expect((TestComponent as any).ɵcmp.viewQuery).not.toBeNull();
});
describe('invalid parameters', () => {
it('should error when creating an @Injectable that extends a class with a faulty parameter', () => {
@Injectable({providedIn: 'root'})
class Legit {
}
@Injectable()
class Base {
constructor(first: Legit, second: any) {}
}
@Injectable({providedIn: 'root'})
class Service extends Base {
}
const ServiceAny = Service as any;
expect(ServiceAny.ɵprov).toBeDefined();
expect(ServiceAny.ɵprov.providedIn).toBe('root');
expect(() => ɵɵinject(Service))
.toThrowError(
/constructor is not compatible with Angular Dependency Injection because its dependency at index 1 of the parameter list is invalid/);
});
it('should error when creating an @Directive that extends an undecorated class with parameters',
() => {
@Injectable({providedIn: 'root'})
class Legit {
}
class BaseDir {
constructor(first: Legit) {}
}
@Directive({selector: 'test'})
class TestDir extends BaseDir {
}
const TestDirAny = TestDir as any;
expect(TestDirAny.ɵfac).toBeDefined();
expect(() => TestDirAny.ɵfac())
.toThrowError(
/constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid/);
});
});
});
it('ensure at least one spec exists', () => {});