fix(ivy): ngtsc should pay attention to declaration order (#25392)
When generating the 'directives:' property of ngComponentDef, ngtsc needs to be conscious of declaration order. If a directive being written into the array is declarated after the component currently being compiled, then the entire directives array needs to be wrapped in a closure. This commit fixes ngtsc to pay attention to such ordering issues within directives arrays. PR Close #25392
This commit is contained in:

committed by
Ben Lesh

parent
6f085f8610
commit
2befc65777
@ -144,6 +144,13 @@ export interface R3ComponentMetadata extends R3DirectiveMetadata {
|
||||
* scope of the compilation.
|
||||
*/
|
||||
directives: Map<string, o.Expression>;
|
||||
|
||||
/**
|
||||
* Whether to wrap the 'directives' array, if one is generated, in a closure.
|
||||
*
|
||||
* This is done when the directives contain forward references.
|
||||
*/
|
||||
wrapDirectivesInClosure: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,7 +165,11 @@ export function compileComponentFromMetadata(
|
||||
|
||||
// e.g. `directives: [MyDirective]`
|
||||
if (directivesUsed.size) {
|
||||
definitionMap.set('directives', o.literalArr(Array.from(directivesUsed)));
|
||||
let directivesExpr: o.Expression = o.literalArr(Array.from(directivesUsed));
|
||||
if (meta.wrapDirectivesInClosure) {
|
||||
directivesExpr = o.fn([], [new o.ReturnStatement(directivesExpr)]);
|
||||
}
|
||||
definitionMap.set('directives', directivesExpr);
|
||||
}
|
||||
|
||||
// e.g. `pipes: [MyPipe]`
|
||||
@ -241,6 +245,7 @@ export function compileComponentFromRender2(
|
||||
directives: typeMapToExpressionMap(directiveTypeBySel, outputCtx),
|
||||
pipes: typeMapToExpressionMap(pipeTypeByName, outputCtx),
|
||||
viewQueries: queriesFromGlobalMetadata(component.viewQueries, outputCtx),
|
||||
wrapDirectivesInClosure: false,
|
||||
};
|
||||
const res = compileComponentFromMetadata(meta, outputCtx.constantPool, bindingParser);
|
||||
|
||||
|
Reference in New Issue
Block a user