MCPcopy Index your code
hub / github.com/angular/angular / validateSwitchBlock

Function validateSwitchBlock

packages/compiler/src/render3/r3_control_flow.ts:607–662  ·  view source on GitHub ↗

Checks that the shape of a `switch` block is valid. Returns an array of errors.

(ast: html.Block)

Source from the content-addressed store, hash-verified

605
606/** Checks that the shape of a `switch` block is valid. Returns an array of errors. */
607function validateSwitchBlock(ast: html.Block): ParseError[] {
608 const errors: ParseError[] = [];
609 let hasDefault = false;
610
611 if (ast.parameters.length !== 1) {
612 errors.push(
613 new ParseError(ast.startSourceSpan, '@switch block must have exactly one parameter'),
614 );
615 return errors;
616 }
617
618 for (const node of ast.children) {
619 // Skip over comments and empty text nodes inside the switch block.
620 // Empty text nodes can be used for formatting while comments don't affect the runtime.
621 if (
622 node instanceof html.Comment ||
623 (node instanceof html.Text && node.value.trim().length === 0)
624 ) {
625 continue;
626 }
627
628 if (
629 !(node instanceof html.Block) ||
630 (node.name !== 'case' && node.name !== 'default' && node.name !== 'default never')
631 ) {
632 errors.push(
633 new ParseError(node.sourceSpan, '@switch block can only contain @case and @default blocks'),
634 );
635 continue;
636 }
637
638 if (node.name === 'default never') {
639 if (hasDefault) {
640 errors.push(
641 new ParseError(node.startSourceSpan, '@switch block can only have one @default block'),
642 );
643 }
644 hasDefault = true;
645 } else if (node.name === 'default') {
646 if (hasDefault) {
647 errors.push(
648 new ParseError(node.startSourceSpan, '@switch block can only have one @default block'),
649 );
650 } else if (node.parameters.length > 0) {
651 errors.push(new ParseError(node.startSourceSpan, '@default block cannot have parameters'));
652 }
653 hasDefault = true;
654 } else if (node.name === 'case' && node.parameters.length !== 1) {
655 errors.push(
656 new ParseError(node.startSourceSpan, '@case block must have exactly one parameter'),
657 );
658 }
659 }
660
661 return errors;
662}
663
664/**

Callers 1

createSwitchBlockFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…