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

Function createSwitchBlock

packages/compiler/src/render3/r3_control_flow.ts:226–365  ·  view source on GitHub ↗
(
  ast: html.Block,
  visitor: html.Visitor,
  bindingParser: BindingParser,
)

Source from the content-addressed store, hash-verified

224
225/** Creates a switch block from an HTML AST node. */
226export function createSwitchBlock(
227 ast: html.Block,
228 visitor: html.Visitor,
229 bindingParser: BindingParser,
230): {node: t.SwitchBlock | null; errors: ParseError[]} {
231 const errors = validateSwitchBlock(ast);
232 const primaryExpression =
233 ast.parameters.length > 0
234 ? parseBlockParameterToBinding(ast.parameters[0], bindingParser)
235 : bindingParser.parseBinding('', false, ast.sourceSpan, 0);
236 const groups: t.SwitchBlockCaseGroup[] = [];
237 const unknownBlocks: t.UnknownBlock[] = [];
238 let collectedCases: t.SwitchBlockCase[] = [];
239 let firstCaseStart: ParseSourceSpan | null = null;
240 let exhaustiveCheck: t.SwitchExhaustiveCheck | null = null;
241
242 // Here we assume that all the blocks are valid given that we validated them above.
243 for (const node of ast.children) {
244 if (!(node instanceof html.Block)) {
245 continue;
246 }
247
248 if (
249 (node.name !== 'case' || node.parameters.length === 0) &&
250 node.name !== 'default' &&
251 node.name !== 'default never'
252 ) {
253 unknownBlocks.push(new t.UnknownBlock(node.name, node.sourceSpan, node.nameSpan));
254 continue;
255 }
256
257 if (exhaustiveCheck !== null) {
258 errors.push(
259 new ParseError(
260 node.sourceSpan,
261 '@default block with "never" parameter must be the last case in a switch',
262 ),
263 );
264 }
265
266 const isCase = node.name === 'case';
267 let expression: AST | null = null;
268
269 if (isCase) {
270 expression = parseBlockParameterToBinding(node.parameters[0], bindingParser);
271 } else if (node.name === 'default never') {
272 if (node.parameters.length > 0) {
273 expression = parseBlockParameterToBinding(node.parameters[0], bindingParser);
274 }
275
276 if (
277 node.children.length > 0 ||
278 (node.endSourceSpan !== null &&
279 node.endSourceSpan.start.offset !== node.endSourceSpan.end.offset)
280 ) {
281 errors.push(
282 new ParseError(
283 node.sourceSpan,

Callers 1

visitBlockMethod · 0.90

Calls 5

validateSwitchBlockFunction · 0.85
parseBindingMethod · 0.45
pushMethod · 0.45
visitAllMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…