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

Function createSwitchBlock

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

Source from the content-addressed store, hash-verified

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