mirror of
https://github.com/angular/angular.git
synced 2026-11-29 01:47:53 -05:00
refactor(compiler): pipeline phase to lexically resolve names in a view (#48580)
This commit introduces `phaseResolveNames()` to the template pipeline. This phase processes any `ir.LexicalReadExpr`s, which represent unresolved identifiers from the user's expressions in the template, and attempts to match them to concepts in the lexical scope of the template (such as local refs) or to a property read of a component or embedded view context. PR Close #48580
This commit is contained in:
committed by
Andrew Kushnir
parent
98613535c5
commit
ce04fdcad1
@@ -9,14 +9,16 @@
|
||||
import * as o from '../../../../output/output_ast';
|
||||
import type {ParseSourceSpan} from '../../../../parse_util';
|
||||
|
||||
import {ExpressionKind} from './enums';
|
||||
import {ExpressionKind, OpKind} from './enums';
|
||||
import {XrefId} from './operations';
|
||||
import {CreateOp} from './ops/create';
|
||||
import {UpdateOp} from './ops/update';
|
||||
|
||||
/**
|
||||
* An `o.Expression` subtype representing a logical expression in the intermediate representation.
|
||||
*/
|
||||
export type Expression = LexicalReadExpr|ReferenceExpr|ContextExpr|NextContextExpr|
|
||||
GetCurrentViewExpr|RestoreViewExpr|ResetViewExpr;
|
||||
GetCurrentViewExpr|RestoreViewExpr|ResetViewExpr|ReadVariableExpr;
|
||||
|
||||
/**
|
||||
* Transformer type which converts IR expressions into general `o.Expression`s (which may be an
|
||||
@@ -232,6 +234,79 @@ export class ResetViewExpr extends ExpressionBase {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read of a variable declared as an `ir.VariableOp` and referenced through its `ir.XrefId`.
|
||||
*/
|
||||
export class ReadVariableExpr extends ExpressionBase {
|
||||
readonly kind = ExpressionKind.ReadVariable;
|
||||
name: string|null = null;
|
||||
constructor(readonly xref: XrefId) {
|
||||
super();
|
||||
}
|
||||
|
||||
visitExpression(): void {}
|
||||
|
||||
isEquivalent(other: o.Expression): boolean {
|
||||
return other instanceof ReadVariableExpr && other.xref === this.xref;
|
||||
}
|
||||
|
||||
isConstant(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
override transformInternalExpressions(): void {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Visits all `Expression`s in the AST of `op` with the `visitor` function.
|
||||
*/
|
||||
export function visitExpressionsInOp(
|
||||
op: CreateOp|UpdateOp, visitor: (expr: Expression) => void): void {
|
||||
transformExpressionsInOp(op, (expr) => {
|
||||
visitor(expr);
|
||||
return expr;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform all `Expression`s in the AST of `op` with the `transform` function.
|
||||
*
|
||||
* All such operations will be replaced with the result of applying `transform`, which may be an
|
||||
* identity transformation.
|
||||
*/
|
||||
export function transformExpressionsInOp(
|
||||
op: CreateOp|UpdateOp, transform: ExpressionTransform): void {
|
||||
switch (op.kind) {
|
||||
case OpKind.Property:
|
||||
op.expression = transformExpressionsInExpression(op.expression, transform);
|
||||
break;
|
||||
case OpKind.Statement:
|
||||
transformExpressionsInStatement(op.statement, transform);
|
||||
break;
|
||||
case OpKind.Variable:
|
||||
op.initializer = transformExpressionsInExpression(op.initializer, transform);
|
||||
break;
|
||||
case OpKind.InterpolateText:
|
||||
for (let i = 0; i < op.expressions.length; i++) {
|
||||
op.expressions[i] = transformExpressionsInExpression(op.expressions[i], transform);
|
||||
}
|
||||
break;
|
||||
case OpKind.Listener:
|
||||
for (const innerOp of op.handlerOps) {
|
||||
transformExpressionsInOp(innerOp, transform);
|
||||
}
|
||||
break;
|
||||
case OpKind.Element:
|
||||
case OpKind.ElementStart:
|
||||
case OpKind.ElementEnd:
|
||||
case OpKind.Template:
|
||||
case OpKind.Text:
|
||||
// These operations contain no expressions.
|
||||
break;
|
||||
default:
|
||||
throw new Error(`AssertionError: transformExpressionsInOp doesn't handle ${OpKind[op.kind]}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform all `Expression`s in the AST of `expr` with the `transform` function.
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* @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 o from '../../../../output/output_ast';
|
||||
import * as ir from '../../ir';
|
||||
import {ComponentCompilation, ViewCompilation} from '../compilation';
|
||||
|
||||
/**
|
||||
* Resolves lexical references in views (`ir.LexicalReadExpr`) to either a target variable or to
|
||||
* property reads on the top-level component context.
|
||||
*
|
||||
* Also matches `ir.RestoreViewExpr` expressions with the variables of their corresponding saved
|
||||
* views.
|
||||
*/
|
||||
export function phaseResolveNames(cpl: ComponentCompilation): void {
|
||||
for (const [_, view] of cpl.views) {
|
||||
processLexicalScope(view, view.create, null);
|
||||
processLexicalScope(view, view.update, null);
|
||||
}
|
||||
}
|
||||
|
||||
function processLexicalScope(
|
||||
view: ViewCompilation, ops: ir.OpList<ir.CreateOp>|ir.OpList<ir.UpdateOp>,
|
||||
savedView: SavedView|null): void {
|
||||
// Maps names defined in the lexical scope of this template to the `ir.XrefId`s of the variable
|
||||
// declarations which represent those values.
|
||||
//
|
||||
// Since variables are generated in each view for the entire lexical scope (including any
|
||||
// identifiers from parent templates) only local variables need be considered here.
|
||||
const scope = new Map<string, ir.XrefId>();
|
||||
|
||||
// First, step through the operations list and:
|
||||
// 1) build up the `scope` mapping
|
||||
// 2) recurse into any listener functions
|
||||
for (const op of ops) {
|
||||
switch (op.kind) {
|
||||
case ir.OpKind.Variable:
|
||||
switch (op.variable.kind) {
|
||||
case ir.SemanticVariableKind.Identifier:
|
||||
// This variable represents some kind of identifier which can be used in the template.
|
||||
if (scope.has(op.variable.name)) {
|
||||
continue;
|
||||
}
|
||||
scope.set(op.variable.name, op.xref);
|
||||
break;
|
||||
case ir.SemanticVariableKind.SavedView:
|
||||
// This variable represents a snapshot of the current view context, and can be used to
|
||||
// restore that context within listener functions.
|
||||
savedView = {
|
||||
view: op.variable.view,
|
||||
variable: op.xref,
|
||||
};
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ir.OpKind.Listener:
|
||||
// Listener functions have separate variable declarations, so process them as a separate
|
||||
// lexical scope.
|
||||
processLexicalScope(view, op.handlerOps, savedView);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Next, use the `scope` mapping to match `ir.LexicalReadExpr` with defined names in the lexical
|
||||
// scope. Also, look for `ir.RestoreViewExpr`s and match them with the snapshotted view context
|
||||
// variable.
|
||||
for (const op of ops) {
|
||||
ir.transformExpressionsInOp(op, expr => {
|
||||
if (expr instanceof ir.LexicalReadExpr) {
|
||||
// `expr` is a read of a name within the lexical scope of this view.
|
||||
// Either that name is defined within the current view, or it represents a property from the
|
||||
// main component context.
|
||||
if (scope.has(expr.name)) {
|
||||
// This was a defined variable in the current scope.
|
||||
return new ir.ReadVariableExpr(scope.get(expr.name)!);
|
||||
} else {
|
||||
// Reading from the component context.
|
||||
return new o.ReadPropExpr(new ir.ContextExpr(view.tpl.root.xref), expr.name);
|
||||
}
|
||||
} else if (expr instanceof ir.RestoreViewExpr && typeof expr.view === 'number') {
|
||||
// `ir.RestoreViewExpr` happens in listener functions and restores a saved view from the
|
||||
// parent creation list. We expect to find that we captured the `savedView` previously, and
|
||||
// that it matches the expected view to be restored.
|
||||
if (savedView === null || savedView.view !== expr.view) {
|
||||
throw new Error(`AssertionError: no saved view ${expr.view} from view ${view.xref}`);
|
||||
}
|
||||
expr.view = new ir.ReadVariableExpr(savedView.variable);
|
||||
return expr;
|
||||
} else {
|
||||
return expr;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Information about a `SavedView` variable.
|
||||
*/
|
||||
interface SavedView {
|
||||
/**
|
||||
* The view `ir.XrefId` which was saved into this variable.
|
||||
*/
|
||||
view: ir.XrefId;
|
||||
|
||||
/**
|
||||
* The `ir.XrefId` of the variable into which the view was saved.
|
||||
*/
|
||||
variable: ir.XrefId;
|
||||
}
|
||||
Reference in New Issue
Block a user