fix(ivy): handle overloaded constructors in ngtsc (#34590)
Currently ngtsc looks for the first `ConstructorDeclaration` when figuring out what the parameters are so that it can generate the DI instructions. The problem is that if a constructor has overloads, it'll have several `ConstructorDeclaration` members with a different number of parameters. These changes tweak the logic so it looks for the constructor implementation. PR Close #34590
This commit is contained in:
@ -99,6 +99,41 @@ describe('compiler compliance: dependency injection', () => {
|
||||
expectEmit(result.source, def, 'Incorrect injectable definition');
|
||||
});
|
||||
|
||||
it('should create a factory definition for an injectable with an overloaded constructor', () => {
|
||||
const files = {
|
||||
app: {
|
||||
'spec.ts': `
|
||||
import {Injectable, Optional} from '@angular/core';
|
||||
|
||||
class MyDependency {}
|
||||
class MyOptionalDependency {}
|
||||
|
||||
@Injectable()
|
||||
export class MyService {
|
||||
constructor(dep: MyDependency);
|
||||
constructor(dep: MyDependency, @Optional() optionalDep?: MyOptionalDependency) {}
|
||||
}
|
||||
`
|
||||
}
|
||||
};
|
||||
|
||||
const factory = `
|
||||
MyService.ɵfac = function MyService_Factory(t) {
|
||||
return new (t || MyService)($r3$.ɵɵinject(MyDependency), $r3$.ɵɵinject(MyOptionalDependency, 8));
|
||||
}`;
|
||||
|
||||
const def = `
|
||||
MyService.ɵprov = $r3$.ɵɵdefineInjectable({
|
||||
token: MyService,
|
||||
factory: MyService.ɵfac
|
||||
});
|
||||
`;
|
||||
|
||||
const result = compile(files, angularFiles);
|
||||
expectEmit(result.source, factory, 'Incorrect factory definition');
|
||||
expectEmit(result.source, def, 'Incorrect injectable definition');
|
||||
});
|
||||
|
||||
it('should create a single factory def if the class has more than one decorator', () => {
|
||||
const files = {
|
||||
app: {
|
||||
|
Reference in New Issue
Block a user