Skip to content

Commit

Permalink
fix(compiler): not catching for loop empty tracking expressions (#54772)
Browse files Browse the repository at this point in the history
Fixes that the template parser wasn't catching empty expressions in the `track` parameter of for loops.

Fixes #54763.

PR Close #54772
  • Loading branch information
crisbeto authored and atscott committed Mar 11, 2024
1 parent 962934b commit 1f129f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/compiler/src/render3/r3_control_flow.ts
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ASTWithSource} from '../expression_parser/ast';
import {ASTWithSource, EmptyExpr} from '../expression_parser/ast';
import * as html from '../ml_parser/ast';
import {ParseError, ParseSourceSpan} from '../parse_util';
import {BindingParser} from '../template_parser/binding_parser';
Expand Down Expand Up @@ -256,6 +256,9 @@ function parseForLoopParameters(
new ParseError(param.sourceSpan, '@for loop can only have one "track" expression'));
} else {
const expression = parseBlockParameterToBinding(param, bindingParser, trackMatch[1]);
if (expression.ast instanceof EmptyExpr) {
errors.push(new ParseError(param.sourceSpan, '@for loop must have a "track" expression'));
}
const keywordSpan = new ParseSourceSpan(
param.sourceSpan.start, param.sourceSpan.start.moveBy('track'.length));
result.trackBy = {expression, keywordSpan};
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler/test/render3/r3_template_transform_spec.ts
Expand Up @@ -1520,6 +1520,11 @@ describe('R3 template transform', () => {
@case {case}
}
`)).toThrowError(/@case block must have exactly one parameter/);
expect(() => parse(`
@switch (cond) {
@case ( ) {case}
}
`)).toThrowError(/@case block must have exactly one parameter/);
});

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

it('should report mismatching optional parentheses around for loop expression', () => {
Expand Down Expand Up @@ -1932,6 +1939,9 @@ describe('R3 template transform', () => {
expect(() => parse(`
@if {hello}
`)).toThrowError(/Conditional block does not have an expression/);
expect(() => parse(`
@if ( ) {hello}
`)).toThrowError(/Conditional block does not have an expression/);
});

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

0 comments on commit 1f129f1

Please sign in to comment.