Skip to content

Commit bc655bf

Browse files
crisbetoAndrewKushnir
authored andcommitted
refactor(compiler): integrate let declarations into the template pipeline (#56299)
These changes integrate let declarations into the template pipeline. This involves a few operations: * Producing a `declareLet` instruction call at creation time to initialize the declaration. * Producing a `storeLet` instruction call in the place of the let declaration, including the necessary `advance` calls beforehand. * For let declarations used within their declaration view, moving the `const` to be placed right after the `storeLet` call to ensure the their value has been computed. * For let declarations that are _only_ used in their declaration view, removing the `storeLet` call and inlining the expression into the constant statement. PR Close #56299
1 parent cc6cd08 commit bc655bf

62 files changed

Lines changed: 2283 additions & 23 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/compiler-cli/linker/src/file_linker/partial_linkers/partial_component_linker_1.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export class PartialComponentLinkerVersion1<TStatement, TExpression>
104104
// Enable the new block syntax if compiled with v17 and
105105
// above, or when using the local placeholder version.
106106
const enableBlockSyntax = semver.major(version) >= 17 || version === PLACEHOLDER_VERSION;
107+
const enableLetSyntax = version === PLACEHOLDER_VERSION;
107108

108109
const template = parseTemplate(templateInfo.code, templateInfo.sourceUrl, {
109110
escapedString: templateInfo.isEscaped,
@@ -116,6 +117,7 @@ export class PartialComponentLinkerVersion1<TStatement, TExpression>
116117
// We normalize line endings if the template is was inline.
117118
i18nNormalizeLineEndingsInICUs: isInline,
118119
enableBlockSyntax,
120+
enableLetSyntax,
119121
});
120122
if (template.errors !== null) {
121123
const errors = template.errors.map((err) => err.toString()).join('\n');

‎packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_let/GOLDEN_PARTIAL.js‎

Lines changed: 717 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 326 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,326 @@
1+
{
2+
"$schema": "../test_case_schema.json",
3+
"cases": [
4+
{
5+
"description": "should create a simple @let declaration",
6+
"angularCompilerOptions": {
7+
"_enableLetSyntax": true
8+
},
9+
"inputFiles": [
10+
"simple_let.ts"
11+
],
12+
"expectations": [
13+
{
14+
"files": [
15+
{
16+
"expected": "simple_let_template.js",
17+
"generated": "simple_let.js"
18+
}
19+
],
20+
"failureMessage": "Incorrect template"
21+
}
22+
]
23+
},
24+
{
25+
"description": "should create multiple @let declarations that depend on each other",
26+
"angularCompilerOptions": {
27+
"_enableLetSyntax": true
28+
},
29+
"inputFiles": [
30+
"multiple_let.ts"
31+
],
32+
"expectations": [
33+
{
34+
"files": [
35+
{
36+
"expected": "multiple_let_template.js",
37+
"generated": "multiple_let.js"
38+
}
39+
],
40+
"failureMessage": "Incorrect template"
41+
}
42+
]
43+
},
44+
{
45+
"description": "should create a let using a pipe",
46+
"angularCompilerOptions": {
47+
"_enableLetSyntax": true
48+
},
49+
"inputFiles": [
50+
"let_with_pipe.ts"
51+
],
52+
"expectations": [
53+
{
54+
"files": [
55+
{
56+
"expected": "let_with_pipe_template.js",
57+
"generated": "let_with_pipe.js"
58+
}
59+
],
60+
"failureMessage": "Incorrect template"
61+
}
62+
]
63+
},
64+
{
65+
"description": "should be able to use let declarations in event listeners",
66+
"angularCompilerOptions": {
67+
"_enableLetSyntax": true
68+
},
69+
"inputFiles": [
70+
"let_in_listener.ts"
71+
],
72+
"expectations": [
73+
{
74+
"files": [
75+
{
76+
"expected": "let_in_listener_template.js",
77+
"generated": "let_in_listener.js"
78+
}
79+
],
80+
"failureMessage": "Incorrect template"
81+
}
82+
]
83+
},
84+
{
85+
"description": "should be able to use let declarations in child views",
86+
"angularCompilerOptions": {
87+
"_enableLetSyntax": true
88+
},
89+
"inputFiles": [
90+
"let_in_child_view.ts"
91+
],
92+
"expectations": [
93+
{
94+
"files": [
95+
{
96+
"expected": "let_in_child_view_template.js",
97+
"generated": "let_in_child_view.js"
98+
}
99+
],
100+
"failureMessage": "Incorrect template"
101+
}
102+
]
103+
},
104+
{
105+
"description": "should share let declarations between parent and child views",
106+
"angularCompilerOptions": {
107+
"_enableLetSyntax": true
108+
},
109+
"inputFiles": [
110+
"let_shared_with_child_view.ts"
111+
],
112+
"expectations": [
113+
{
114+
"files": [
115+
{
116+
"expected": "let_shared_with_child_view_template.js",
117+
"generated": "let_shared_with_child_view.js"
118+
}
119+
],
120+
"failureMessage": "Incorrect template"
121+
}
122+
]
123+
},
124+
{
125+
"description": "should be able to use let declarations in event listeners inside child views",
126+
"angularCompilerOptions": {
127+
"_enableLetSyntax": true
128+
},
129+
"inputFiles": [
130+
"let_in_child_view_listener.ts"
131+
],
132+
"expectations": [
133+
{
134+
"files": [
135+
{
136+
"expected": "let_in_child_view_listener_template.js",
137+
"generated": "let_in_child_view_listener.js"
138+
}
139+
],
140+
"failureMessage": "Incorrect template"
141+
}
142+
]
143+
},
144+
{
145+
"description": "should be able to use local references in let declarations",
146+
"angularCompilerOptions": {
147+
"_enableLetSyntax": true
148+
},
149+
"inputFiles": [
150+
"let_local_refs.ts"
151+
],
152+
"expectations": [
153+
{
154+
"files": [
155+
{
156+
"expected": "let_local_refs_template.js",
157+
"generated": "let_local_refs.js"
158+
}
159+
],
160+
"failureMessage": "Incorrect template"
161+
}
162+
]
163+
},
164+
{
165+
"description": "should be able to use forward references defined after the let declaration",
166+
"angularCompilerOptions": {
167+
"_enableLetSyntax": true
168+
},
169+
"inputFiles": [
170+
"let_local_forward_refs.ts"
171+
],
172+
"expectations": [
173+
{
174+
"files": [
175+
{
176+
"expected": "let_local_forward_refs_template.js",
177+
"generated": "let_local_forward_refs.js"
178+
}
179+
],
180+
"failureMessage": "Incorrect template"
181+
}
182+
]
183+
},
184+
{
185+
"description": "should be able to use for loop variables in let declarations",
186+
"angularCompilerOptions": {
187+
"_enableLetSyntax": true
188+
},
189+
"inputFiles": [
190+
"let_for_loop.ts"
191+
],
192+
"expectations": [
193+
{
194+
"files": [
195+
{
196+
"expected": "let_for_loop_template.js",
197+
"generated": "let_for_loop.js"
198+
}
199+
],
200+
"failureMessage": "Incorrect template"
201+
}
202+
]
203+
},
204+
{
205+
"description": "should transform an invalid let reference to undefined",
206+
"angularCompilerOptions": {
207+
"_enableLetSyntax": true,
208+
"checkTemplateBodies": false
209+
},
210+
"inputFiles": [
211+
"let_invalid_forward_ref.ts"
212+
],
213+
"expectations": [
214+
{
215+
"files": [
216+
{
217+
"expected": "let_invalid_forward_ref_template.js",
218+
"generated": "let_invalid_forward_ref.js"
219+
}
220+
],
221+
"failureMessage": "Incorrect template"
222+
}
223+
]
224+
},
225+
{
226+
"description": "should remove a single unused let declaration",
227+
"angularCompilerOptions": {
228+
"_enableLetSyntax": true
229+
},
230+
"inputFiles": [
231+
"let_single_optimization.ts"
232+
],
233+
"expectations": [
234+
{
235+
"files": [
236+
{
237+
"expected": "let_single_optimization_template.js",
238+
"generated": "let_single_optimization.js"
239+
}
240+
],
241+
"failureMessage": "Incorrect template"
242+
}
243+
]
244+
},
245+
{
246+
"description": "should remove a chain of unused let declarations",
247+
"angularCompilerOptions": {
248+
"_enableLetSyntax": true
249+
},
250+
"inputFiles": [
251+
"let_multiple_optimization.ts"
252+
],
253+
"expectations": [
254+
{
255+
"files": [
256+
{
257+
"expected": "let_multiple_optimization_template.js",
258+
"generated": "let_multiple_optimization.js"
259+
}
260+
],
261+
"failureMessage": "Incorrect template"
262+
}
263+
]
264+
},
265+
{
266+
"description": "should remove only the unused let declarations from the middle of a chain of declarations",
267+
"angularCompilerOptions": {
268+
"_enableLetSyntax": true
269+
},
270+
"inputFiles": [
271+
"let_partial_optimization.ts"
272+
],
273+
"expectations": [
274+
{
275+
"files": [
276+
{
277+
"expected": "let_partial_optimization_template.js",
278+
"generated": "let_partial_optimization.js"
279+
}
280+
],
281+
"failureMessage": "Incorrect template"
282+
}
283+
]
284+
},
285+
{
286+
"description": "should not remove let declarations that are only used in an event listener",
287+
"angularCompilerOptions": {
288+
"_enableLetSyntax": true
289+
},
290+
"inputFiles": [
291+
"let_optimization_listener.ts"
292+
],
293+
"expectations": [
294+
{
295+
"files": [
296+
{
297+
"expected": "let_optimization_listener_template.js",
298+
"generated": "let_optimization_listener.js"
299+
}
300+
],
301+
"failureMessage": "Incorrect template"
302+
}
303+
]
304+
},
305+
{
306+
"description": "should not remove let declarations that are only used in a child view",
307+
"angularCompilerOptions": {
308+
"_enableLetSyntax": true
309+
},
310+
"inputFiles": [
311+
"let_optimization_child_view.ts"
312+
],
313+
"expectations": [
314+
{
315+
"files": [
316+
{
317+
"expected": "let_optimization_child_view_template.js",
318+
"generated": "let_optimization_child_view.js"
319+
}
320+
],
321+
"failureMessage": "Incorrect template"
322+
}
323+
]
324+
}
325+
]
326+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {Component} from '@angular/core';
2+
3+
@Component({
4+
template: `
5+
@for (item of items; track item) {
6+
@let outerFirst = $first;
7+
8+
@for (subitem of item.children; track subitem) {
9+
@let innerFirst = $first;
10+
11+
{{outerFirst || innerFirst}}
12+
}
13+
}
14+
`,
15+
standalone: true,
16+
})
17+
export class MyApp {
18+
items: {children: any[]}[] = [];
19+
}

0 commit comments

Comments
 (0)