mirror of
https://github.com/angular/angular.git
synced 2026-11-24 07:27:53 -05:00
refactor(compiler): template pipeline ops/exprs to represent input templates (#48580)
This commit adds the IR operations and expressions required to "ingest" a template into the template pipeline. They form the starting point for template compilation. Ingestion is implemented in a following commit. PR Close #48580
This commit is contained in:
committed by
Andrew Kushnir
parent
98b28548e7
commit
2e1fdb3e59
@@ -6,6 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
export * from './src/element';
|
||||
export * from './src/expression';
|
||||
export * from './src/enums';
|
||||
export * from './src/operations';
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google LLC All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import * as core from '../../../../core';
|
||||
import {splitNsName} from '../../../../ml_parser/tags';
|
||||
import * as o from '../../../../output/output_ast';
|
||||
|
||||
/**
|
||||
* Enumeration of the types of attributes which can be applied to an element.
|
||||
*/
|
||||
export enum ElementAttributeKind {
|
||||
/**
|
||||
* Static attributes.
|
||||
*/
|
||||
Attribute,
|
||||
|
||||
/**
|
||||
* Class bindings.
|
||||
*/
|
||||
Class,
|
||||
|
||||
/**
|
||||
* Style bindings.
|
||||
*/
|
||||
Style,
|
||||
|
||||
/**
|
||||
* Dynamic property or attribute bindings.
|
||||
*/
|
||||
Binding,
|
||||
|
||||
/**
|
||||
* Attributes on a template node.
|
||||
*/
|
||||
Template,
|
||||
|
||||
/**
|
||||
* Internationalized attributes.
|
||||
*/
|
||||
I18n,
|
||||
}
|
||||
|
||||
const FLYWEIGHT_ARRAY: ReadonlyArray<o.Expression> = Object.freeze<o.Expression[]>([]);
|
||||
|
||||
/**
|
||||
* Container for all of the various kinds of attributes which are applied on an element.
|
||||
*/
|
||||
export class ElementAttributes {
|
||||
private known = new Set<string>();
|
||||
private byKind = new Map<ElementAttributeKind, o.Expression[]>;
|
||||
|
||||
projectAs: string|null = null;
|
||||
|
||||
get attributes(): ReadonlyArray<o.Expression> {
|
||||
return this.byKind.get(ElementAttributeKind.Attribute) ?? FLYWEIGHT_ARRAY;
|
||||
}
|
||||
|
||||
get classes(): ReadonlyArray<o.Expression> {
|
||||
return this.byKind.get(ElementAttributeKind.Class) ?? FLYWEIGHT_ARRAY;
|
||||
}
|
||||
|
||||
get styles(): ReadonlyArray<o.Expression> {
|
||||
return this.byKind.get(ElementAttributeKind.Style) ?? FLYWEIGHT_ARRAY;
|
||||
}
|
||||
|
||||
get bindings(): ReadonlyArray<o.Expression> {
|
||||
return this.byKind.get(ElementAttributeKind.Binding) ?? FLYWEIGHT_ARRAY;
|
||||
}
|
||||
|
||||
get template(): ReadonlyArray<o.Expression> {
|
||||
return this.byKind.get(ElementAttributeKind.Template) ?? FLYWEIGHT_ARRAY;
|
||||
}
|
||||
|
||||
get i18n(): ReadonlyArray<o.Expression> {
|
||||
return this.byKind.get(ElementAttributeKind.I18n) ?? FLYWEIGHT_ARRAY;
|
||||
}
|
||||
|
||||
add(kind: ElementAttributeKind, name: string, value: o.Expression|null): void {
|
||||
if (this.known.has(name)) {
|
||||
return;
|
||||
}
|
||||
this.known.add(name);
|
||||
const array = this.arrayFor(kind);
|
||||
array.push(...getAttributeNameLiterals(name));
|
||||
if (value !== null) {
|
||||
array.push(value);
|
||||
}
|
||||
}
|
||||
|
||||
private arrayFor(kind: ElementAttributeKind): o.Expression[] {
|
||||
if (!this.byKind.has(kind)) {
|
||||
this.byKind.set(kind, []);
|
||||
}
|
||||
return this.byKind.get(kind)!;
|
||||
}
|
||||
}
|
||||
|
||||
function getAttributeNameLiterals(name: string): o.LiteralExpr[] {
|
||||
const [attributeNamespace, attributeName] = splitNsName(name);
|
||||
const nameLiteral = o.literal(attributeName);
|
||||
|
||||
if (attributeNamespace) {
|
||||
return [
|
||||
o.literal(core.AttributeMarker.NamespaceURI), o.literal(attributeNamespace), nameLiteral
|
||||
];
|
||||
}
|
||||
|
||||
return [nameLiteral];
|
||||
}
|
||||
|
||||
export function assertIsElementAttributes(attrs: any): asserts attrs is ElementAttributes {
|
||||
if (!(attrs instanceof ElementAttributes)) {
|
||||
throw new Error(
|
||||
`AssertionError: ElementAttributes has already been coalesced into the view constants`);
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,46 @@ export enum OpKind {
|
||||
* An operation which wraps an output AST statement.
|
||||
*/
|
||||
Statement,
|
||||
|
||||
/**
|
||||
* An operation to begin rendering of an element.
|
||||
*/
|
||||
ElementStart,
|
||||
|
||||
/**
|
||||
* An operation to render an element with no children.
|
||||
*/
|
||||
Element,
|
||||
|
||||
/**
|
||||
* An operation which declares an embedded view.
|
||||
*/
|
||||
Template,
|
||||
|
||||
/**
|
||||
* An operation to end rendering of an element previously started with `ElementStart`.
|
||||
*/
|
||||
ElementEnd,
|
||||
|
||||
/**
|
||||
* An operation to render a text node.
|
||||
*/
|
||||
Text,
|
||||
|
||||
/**
|
||||
* An operation declaring an event listener for an element.
|
||||
*/
|
||||
Listener,
|
||||
|
||||
/**
|
||||
* An operation to interpolate text into a text node.
|
||||
*/
|
||||
InterpolateText,
|
||||
|
||||
/**
|
||||
* An operation to bind an expression to a property of an element.
|
||||
*/
|
||||
Property,
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,9 +6,221 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ListEndOp, StatementOp} from './shared';
|
||||
import {ElementAttributes} from '../element';
|
||||
import {OpKind} from '../enums';
|
||||
import {Op, OpList, XrefId} from '../operations';
|
||||
|
||||
import {ListEndOp, NEW_OP, StatementOp} from './shared';
|
||||
import type {UpdateOp} from './update';
|
||||
|
||||
/**
|
||||
* An operation usable on the creation side of the IR.
|
||||
*/
|
||||
export type CreateOp = ListEndOp<CreateOp>|StatementOp<CreateOp>;
|
||||
export type CreateOp = ListEndOp<CreateOp>|StatementOp<CreateOp>|ElementOp|ElementStartOp|
|
||||
ElementEndOp|TemplateOp|TextOp|ListenerOp;
|
||||
|
||||
/**
|
||||
* Representation of a local reference on an element.
|
||||
*/
|
||||
export interface LocalRef {
|
||||
/**
|
||||
* User-defined name of the local ref variable.
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Target of the local reference variable (often `''`).
|
||||
*/
|
||||
target: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base interface for `Element`, `ElementStart`, and `Template` operations, containing common fields
|
||||
* used to represent their element-like nature.
|
||||
*/
|
||||
export interface ElementOpBase extends Op<CreateOp> {
|
||||
kind: OpKind.Element|OpKind.ElementStart|OpKind.Template;
|
||||
|
||||
/**
|
||||
* `XrefId` allocated for this element.
|
||||
*
|
||||
* This ID is used to reference this element from other IR structures.
|
||||
*/
|
||||
xref: XrefId;
|
||||
|
||||
/**
|
||||
* The HTML tag name for this element.
|
||||
*/
|
||||
tag: string;
|
||||
|
||||
/**
|
||||
* Attributes of various kinds on this element.
|
||||
*
|
||||
* Before attribute processing, this is an `ElementAttributes` structure representing the
|
||||
* attributes on this element.
|
||||
*
|
||||
* After processing, it's a `ConstIndex` pointer into the shared `consts` array of the component
|
||||
* compilation.
|
||||
*/
|
||||
attributes: ElementAttributes|ConstIndex|null;
|
||||
|
||||
/**
|
||||
* Local references to this element.
|
||||
*
|
||||
* Before local ref processing, this is an array of `LocalRef` declarations.
|
||||
*
|
||||
* After processing, it's a `ConstIndex` pointer into the shared `consts` array of the component
|
||||
* compilation.
|
||||
*/
|
||||
localRefs: LocalRef[]|ConstIndex|null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logical operation representing the start of an element in the creation IR.
|
||||
*/
|
||||
export interface ElementStartOp extends ElementOpBase {
|
||||
kind: OpKind.ElementStart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an `ElementStartOp`.
|
||||
*/
|
||||
export function createElementStartOp(tag: string, xref: XrefId): ElementStartOp {
|
||||
return {
|
||||
kind: OpKind.ElementStart,
|
||||
xref,
|
||||
tag,
|
||||
attributes: new ElementAttributes(),
|
||||
localRefs: [],
|
||||
...NEW_OP,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Logical operation representing an element with no children in the creation IR.
|
||||
*/
|
||||
export interface ElementOp extends ElementOpBase {
|
||||
kind: OpKind.Element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logical operation representing an embedded view declaration in the creation IR.
|
||||
*/
|
||||
export interface TemplateOp extends ElementOpBase {
|
||||
kind: OpKind.Template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a `TemplateOp`.
|
||||
*/
|
||||
export function createTemplateOp(xref: XrefId, tag: string): TemplateOp {
|
||||
return {
|
||||
kind: OpKind.Template,
|
||||
xref,
|
||||
attributes: new ElementAttributes(),
|
||||
tag,
|
||||
localRefs: [],
|
||||
...NEW_OP,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Logical operation representing the end of an element structure in the creation IR.
|
||||
*
|
||||
* Pairs with an `ElementStart` operation.
|
||||
*/
|
||||
export interface ElementEndOp extends Op<CreateOp> {
|
||||
kind: OpKind.ElementEnd;
|
||||
|
||||
/**
|
||||
* The `XrefId` of the element declared via `ElementStart`.
|
||||
*/
|
||||
xref: XrefId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an `ElementEndOp`.
|
||||
*/
|
||||
export function createElementEndOp(xref: XrefId): ElementEndOp {
|
||||
return {
|
||||
kind: OpKind.ElementEnd,
|
||||
xref,
|
||||
...NEW_OP,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Logical operation representing a text node in the creation IR.
|
||||
*/
|
||||
export interface TextOp extends Op<CreateOp> {
|
||||
kind: OpKind.Text;
|
||||
|
||||
/**
|
||||
* `XrefId` used to reference this text node in other IR structures.
|
||||
*/
|
||||
xref: XrefId;
|
||||
|
||||
/**
|
||||
* The static initial value of the text node.
|
||||
*/
|
||||
initialValue: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a `TextOp`.
|
||||
*/
|
||||
export function createTextOp(xref: XrefId, initialValue: string): TextOp {
|
||||
return {
|
||||
kind: OpKind.Text,
|
||||
xref,
|
||||
initialValue,
|
||||
...NEW_OP,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Logical operation representing an event listener on an element in the creation IR.
|
||||
*/
|
||||
export interface ListenerOp extends Op<CreateOp> {
|
||||
kind: OpKind.Listener;
|
||||
|
||||
/**
|
||||
* `XrefId` of the element on which the event listener is bound.
|
||||
*/
|
||||
xref: XrefId;
|
||||
|
||||
/**
|
||||
* Name of the event which is being listened to.
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* A list of `UpdateOp`s representing the body of the event listener.
|
||||
*/
|
||||
handlerOps: OpList<UpdateOp>;
|
||||
|
||||
/**
|
||||
* Name of the function
|
||||
*/
|
||||
handlerFnName: string|null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a `ListenerOp`.
|
||||
*/
|
||||
export function createListenerOp(xref: XrefId, name: string): ListenerOp {
|
||||
return {
|
||||
kind: OpKind.Listener,
|
||||
xref,
|
||||
name,
|
||||
handlerOps: new OpList(),
|
||||
handlerFnName: null,
|
||||
...NEW_OP,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An index into the `consts` array which is shared across the compilation of all views in a
|
||||
* component.
|
||||
*/
|
||||
export type ConstIndex = number&{__brand: 'ConstIndex'};
|
||||
|
||||
@@ -6,9 +6,92 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ListEndOp, StatementOp} from './shared';
|
||||
import * as o from '../../../../../output/output_ast';
|
||||
import {OpKind} from '../enums';
|
||||
import {Op, XrefId} from '../operations';
|
||||
|
||||
import {ListEndOp, NEW_OP, StatementOp} from './shared';
|
||||
|
||||
/**
|
||||
* An operation usable on the update side of the IR.
|
||||
*/
|
||||
export type UpdateOp = ListEndOp<UpdateOp>|StatementOp<UpdateOp>;
|
||||
export type UpdateOp = ListEndOp<UpdateOp>|StatementOp<UpdateOp>|PropertyOp|InterpolateTextOp;
|
||||
|
||||
/**
|
||||
* A logical operation to perform string interpolation on a text node.
|
||||
*
|
||||
* Interpolation inputs are stored as static `string`s and dynamic `o.Expression`s, in separate
|
||||
* arrays. Thus, the interpolation `A{{b}}C{{d}}E` is stored as 3 static strings `['A', 'C', 'E']`
|
||||
* and 2 dynamic expressions `[b, d]`.
|
||||
*/
|
||||
export interface InterpolateTextOp extends Op<UpdateOp> {
|
||||
kind: OpKind.InterpolateText;
|
||||
|
||||
/**
|
||||
* Reference to the text node to which the interpolation is bound.
|
||||
*/
|
||||
xref: XrefId;
|
||||
|
||||
/**
|
||||
* All of the literal strings in the text interpolation, in order.
|
||||
*
|
||||
* Conceptually interwoven around the `expressions`.
|
||||
*/
|
||||
strings: string[];
|
||||
|
||||
/**
|
||||
* All of the dynamic expressions in the text interpolation, in order.
|
||||
*
|
||||
* Conceptually interwoven in between the `strings`.
|
||||
*/
|
||||
expressions: o.Expression[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an `InterpolationTextOp`.
|
||||
*/
|
||||
export function createInterpolateTextOp(
|
||||
xref: XrefId, strings: string[], expressions: o.Expression[]): InterpolateTextOp {
|
||||
return {
|
||||
kind: OpKind.InterpolateText,
|
||||
xref,
|
||||
strings,
|
||||
expressions,
|
||||
...NEW_OP,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A logical operation representing binding to a property in the update IR.
|
||||
*/
|
||||
export interface PropertyOp extends Op<UpdateOp> {
|
||||
kind: OpKind.Property;
|
||||
|
||||
/**
|
||||
* Reference to the element on which the property is bound.
|
||||
*/
|
||||
xref: XrefId;
|
||||
|
||||
/**
|
||||
* Name of the bound property.
|
||||
*/
|
||||
name: string;
|
||||
|
||||
/**
|
||||
* Expression which is bound to the property.
|
||||
*/
|
||||
expression: o.Expression;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a `PropertyOp`.
|
||||
*/
|
||||
export function createPropertyOp(xref: XrefId, name: string, expression: o.Expression): PropertyOp {
|
||||
return {
|
||||
kind: OpKind.Property,
|
||||
xref,
|
||||
name,
|
||||
expression,
|
||||
...NEW_OP,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -86,6 +86,11 @@ export class ViewCompilation {
|
||||
* ops(): Generator<ir.CreateOp|ir.UpdateOp> {
|
||||
for (const op of this.create) {
|
||||
yield op;
|
||||
if (op.kind === ir.OpKind.Listener) {
|
||||
for (const listenerOp of op.handlerOps) {
|
||||
yield listenerOp;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const op of this.update) {
|
||||
yield op;
|
||||
|
||||
Reference in New Issue
Block a user