Skip to content

Commit 2ef777b

Browse files
karaIgorMinar
authored andcommitted
fix(ivy): convert context code into a tree-shakable instruction (angular#24943)
PR Close angular#24943
1 parent fe14f18 commit 2ef777b

24 files changed

+1058
-519
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
Tests in this directory are excluded from running in the browser and only run in node.
1+
Tests in this directory should be run with:
2+
3+
bazel test --define=compile=local packages/compiler-cli/test/compliance:compliance

packages/compiler-cli/test/compliance/r3_compiler_compliance_spec.ts

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,16 @@ describe('compiler compliance', () => {
541541
const MyComponentDefinition = `
542542
const $c1$ = ["foo", ""];
543543
const $c2$ = ["if", ""];
544-
function MyComponent_li_Template_2(rf, ctx0, ctx) {
544+
function MyComponent_li_Template_2(rf, ctx) {
545545
if (rf & 1) {
546546
$r3$.ɵE(0, "li");
547547
$r3$.ɵT(1);
548548
$r3$.ɵe();
549549
}
550550
if (rf & 2) {
551-
const $foo$ = $r3$.ɵr(1, 1);
552-
$r3$.ɵt(1, $r3$.ɵi2("", ctx.salutation, " ", $foo$, ""));
551+
const $myComp$ = $r3$.ɵx();
552+
const $foo$ = $r3$.ɵr(1);
553+
$r3$.ɵt(1, $r3$.ɵi2("", $myComp$.salutation, " ", $foo$, ""));
553554
}
554555
}
555556
@@ -1174,7 +1175,7 @@ describe('compiler compliance', () => {
11741175
$r3$.ɵT(2);
11751176
}
11761177
if (rf & 2) {
1177-
const $user$ = $r3$.ɵld(1);
1178+
const $user$ = $r3$.ɵr(1);
11781179
$r3$.ɵt(2, $r3$.ɵi1("Hello ", $user$.value, "!"));
11791180
}
11801181
}
@@ -1224,20 +1225,22 @@ describe('compiler compliance', () => {
12241225
const $c2$ = ["if", ""];
12251226
const $c3$ = ["baz", ""];
12261227
const $c4$ = ["bar", ""];
1227-
function MyComponent_div_span_Template_2(rf, ctx1, ctx0, ctx) {
1228+
function MyComponent_div_span_Template_2(rf, ctx) {
12281229
if (rf & 1) {
12291230
$r3$.ɵE(0, "span");
12301231
$r3$.ɵT(1);
12311232
$r3$.ɵe();
12321233
}
12331234
if (rf & 2) {
1234-
const $foo$ = $r3$.ɵr(2, 1);
1235-
const $bar$ = $r3$.ɵr(1, 4);
1236-
const $baz$ = $r3$.ɵr(2, 5);
1235+
$r3$.ɵx();
1236+
const $bar$ = $r3$.ɵr(4);
1237+
$r3$.ɵx();
1238+
const $foo$ = $r3$.ɵr(1);
1239+
const $baz$ = $r3$.ɵr(5);
12371240
$r3$.ɵt(1, $r3$.ɵi3("", $foo$, "-", $bar$, "-", $baz$, ""));
12381241
}
12391242
}
1240-
function MyComponent_div_Template_3(rf, ctx0, ctx) {
1243+
function MyComponent_div_Template_3(rf, ctx) {
12411244
if (rf & 1) {
12421245
$r3$.ɵE(0, "div");
12431246
$r3$.ɵT(1);
@@ -1246,8 +1249,9 @@ describe('compiler compliance', () => {
12461249
$r3$.ɵe();
12471250
}
12481251
if (rf & 2) {
1249-
const $foo$ = $r3$.ɵr(1, 1);
1250-
const $bar$ = $r3$.ɵld(4);
1252+
const $bar$ = $r3$.ɵr(4);
1253+
$r3$.ɵx();
1254+
const $foo$ = $r3$.ɵr(1);
12511255
$r3$.ɵt(1, $r3$.ɵi2(" ", $foo$, "-", $bar$, " "));
12521256
}
12531257
}
@@ -1264,7 +1268,7 @@ describe('compiler compliance', () => {
12641268
$r3$.ɵEe(4, "div", null, $c3$);
12651269
}
12661270
if (rf & 2) {
1267-
const $foo$ = $r3$.ɵld(1);
1271+
const $foo$ = $r3$.ɵr(1);
12681272
$r3$.ɵt(2, $r3$.ɵi1(" ", $foo$, " "));
12691273
}
12701274
},
@@ -1278,6 +1282,77 @@ describe('compiler compliance', () => {
12781282

12791283
});
12801284

1285+
it('should support local refs mixed with context assignments', () => {
1286+
const files = {
1287+
app: {
1288+
'spec.ts': `
1289+
import {Component, NgModule} from '@angular/core';
1290+
import {CommonModule} from '@angular/common';
1291+
1292+
@Component({
1293+
selector: 'my-component',
1294+
template: \`
1295+
<div *ngFor="let item of items">
1296+
<div #foo></div>
1297+
<span *ngIf="showing">
1298+
{{ foo }} - {{ item }}
1299+
</span>
1300+
</div>\`
1301+
})
1302+
export class MyComponent {}
1303+
1304+
@NgModule({declarations: [MyComponent], imports: [CommonModule]})
1305+
export class MyModule {}
1306+
`
1307+
}
1308+
};
1309+
1310+
const template = `
1311+
const $c0$ = ["ngFor","","ngForOf",""];
1312+
const $c1$ = ["foo", ""];
1313+
const $c2$ = ["ngIf",""];
1314+
1315+
function MyComponent_div_span_Template_3(rf, ctx) {
1316+
if (rf & 1) {
1317+
$i0$.ɵE(0, "span");
1318+
$i0$.ɵT(1);
1319+
$i0$.ɵe();
1320+
}
1321+
if (rf & 2) {
1322+
const $item$ = $i0$.ɵx().$implicit;
1323+
const $foo$ = $i0$.ɵr(2);
1324+
$i0$.ɵt(1, $i0$.ɵi2(" ", $foo$, " - ", $item$, " "));
1325+
}
1326+
}
1327+
1328+
function MyComponent_div_Template_0(rf, ctx) {
1329+
if (rf & 1) {
1330+
$i0$.ɵE(0, "div");
1331+
$i0$.ɵEe(1, "div", null, $c1$);
1332+
$i0$.ɵC(3, MyComponent_div_span_Template_3, null, $c2$);
1333+
$i0$.ɵe();
1334+
}
1335+
if (rf & 2) {
1336+
const $app$ = $i0$.ɵx();
1337+
$i0$.ɵp(3, "ngIf", $i0$.ɵb($app$.showing));
1338+
}
1339+
}
1340+
1341+
// ...
1342+
template:function MyComponent_Template(rf, ctx){
1343+
if (rf & 1) {
1344+
$i0$.ɵC(0, MyComponent_div_Template_0, null, $c0$);
1345+
}
1346+
if (rf & 2) {
1347+
$i0$.ɵp(0, "ngForOf", $i0$.ɵb(ctx.items));
1348+
}
1349+
}`;
1350+
1351+
const result = compile(files, angularFiles);
1352+
1353+
expectEmit(result.source, template, 'Incorrect template');
1354+
});
1355+
12811356
describe('lifecycle hooks', () => {
12821357
const files = {
12831358
app: {
@@ -1452,7 +1527,7 @@ describe('compiler compliance', () => {
14521527

14531528
const MyComponentDefinition = `
14541529
const $_c0$ = ["for","","forOf",""];
1455-
function MyComponent__svg_g_Template_1(rf, ctx0, ctx) {
1530+
function MyComponent__svg_g_Template_1(rf, ctx) {
14561531
if (rf & 1) {
14571532
$r3$.ɵNS();
14581533
$r3$.ɵE(0,"g");
@@ -1525,14 +1600,14 @@ describe('compiler compliance', () => {
15251600

15261601
const MyComponentDefinition = `
15271602
const $_c0$ = ["for","","forOf",""];
1528-
function MyComponent_li_Template_1(rf, ctx0, ctx) {
1603+
function MyComponent_li_Template_1(rf, ctx) {
15291604
if (rf & 1) {
15301605
$r3$.ɵE(0, "li");
15311606
$r3$.ɵT(1);
15321607
$r3$.ɵe();
15331608
}
15341609
if (rf & 2) {
1535-
const $item$ = ctx0.$implicit;
1610+
const $item$ = ctx.$implicit;
15361611
$r3$.ɵt(1, $r3$.ɵi1("", $item$.name, ""));
15371612
}
15381613
}
@@ -1602,20 +1677,20 @@ describe('compiler compliance', () => {
16021677

16031678
const MyComponentDefinition = `
16041679
const $c1$ = ["for", "", "forOf", ""];
1605-
function MyComponent_li_li_Template_4(rf, ctx1, ctx0, ctx) {
1680+
function MyComponent_li_li_Template_4(rf, ctx) {
16061681
if (rf & 1) {
16071682
$r3$.ɵE(0, "li");
16081683
$r3$.ɵT(1);
16091684
$r3$.ɵe();
16101685
}
16111686
if (rf & 2) {
1612-
const $item$ = ctx0.$implicit;
1613-
const $info$ = ctx1.$implicit;
1687+
const $info$ = ctx.$implicit;
1688+
const $item$ = $r3$.ɵx().$implicit;
16141689
$r3$.ɵt(1, $r3$.ɵi2(" ", $item$.name, ": ", $info$.description, " "));
16151690
}
16161691
}
16171692
1618-
function MyComponent_li_Template_1(rf, ctx0, ctx) {
1693+
function MyComponent_li_Template_1(rf, ctx) {
16191694
if (rf & 1) {
16201695
$r3$.ɵE(0, "li");
16211696
$r3$.ɵE(1, "div");
@@ -1627,7 +1702,7 @@ describe('compiler compliance', () => {
16271702
$r3$.ɵe();
16281703
}
16291704
if (rf & 2) {
1630-
const $item$ = ctx0.$implicit;
1705+
const $item$ = ctx.$implicit;
16311706
$r3$.ɵt(2, $r3$.ɵi1("", IDENT.name, ""));
16321707
$r3$.ɵp(4, "forOf", $r3$.ɵb(IDENT.infos));
16331708
}

0 commit comments

Comments
 (0)