MCPcopy Create free account
hub / github.com/angular/angular / validateSwitchBlock

Function validateSwitchBlock

packages/compiler/src/render3/r3_control_flow.ts:599–654  ·  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

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

Callers 1

createSwitchBlockFunction · 0.85

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected