Skip to content

Commit 1f129f1

Browse files
crisbetoatscott
authored andcommitted
fix(compiler): not catching for loop empty tracking expressions (#54772)
Fixes that the template parser wasn't catching empty expressions in the `track` parameter of for loops. Fixes #54763. PR Close #54772
1 parent 962934b commit 1f129f1

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

packages/compiler/src/render3/r3_control_flow.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ASTWithSource} from '../expression_parser/ast';
9+
import {ASTWithSource, EmptyExpr} from '../expression_parser/ast';
1010
import * as html from '../ml_parser/ast';
1111
import {ParseError, ParseSourceSpan} from '../parse_util';
1212
import {BindingParser} from '../template_parser/binding_parser';
@@ -256,6 +256,9 @@ function parseForLoopParameters(
256256
new ParseError(param.sourceSpan, '@for loop can only have one "track" expression'));
257257
} else {
258258
const expression = parseBlockParameterToBinding(param, bindingParser, trackMatch[1]);
259+
if (expression.ast instanceof EmptyExpr) {
260+
errors.push(new ParseError(param.sourceSpan, '@for loop must have a "track" expression'));
261+
}
259262
const keywordSpan = new ParseSourceSpan(
260263
param.sourceSpan.start, param.sourceSpan.start.moveBy('track'.length));
261264
result.trackBy = {expression, keywordSpan};

packages/compiler/test/render3/r3_template_transform_spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,11 @@ describe('R3 template transform', () => {
15201520
@case {case}
15211521
}
15221522
`)).toThrowError(/@case block must have exactly one parameter/);
1523+
expect(() => parse(`
1524+
@switch (cond) {
1525+
@case ( ) {case}
1526+
}
1527+
`)).toThrowError(/@case block must have exactly one parameter/);
15231528
});
15241529

15251530
it('should report if a case has more than one parameter', () => {
@@ -1711,6 +1716,8 @@ describe('R3 template transform', () => {
17111716
it('should report if for loop does not have a tracking expression', () => {
17121717
expect(() => parse(`@for (a of b) {hello}`))
17131718
.toThrowError(/@for loop must have a "track" expression/);
1719+
expect(() => parse(`@for (a of b; track ) {hello}`))
1720+
.toThrowError(/@for loop must have a "track" expression/);
17141721
});
17151722

17161723
it('should report mismatching optional parentheses around for loop expression', () => {
@@ -1932,6 +1939,9 @@ describe('R3 template transform', () => {
19321939
expect(() => parse(`
19331940
@if {hello}
19341941
`)).toThrowError(/Conditional block does not have an expression/);
1942+
expect(() => parse(`
1943+
@if ( ) {hello}
1944+
`)).toThrowError(/Conditional block does not have an expression/);
19351945
});
19361946

19371947
it('should report an unknown parameter in an if block', () => {

0 commit comments

Comments
 (0)