refactor(ivy): extract repetitious code for adding update instructions (#30503)

PR Close #30503
This commit is contained in:
Ben Lesh
2019-05-15 20:05:12 -07:00
committed by Jason Aden
parent 7555a46e23
commit 1537aec1f9

View File

@ -754,37 +754,24 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
if (inputType === BindingType.Property) { if (inputType === BindingType.Property) {
if (value instanceof Interpolation) { if (value instanceof Interpolation) {
// prop="{{value}}" and friends // prop="{{value}}" and friends
this.updateInstruction( this.interpolatedUpdateInstruction(
elementIndex, input.sourceSpan, getPropertyInterpolationExpression(value), getPropertyInterpolationExpression(value), elementIndex, attrName, input, value,
() => params);
[o.literal(attrName),
...this.getUpdateInstructionArguments(o.variable(CONTEXT_NAME), value),
...params]);
} else { } else {
// [prop]="value" // [prop]="value"
this.updateInstruction(elementIndex, input.sourceSpan, R3.property, () => { this.boundUpdateInstruction(
return [ R3.property, elementIndex, attrName, input, implicit, value, params);
o.literal(attrName), this.convertPropertyBinding(implicit, value, true), ...params
];
});
} }
} else if (inputType === BindingType.Attribute) { } else if (inputType === BindingType.Attribute) {
if (value instanceof Interpolation) { if (value instanceof Interpolation) {
// attr.name="{{value}}" and friends // attr.name="{{value}}" and friends
this.updateInstruction( this.interpolatedUpdateInstruction(
elementIndex, input.sourceSpan, getAttributeInterpolationExpression(value), getAttributeInterpolationExpression(value), elementIndex, attrName, input, value,
() => params);
[o.literal(attrName),
...this.getUpdateInstructionArguments(o.variable(CONTEXT_NAME), value),
...params]);
} else { } else {
// [attr.name]="value" // [attr.name]="value"
this.updateInstruction(elementIndex, input.sourceSpan, R3.attribute, () => { this.boundUpdateInstruction(
return [ R3.attribute, elementIndex, attrName, input, implicit, value, params);
o.literal(attrName), this.convertPropertyBinding(implicit, value, true), ...params
];
});
} }
} else { } else {
// class prop // class prop
@ -819,6 +806,32 @@ export class TemplateDefinitionBuilder implements t.Visitor<void>, LocalResolver
} }
} }
/**
* Adds an update instruction for a bound property or attribute, such as `[prop]="value"` or
* `[attr.title]="value"`
*/
boundUpdateInstruction(
instruction: o.ExternalReference, elementIndex: number, attrName: string,
input: t.BoundAttribute, implicit: o.ReadVarExpr, value: any, params: any[]) {
this.updateInstruction(elementIndex, input.sourceSpan, instruction, () => {
return [o.literal(attrName), this.convertPropertyBinding(implicit, value, true), ...params];
});
}
/**
* Adds an update instruction for an interpolated property or attribute, such as
* `prop="{{value}}"` or `attr.title="{{value}}"`
*/
interpolatedUpdateInstruction(
instruction: o.ExternalReference, elementIndex: number, attrName: string,
input: t.BoundAttribute, value: any, params: any[]) {
this.updateInstruction(
elementIndex, input.sourceSpan, instruction,
() =>
[o.literal(attrName),
...this.getUpdateInstructionArguments(o.variable(CONTEXT_NAME), value), ...params]);
}
visitTemplate(template: t.Template) { visitTemplate(template: t.Template) {
const NG_TEMPLATE_TAG_NAME = 'ng-template'; const NG_TEMPLATE_TAG_NAME = 'ng-template';
const templateIndex = this.allocateDataSlot(); const templateIndex = this.allocateDataSlot();