Skip to content

Commit

Permalink
refactor(compiler): Make some fixups to template binidng ingestion (#…
Browse files Browse the repository at this point in the history
…53457)

Responding to comments on #53457 about the previous sequence of template binding PRs.

PR Close #53457
  • Loading branch information
dylhunn authored and alxhub committed Dec 11, 2023
1 parent 7967d28 commit 7fa791e
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions packages/compiler/src/template/pipeline/src/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ function asMessage(i18nMeta: i18n.I18nMeta|null|undefined): i18n.Message|null {
return null;
}
if (!(i18nMeta instanceof i18n.Message)) {
throw Error(`Unhandled i18n metadata type for binding: ${i18nMeta.constructor.name}`);
throw Error(`Expected i18n meta to be a Message, but got: ${i18nMeta.constructor.name}`);
}
return i18nMeta;
}
Expand Down Expand Up @@ -867,27 +867,26 @@ function ingestTemplateBindings(
if (attr instanceof t.TextAttribute) {
bindings.push(createTemplateBinding(
unit, op.xref, e.BindingType.Attribute, attr.name, attr.value, null, SecurityContext.NONE,
true, true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
} else {
bindings.push(createTemplateBinding(
unit, op.xref, attr.type, attr.name, astOf(attr.value), attr.unit, attr.securityContext,
false, true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
true, templateKind, asMessage(attr.i18n), attr.sourceSpan));
}
}

for (const attr of template.attributes) {
// Attribute literal bindings, such as `attr.foo="bar"`.
bindings.push(createTemplateBinding(
unit, op.xref, e.BindingType.Attribute, attr.name, attr.value, null, SecurityContext.NONE,
true, false, templateKind, asMessage(attr.i18n), attr.sourceSpan));
false, templateKind, asMessage(attr.i18n), attr.sourceSpan));
}

for (const input of template.inputs) {
// Dynamic bindings (both attribute and property bindings).
bindings.push(createTemplateBinding(
unit, op.xref, input.type, input.name, astOf(input.value), input.unit,
input.securityContext, false, false, templateKind, asMessage(input.i18n),
input.sourceSpan));
input.securityContext, false, templateKind, asMessage(input.i18n), input.sourceSpan));
}

unit.create.push(bindings.filter(
Expand All @@ -905,7 +904,8 @@ function ingestTemplateBindings(
makeListenerHandlerOps(unit, output.handler, output.handlerSpan), output.phase, false,
output.sourceSpan));
}
if (templateKind === ir.TemplateKind.Structural && output.type === e.ParsedEventType.Regular) {
if (templateKind === ir.TemplateKind.Structural &&
output.type !== e.ParsedEventType.Animation) {
// Animation bindings are excluded from the structural template's const array.
unit.create.push(ir.createExtractedAttributeOp(
op.xref, ir.BindingKind.Property, output.name, null, null, null));
Expand Down Expand Up @@ -938,24 +938,22 @@ function ingestTemplateBindings(
* string literal if the parser considered it a text binding.
* @param unit If the binding has a unit (e.g. `px` for style bindings), then this is the unit.
* @param securityContext The security context of the binding.
* @param isTextBinding Whether the binding is a text binding according to the parser (e.g. `<div
* literal="foo">`). Even if this is false, the binding still might be an extractable constant
* expression (see the later attribute extraction phase).
* @param isStructuralTemplateAttribute Whether this binding actually applies to the structural
* ng-template. For example, an `ngFor` would actually apply to the structural template. (Most
* bindings on structural elements target the inner element, not the template.)
* @param templateKind Whether this is an explicit `ng-template` or an implicit template created by
* a structural directive.
* a structural directive. This should never be a block template.
* @param i18nMessage The i18n metadata for the binding, if any.
* @param sourceSpan The source span of the binding.
* @returns An IR binding op, or null if the binding should be skipped.
*/
function createTemplateBinding(
view: ViewCompilationUnit, xref: ir.XrefId, type: e.BindingType, name: string,
value: e.AST|string, unit: string|null, securityContext: SecurityContext,
isTextBinding: boolean, isStructuralTemplateAttribute: boolean,
templateKind: ir.TemplateKind|null, i18nMessage: i18n.Message|null,
sourceSpan: ParseSourceSpan): ir.BindingOp|ir.ExtractedAttributeOp|null {
isStructuralTemplateAttribute: boolean, templateKind: ir.TemplateKind|null,
i18nMessage: i18n.Message|null, sourceSpan: ParseSourceSpan): ir.BindingOp|
ir.ExtractedAttributeOp|null {
const isTextBinding = typeof value === 'string';
// If this is a structural template, then several kinds of bindings should not result in an
// update instruction.
if (templateKind === ir.TemplateKind.Structural) {
Expand Down

0 comments on commit 7fa791e

Please sign in to comment.