refactor(compiler): Don't double-create pipes in switch cases (#52289)

Previously, we would emit *two* pipe creation instructions for each pipe in a switch case. This is because we were visiting both the transformed and raw versions of the pipe bindings.

Now, we clear the raw case expressions array after generating the transformed test expression.

Also, we introduce some new goldens, because our pipe creation order is harmlessly different.

PR Close #52289
This commit is contained in:
Dylan Hunn 2023-10-23 18:55:11 -07:00
parent 17be1a8aca
commit d82d58621e
4 changed files with 66 additions and 6 deletions

View file

@ -62,13 +62,13 @@
"files": [
{
"expected": "switch_with_pipe_template.js",
"generated": "switch_with_pipe.js"
"generated": "switch_with_pipe.js",
"templatePipelineExpected": "switch_with_pipe_template.pipeline.js"
}
],
"failureMessage": "Incorrect template"
}
],
"skipForTemplatePipeline": true
]
},
{
"description": "should generate a basic if block",
@ -148,13 +148,13 @@
"files": [
{
"expected": "if_with_pipe_template.js",
"generated": "if_with_pipe.js"
"generated": "if_with_pipe.js",
"templatePipelineExpected": "if_with_pipe_template.pipeline.js"
}
],
"failureMessage": "Incorrect template"
}
],
"skipForTemplatePipeline": true
]
},
{
"description": "should generate an if block with an aliased expression",

View file

@ -0,0 +1,36 @@
function $MyApp_Conditional_3_Template$(rf, ctx) {
if (rf & 1) {
$r3$.ɵɵtext(0, " one ");
}
}
function $MyApp_Conditional_5_Template$(rf, ctx) {
if (rf & 1) {
$r3$.ɵɵtext(0, " two ");
}
}
function $MyApp_Conditional_6_Template$(rf, ctx) {
if (rf & 1) {
$r3$.ɵɵtext(0, " three ");
}
}
function MyApp_Template(rf, ctx) {
if (rf & 1) {
$r3$.ɵɵelementStart(0, "div");
$r3$.ɵɵtext(1);
$r3$.ɵɵtemplate(2, MyApp_Conditional_2_Template, 1, 0);
$r3$.ɵɵpipe(3, "test");
$r3$.ɵɵpipe(4, "test");
$r3$.ɵɵtemplate(5, MyApp_Conditional_5_Template, 1, 0)(6, MyApp_Conditional_6_Template, 1, 0);
$r3$.ɵɵelementEnd();
}
if (rf & 2) {
$r3$.ɵɵadvance(1);
$r3$.ɵɵtextInterpolate1(" ", ctx.message, " ");
$r3$.ɵɵadvance(1);
$r3$.ɵɵconditional(2, $r3$.ɵɵpipeBind1(3, 2, ctx.val) === 1 ? 2 : $r3$.ɵɵpipeBind1(4, 4, ctx.val) === 2 ? 5 : 6);
}
}

View file

@ -0,0 +1,20 @@
function MyApp_Template(rf, ctx) {
if (rf & 1) {
$r3$.ɵɵelementStart(0, "div");
$r3$.ɵɵtext(1);
$r3$.ɵɵtemplate(2, MyApp_Case_2_Template, 1, 0);
$r3$.ɵɵpipe(3, "test");
$r3$.ɵɵpipe(4, "test");
$r3$.ɵɵpipe(5, "test");
$r3$.ɵɵtemplate(6, MyApp_Case_6_Template, 1, 0)(7, MyApp_Case_7_Template, 1, 0);
$r3$.ɵɵelementEnd();
}
if (rf & 2) {
let $MyApp_contFlowTmp$;
$r3$.ɵɵadvance(1);
$r3$.ɵɵtextInterpolate1(" ", ctx.message, " ");
$r3$.ɵɵadvance(1);
$r3$.ɵɵconditional(2, ($MyApp_contFlowTmp$ = $r3$.ɵɵpipeBind1(3, 2, ctx.value())) === $r3$.ɵɵpipeBind1(4, 4, 0) ? 2 : $MyApp_contFlowTmp$ === $r3$.ɵɵpipeBind1(5, 6, 1) ? 6 : 7);
}
}

View file

@ -58,6 +58,10 @@ export function phaseConditionals(job: ComponentCompilationJob): void {
// Save the resulting aggregate Joost-expression.
op.processed = test;
// Clear the original conditions array, since we no longer need it, and don't want it to
// affect subsequent phases (e.g. pipe creation).
op.conditions = [];
}
}
}