From 9d0fa36191a9e174e3eb7470e7350e2d3b14cc60 Mon Sep 17 00:00:00 2001 From: Dylan Hunn Date: Fri, 8 Dec 2023 17:04:56 -0800 Subject: [PATCH] refactor(compiler): Completely eliminate `BindingFlags`. It turns out that `BindingFlags.BindingTargetsTemplate` is actally a redundant property! It will be true in either of the following cases: 1. The template is a normal non-structural `ng-template`. We already know this from `TemplateKind`. 2. The binding came from `templateAttrs` (instead of `attrs`). We have this information in `BindingFlags.IsStructuralTemplateAttribute`. Therefore, I can just eliminate `BindingFlags.BindingTargetsTemplate`. There's no reason to keep `BindingFlags` around for a single value, so I convert `BindingFlags.IsStructuralTemplateAttribute` to a boolean parameter (with the eventual goal of eliminating it entirely). --- .../src/template/pipeline/src/ingest.ts | 44 +++++-------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/packages/compiler/src/template/pipeline/src/ingest.ts b/packages/compiler/src/template/pipeline/src/ingest.ts index 7bf0486a0bec0a..d50261cf2c154b 100644 --- a/packages/compiler/src/template/pipeline/src/ingest.ts +++ b/packages/compiler/src/template/pipeline/src/ingest.ts @@ -772,20 +772,6 @@ const BINDING_KINDS = new Map([ [e.BindingType.Animation, ir.BindingKind.Animation], ]); -enum TemplateBindingFlags { - None = 0b000, - - /** - * The binding belongs to the `` side of a `t.Template`. - */ - BindingTargetsTemplate = 0b01, - - /** - * The binding is on a structural directive. - */ - IsStructuralTemplateAttribute = 0b10, -} - /** * Checks whether the given template is a plain ng-template (as opposed to another kind of template * such as a structural directive template or control flow template). This is checked based on the @@ -875,25 +861,17 @@ function ingestElementBindings( function ingestTemplateBindings( unit: ViewCompilationUnit, op: ir.ElementOpBase, template: t.Template, templateKind: ir.TemplateKind|null): void { - let flags: TemplateBindingFlags = TemplateBindingFlags.None; let bindings = new Array(); - if (isPlainTemplate(template)) { - flags |= TemplateBindingFlags.BindingTargetsTemplate; - } - - const templateAttrFlags = flags | TemplateBindingFlags.BindingTargetsTemplate | - TemplateBindingFlags.IsStructuralTemplateAttribute; for (const attr of template.templateAttrs) { if (attr instanceof t.TextAttribute) { bindings.push(createTemplateBinding( unit, op.xref, e.BindingType.Attribute, attr.name, o.literal(attr.value), null, - SecurityContext.NONE, true, templateAttrFlags, templateKind, asMessage(attr.i18n), - attr.sourceSpan)); + SecurityContext.NONE, true, 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, templateAttrFlags, templateKind, asMessage(attr.i18n), attr.sourceSpan)); + false, true, templateKind, asMessage(attr.i18n), attr.sourceSpan)); } } @@ -901,14 +879,14 @@ function ingestTemplateBindings( // Attribute literal bindings, such as `attr.foo="bar"`. bindings.push(createTemplateBinding( unit, op.xref, e.BindingType.Attribute, attr.name, o.literal(attr.value), null, - SecurityContext.NONE, true, flags, templateKind, asMessage(attr.i18n), attr.sourceSpan)); + SecurityContext.NONE, true, false, templateKind, asMessage(attr.i18n), attr.sourceSpan)); } for (const input of template.inputs) { // All 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, flags, templateKind, asMessage(input.i18n), + input.securityContext, false, false, templateKind, asMessage(input.i18n), input.sourceSpan)); } @@ -933,7 +911,6 @@ function ingestTemplateBindings( output.sourceSpan)); } - // TODO: Perhaps we could do this in a phase? (It likely wouldn't change the slot indices.) if (bindings.some(b => b?.i18nMessage) !== null) { unit.create.push( @@ -944,10 +921,12 @@ function ingestTemplateBindings( function createTemplateBinding( view: ViewCompilationUnit, xref: ir.XrefId, type: e.BindingType, name: string, value: e.AST|o.Expression, unit: string|null, securityContext: SecurityContext, - isTextBinding: boolean, flags: TemplateBindingFlags, templateKind: ir.TemplateKind|null, - i18nMessage: i18n.Message|null, sourceSpan: ParseSourceSpan): ir.BindingOp| - ir.ExtractedAttributeOp|null { - if (!(flags & TemplateBindingFlags.BindingTargetsTemplate) && + isTextBinding: boolean, isStructuralTemplateAttribute: boolean, + templateKind: ir.TemplateKind|null, i18nMessage: i18n.Message|null, + sourceSpan: ParseSourceSpan): ir.BindingOp|ir.ExtractedAttributeOp|null { + const bindingTargetsTemplate = + isStructuralTemplateAttribute || templateKind === ir.TemplateKind.NgTemplate; + if (!bindingTargetsTemplate && (type === e.BindingType.Property || type === e.BindingType.Class || type === e.BindingType.Style)) { // Because this binding doesn't really target the ng-template, it must be a binding on an inner @@ -967,8 +946,7 @@ function createTemplateBinding( return ir.createBindingOp( xref, BINDING_KINDS.get(type)!, name, convertAstWithInterpolation(view.job, value, i18nMessage), unit, securityContext, - isTextBinding, !!(flags & TemplateBindingFlags.IsStructuralTemplateAttribute), templateKind, - i18nMessage ?? null, sourceSpan); + isTextBinding, isStructuralTemplateAttribute, templateKind, i18nMessage ?? null, sourceSpan); } function makeListenerHandlerOps(