refactor(ivy): use generated consts value to set binding index (#25533)
PR Close #25533
This commit is contained in:

committed by
Jason Aden

parent
4708cb91ef
commit
f2aa9c6a7f
@ -189,13 +189,15 @@ export function compileComponentFromMetadata(
|
||||
const pipesUsed = new Set<o.Expression>();
|
||||
|
||||
const template = meta.template;
|
||||
const templateFunctionExpression =
|
||||
new TemplateDefinitionBuilder(
|
||||
constantPool, BindingScope.ROOT_SCOPE, 0, templateTypeName, templateName,
|
||||
meta.viewQueries, directiveMatcher, directivesUsed, meta.pipes, pipesUsed,
|
||||
R3.namespaceHTML)
|
||||
.buildTemplateFunction(
|
||||
template.nodes, [], template.hasNgContent, template.ngContentSelectors);
|
||||
const templateBuilder = new TemplateDefinitionBuilder(
|
||||
constantPool, BindingScope.ROOT_SCOPE, 0, templateTypeName, templateName, meta.viewQueries,
|
||||
directiveMatcher, directivesUsed, meta.pipes, pipesUsed, R3.namespaceHTML);
|
||||
|
||||
const templateFunctionExpression = templateBuilder.buildTemplateFunction(
|
||||
template.nodes, [], template.hasNgContent, template.ngContentSelectors);
|
||||
|
||||
// e.g. `consts: 2`
|
||||
definitionMap.set('consts', o.literal(templateBuilder.getSlotCount()));
|
||||
|
||||
definitionMap.set('template', templateFunctionExpression);
|
||||
|
||||
|
@ -171,6 +171,10 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
|
||||
// resolving bindings.
|
||||
t.visitAll(this, nodes);
|
||||
|
||||
// Nested templates must be processed before creation instructions so template()
|
||||
// instructions can be generated with the correct internal const count.
|
||||
this._nestedTemplateFns.forEach(buildTemplateFn => buildTemplateFn());
|
||||
|
||||
// Generate all the creation mode instructions (e.g. resolve bindings in listeners)
|
||||
const creationStatements = this._creationCodeFns.map((fn: () => o.Statement) => fn());
|
||||
|
||||
@ -208,8 +212,6 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
|
||||
}
|
||||
}
|
||||
|
||||
this._nestedTemplateFns.forEach(buildTemplateFn => buildTemplateFn());
|
||||
|
||||
return o.fn(
|
||||
// i.e. (rf: RenderFlags, ctx: any)
|
||||
[new o.FnParam(RENDER_FLAGS, o.NUMBER_TYPE), new o.FnParam(CONTEXT_NAME, null)],
|
||||
@ -727,9 +729,6 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
|
||||
parameters.push(this.constantPool.getConstLiteral(o.literalArr(attributeNames), true));
|
||||
}
|
||||
|
||||
// e.g. template(1, MyComp_Template_1)
|
||||
this.creationInstruction(template.sourceSpan, R3.templateCreate, trimTrailingNulls(parameters));
|
||||
|
||||
// e.g. p(1, 'forOf', ɵbind(ctx.items));
|
||||
const context = o.variable(CONTEXT_NAME);
|
||||
template.inputs.forEach(input => {
|
||||
@ -756,6 +755,12 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
|
||||
templateVisitor.buildTemplateFunction(template.children, template.variables);
|
||||
this.constantPool.statements.push(templateFunctionExpr.toDeclStmt(templateName, null));
|
||||
});
|
||||
|
||||
// e.g. template(1, MyComp_Template_1)
|
||||
this.creationInstruction(template.sourceSpan, R3.templateCreate, () => {
|
||||
parameters.splice(2, 0, o.literal(templateVisitor.getSlotCount()));
|
||||
return trimTrailingNulls(parameters);
|
||||
});
|
||||
}
|
||||
|
||||
// These should be handled in the template or element directly.
|
||||
@ -801,6 +806,9 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
|
||||
}
|
||||
|
||||
private allocateDataSlot() { return this._dataIndex++; }
|
||||
|
||||
getSlotCount() { return this._dataIndex; }
|
||||
|
||||
private bindingContext() { return `${this._bindingContext++}`; }
|
||||
|
||||
// Bindings must only be resolved after all local refs have been visited, so all
|
||||
|
Reference in New Issue
Block a user