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