fix(ivy): prevent invalid forward references in setClassMetadata call (#27561)
In Ivy, a pure call to `setClassMetadata` is inserted to retain the information that would otherwise be lost while eliding the Angular decorators. In the past, the Angular constructor decorators were wrapped inside of an anonymous function which was only evaluated once `ReflectionCapabilities` was requested for such metadata. This approach prevents forward references from inside the constructor parameter decorators from being evaluated before they are available. In the `setClassMetadata` call, the constructor parameters were not wrapped within an anonymous function, such that forward references were evaluated too early, causing runtime errors. This commit changes the `setClassMetadata` call to pass the constructor parameter decorators inside of an anonymous function again, such that forward references are not resolved until requested by `ReflectionCapabilities`, therefore avoiding the early reads of forward refs. PR Close #27561
This commit is contained in:
@ -45,8 +45,15 @@ export function generateSetClassMetadataCall(
|
||||
let metaCtorParameters: ts.Expression = ts.createNull();
|
||||
const classCtorParameters = reflection.getConstructorParameters(clazz);
|
||||
if (classCtorParameters !== null) {
|
||||
metaCtorParameters = ts.createArrayLiteral(
|
||||
const ctorParameters = ts.createArrayLiteral(
|
||||
classCtorParameters.map(param => ctorParameterToMetadata(param, isCore)));
|
||||
metaCtorParameters = ts.createFunctionExpression(
|
||||
/* modifiers */ undefined,
|
||||
/* asteriskToken */ undefined,
|
||||
/* name */ undefined,
|
||||
/* typeParameters */ undefined,
|
||||
/* parameters */ undefined,
|
||||
/* type */ undefined, ts.createBlock([ts.createReturn(ctorParameters)]));
|
||||
}
|
||||
|
||||
// Do the same for property decorators.
|
||||
|
@ -35,7 +35,7 @@ describe('ngtsc setClassMetadata converter', () => {
|
||||
`/*@__PURE__*/ i0.ɵsetClassMetadata(Target, [{ type: Component, args: ['metadata'] }], null, null);`);
|
||||
});
|
||||
|
||||
it('should convert decorated class construtor parameter metadata', () => {
|
||||
it('should convert decorated class constructor parameter metadata', () => {
|
||||
const res = compileAndPrint(`
|
||||
import {Component, Inject, Injector} from '@angular/core';
|
||||
const FOO = 'foo';
|
||||
@ -45,7 +45,7 @@ describe('ngtsc setClassMetadata converter', () => {
|
||||
}
|
||||
`);
|
||||
expect(res).toContain(
|
||||
`[{ type: undefined, decorators: [{ type: Inject, args: [FOO] }] }, { type: Injector }], null);`);
|
||||
`function () { return [{ type: undefined, decorators: [{ type: Inject, args: [FOO] }] }, { type: Injector }]; }, null);`);
|
||||
});
|
||||
|
||||
it('should convert decorated field metadata', () => {
|
||||
|
Reference in New Issue
Block a user