Skip to content

Commit

Permalink
refactor(compiler): cleanup unused code in template builder pipeline (#…
Browse files Browse the repository at this point in the history
…54654)

This removes some unused code and fixes a few typos across the pipeline code.

PR Close #54654
  • Loading branch information
cexbrayat authored and pkozlowski-opensource committed Mar 4, 2024
1 parent 6531e4c commit 5e32a77
Show file tree
Hide file tree
Showing 17 changed files with 22 additions and 66 deletions.
29 changes: 2 additions & 27 deletions packages/compiler/src/template/pipeline/ir/src/enums.ts
Expand Up @@ -360,16 +360,6 @@ export enum ExpressionKind {
*/
ReadTemporaryExpr,

/**
* An expression representing a sanitizer function.
*/
SanitizerExpr,

/**
* An expression representing a function to create trusted values.
*/
TrustedValueFnExpr,

/**
* An expression that will cause a literal slot index to be emitted.
*/
Expand All @@ -380,12 +370,6 @@ export enum ExpressionKind {
*/
ConditionalCase,

/**
* A variable for use inside a repeater, providing one of the ambiently-available context
* properties ($even, $first, etc.).
*/
DerivedRepeaterVar,

/**
* An expression that will be automatically extracted to the component const array.
*/
Expand Down Expand Up @@ -442,15 +426,6 @@ export enum CompatibilityMode {
TemplateDefinitionBuilder,
}

/**
* Enumeration of the different kinds of `@defer` secondary blocks.
*/
export enum DeferSecondaryKind {
Loading,
Placeholder,
Error,
}

/**
* Enumeration of the types of attributes which can be applied to an element.
*/
Expand Down Expand Up @@ -507,7 +482,7 @@ export enum I18nParamResolutionTime {
Creation,

/**
* Param is resolved during post-processing. This should be used for params who's value comes from
* Param is resolved during post-processing. This should be used for params whose value comes from
* an ICU.
*/
Postproccessing
Expand Down Expand Up @@ -536,7 +511,7 @@ export enum I18nParamValueFlags {
None = 0b0000,

/**
* This value represtents an element tag.
* This value represents an element tag.
*/
ElementTag = 0b1,

Expand Down
7 changes: 0 additions & 7 deletions packages/compiler/src/template/pipeline/ir/src/ops/create.ts
Expand Up @@ -1387,10 +1387,3 @@ export function createI18nAttributesOp(
* component.
*/
export type ConstIndex = number&{__brand: 'ConstIndex'};

export function literalOrArrayLiteral(value: any): o.Expression {
if (Array.isArray(value)) {
return o.literalArr(value.map(literalOrArrayLiteral));
}
return o.literal(value, o.INFERRED_TYPE);
}
9 changes: 0 additions & 9 deletions packages/compiler/src/template/pipeline/ir/src/traits.ts
Expand Up @@ -122,15 +122,6 @@ export const TRAIT_CONSUMES_VARS: ConsumesVarsTrait = {
[ConsumesVarsTrait]: true,
} as const;

/**
* Default values for `UsesVarOffset` fields (used with the spread operator to initialize
* implementors of this trait).
*/
export const TRAIT_USES_VAR_OFFSET: UsesVarOffsetTrait = {
[UsesVarOffset]: true,
varOffset: null,
} as const;

/**
* Test whether an operation implements `ConsumesSlotOpTrait`.
*/
Expand Down
1 change: 0 additions & 1 deletion packages/compiler/src/template/pipeline/src/ingest.ts
Expand Up @@ -565,7 +565,6 @@ function ingestDeferBlock(unit: ViewCompilationUnit, deferBlock: t.DeferredBlock
function ingestIcu(unit: ViewCompilationUnit, icu: t.Icu) {
if (icu.i18n instanceof i18n.Message && isSingleI18nIcu(icu.i18n)) {
const xref = unit.job.allocateXrefId();
const icuNode = icu.i18n.nodes[0];
unit.create.push(ir.createIcuStartOp(xref, icu.i18n, icuFromI18nMessage(icu.i18n).name, null!));
for (const [placeholder, text] of Object.entries({...icu.vars, ...icu.placeholders})) {
if (text instanceof t.BoundText) {
Expand Down
Expand Up @@ -38,7 +38,7 @@ export function extractAttributes(job: CompilationJob): void {
}

ir.OpList.insertBefore<ir.CreateOp>(
// Deliberaly null i18nMessage value
// Deliberately null i18nMessage value
ir.createExtractedAttributeOp(
op.target, bindingKind, null, op.name, /* expression */ null,
/* i18nContext */ null,
Expand Down
Expand Up @@ -133,7 +133,7 @@ class ElementAttributes {

constructor(private compatibility: ir.CompatibilityMode) {}

private isKnown(kind: ir.BindingKind, name: string, value: o.Expression|null) {
private isKnown(kind: ir.BindingKind, name: string) {
const nameToValue = this.known.get(kind) ?? new Set<string>();
this.known.set(kind, nameToValue);
if (nameToValue.has(name)) {
Expand All @@ -151,7 +151,7 @@ class ElementAttributes {
const allowDuplicates = this.compatibility === ir.CompatibilityMode.TemplateDefinitionBuilder &&
(kind === ir.BindingKind.Attribute || kind === ir.BindingKind.ClassName ||
kind === ir.BindingKind.StyleProperty);
if (!allowDuplicates && this.isKnown(kind, name, value)) {
if (!allowDuplicates && this.isKnown(kind, name)) {
return;
}

Expand Down
Expand Up @@ -143,7 +143,7 @@ function createI18nMessage(
*/
function formatIcuPlaceholder(op: ir.IcuPlaceholderOp) {
if (op.strings.length !== op.expressionPlaceholders.length + 1) {
throw Error(`AsserionError: Invalid ICU placeholder with ${op.strings.length} strings and ${
throw Error(`AssertionError: Invalid ICU placeholder with ${op.strings.length} strings and ${
op.expressionPlaceholders.length} expressions`);
}
const values = op.expressionPlaceholders.map(formatValue);
Expand Down
4 changes: 2 additions & 2 deletions packages/compiler/src/template/pipeline/src/phases/naming.ts
Expand Up @@ -89,8 +89,8 @@ function addNamesToView(
const emptyView = unit.job.views.get(op.emptyView)!;
// Repeater empty view function is at slot +2 (metadata is in the first slot).
addNamesToView(
emptyView, `${baseName}_${`${op.functionNameSuffix}Empty`}_${op.handle.slot + 2}`,
state, compatibility);
emptyView, `${baseName}_${op.functionNameSuffix}Empty_${op.handle.slot + 2}`, state,
compatibility);
}
// Repeater primary view function is at slot +1 (metadata is in the first slot).
addNamesToView(
Expand Down
Expand Up @@ -47,8 +47,8 @@ function propagateI18nBlocksToTemplates(
case ir.OpKind.RepeaterCreate:
// Propagate i18n blocks to the @for template.
const forView = unit.job.views.get(op.xref)!;
subTemplateIndex = propagateI18nBlocksForView(
unit.job.views.get(op.xref)!, i18nBlock, op.i18nPlaceholder, subTemplateIndex);
subTemplateIndex =
propagateI18nBlocksForView(forView, i18nBlock, op.i18nPlaceholder, subTemplateIndex);
// Then if there's an @empty template, propagate the i18n blocks for it as well.
if (op.emptyView !== null) {
subTemplateIndex = propagateI18nBlocksForView(
Expand Down
Expand Up @@ -10,7 +10,7 @@ import * as ir from '../../ir';
import type {CompilationJob} from '../compilation';

/**
* Bidningd with no content can be safely deleted.
* Binding with no content can be safely deleted.
*/
export function removeEmptyBindings(job: CompilationJob): void {
for (const unit of job.units) {
Expand Down
Expand Up @@ -8,21 +8,20 @@

import * as o from '../../../../output/output_ast';
import * as ir from '../../ir';
import type {CompilationJob, CompilationUnit} from '../compilation';
import type {CompilationJob} from '../compilation';

/**
* Any variable inside a listener with the name `$event` will be transformed into a output lexical
* read immediately, and does not participate in any of the normal logic for handling variables.
*/
export function resolveDollarEvent(job: CompilationJob): void {
for (const unit of job.units) {
transformDollarEvent(unit, unit.create);
transformDollarEvent(unit, unit.update);
transformDollarEvent(unit.create);
transformDollarEvent(unit.update);
}
}

function transformDollarEvent(
unit: CompilationUnit, ops: ir.OpList<ir.CreateOp>|ir.OpList<ir.UpdateOp>): void {
function transformDollarEvent(ops: ir.OpList<ir.CreateOp>|ir.OpList<ir.UpdateOp>): void {
for (const op of ops) {
if (op.kind === ir.OpKind.Listener || op.kind === ir.OpKind.TwoWayListener) {
ir.transformExpressionsInOp(op, (expr) => {
Expand Down
Expand Up @@ -268,7 +268,7 @@ function recordTemplateClose(
job: ComponentCompilationJob, view: ViewCompilationUnit, slot: number,
i18nPlaceholder: i18n.TagPlaceholder|i18n.BlockPlaceholder, i18nContext: ir.I18nContextOp,
i18nBlock: ir.I18nStartOp, structuralDirective?: ir.TemplateOp) {
const {startName, closeName} = i18nPlaceholder;
const {closeName} = i18nPlaceholder;
const flags = ir.I18nParamValueFlags.TemplateTag | ir.I18nParamValueFlags.CloseTag;
// Self-closing tags don't have a closing tag placeholder, instead the template's closing is
// recorded via an additional flag on the template start value.
Expand Down
Expand Up @@ -14,14 +14,14 @@ import {ComponentCompilationJob} from '../compilation';
*/
export function resolveI18nExpressionPlaceholders(job: ComponentCompilationJob) {
// Record all of the i18n context ops, and the sub-template index for each i18n op.
const subTemplateIndicies = new Map<ir.XrefId, number|null>();
const subTemplateIndices = new Map<ir.XrefId, number|null>();
const i18nContexts = new Map<ir.XrefId, ir.I18nContextOp>();
const icuPlaceholders = new Map<ir.XrefId, ir.IcuPlaceholderOp>();
for (const unit of job.units) {
for (const op of unit.create) {
switch (op.kind) {
case ir.OpKind.I18nStart:
subTemplateIndicies.set(op.xref, op.subTemplateIndex);
subTemplateIndices.set(op.xref, op.subTemplateIndex);
break;
case ir.OpKind.I18nContext:
i18nContexts.set(op.xref, op);
Expand All @@ -47,7 +47,7 @@ export function resolveI18nExpressionPlaceholders(job: ComponentCompilationJob)
for (const op of unit.update) {
if (op.kind === ir.OpKind.I18nExpression) {
const index = expressionIndices.get(referenceIndex(op)) || 0;
const subTemplateIndex = subTemplateIndicies.get(op.i18nOwner) ?? null;
const subTemplateIndex = subTemplateIndices.get(op.i18nOwner) ?? null;
const value: ir.I18nParamValue = {
value: index,
subTemplateIndex: subTemplateIndex,
Expand Down
Expand Up @@ -76,7 +76,7 @@ function processLexicalScope(
// Listeners were already processed above with their own scopes.
continue;
}
ir.transformExpressionsInOp(op, (expr, flags) => {
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
Expand Down
Expand Up @@ -8,7 +8,7 @@

import * as o from '../../../../output/output_ast';
import * as ir from '../../ir';
import type {CompilationJob, CompilationUnit} from '../compilation';
import type {CompilationJob} from '../compilation';

/**
* Find all assignments and usages of temporary variables, which are linked to each other with cross
Expand Down
Expand Up @@ -53,7 +53,7 @@ export function countVariables(job: CompilationJob): void {
});
}

// Compatiblity mode pass for pure function offsets (as explained above).
// Compatibility mode pass for pure function offsets (as explained above).
if (job.compatibility === ir.CompatibilityMode.TemplateDefinitionBuilder) {
for (const op of unit.ops()) {
ir.visitExpressionsInOp(op, expr => {
Expand Down
Expand Up @@ -211,7 +211,6 @@ function optimizeVariablesInOpList(
const toInline: ir.XrefId[] = [];
for (const [id, count] of varUsages) {
const decl = varDecls.get(id)!;
const varInfo = opMap.get(decl as ir.CreateOp | ir.UpdateOp)!;
// We can inline variables that:
// - are used exactly once, and
// - are not used remotely
Expand Down

0 comments on commit 5e32a77

Please sign in to comment.