Skip to content

Commit 5f528bf

Browse files
crisbetoalxhub
authored andcommitted
fix(compiler): allow comments between switch cases (angular#52449)
Fixes that the template parser was throwing an error if a comment is used directly inside an `@switch` block. Fixes angular#52421. PR Close angular#52449
1 parent b5ef68f commit 5f528bf

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

packages/compiler/src/render3/r3_control_flow.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,10 @@ function validateSwitchBlock(ast: html.Block): ParseError[] {
338338
}
339339

340340
for (const node of ast.children) {
341-
// Skip over empty text nodes inside the switch block since they can be used for formatting.
342-
if (node instanceof html.Text && node.value.trim().length === 0) {
341+
// Skip over comments and empty text nodes inside the switch block.
342+
// Empty text nodes can be used for formatting while comments don't affect the runtime.
343+
if (node instanceof html.Comment ||
344+
(node instanceof html.Text && node.value.trim().length === 0)) {
343345
continue;
344346
}
345347

packages/compiler/test/render3/r3_template_transform_spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,24 @@ describe('R3 template transform', () => {
13641364
]);
13651365
});
13661366

1367+
it('should parse a switch block containing comments', () => {
1368+
expectFromHtml(`
1369+
@switch (cond.kind) {
1370+
<!-- X case -->
1371+
@case (x) { X case }
1372+
1373+
<!-- default case -->
1374+
@default { No case matched }
1375+
}
1376+
`).toEqual([
1377+
['SwitchBlock', 'cond.kind'],
1378+
['SwitchBlockCase', 'x'],
1379+
['Text', ' X case '],
1380+
['SwitchBlockCase', null],
1381+
['Text', ' No case matched '],
1382+
]);
1383+
});
1384+
13671385
describe('validations', () => {
13681386
it('should report syntax error in switch expression', () => {
13691387
expect(() => parse(`

0 commit comments

Comments
 (0)