(i: number, ctx: Context)
| 380 | |
| 381 | function convertStmts(nodes: p.TSESTree.Statement[], requireReturn: boolean, ctx: Context): Term { |
| 382 | function convertStmt(i: number, ctx: Context): Term { |
| 383 | const last = nodes.length - 1 === i; |
| 384 | const node = nodes[i]; |
| 385 | switch (node.type) { |
| 386 | case "VariableDeclaration": { |
| 387 | if (last && requireReturn) error("return is required", node); |
| 388 | if (node.declarations.length !== 1) error("multiple variable declaration is not allowed", node); |
| 389 | const decl = node.declarations[0]; |
| 390 | if (!decl.init) error("variable initializer is required", decl); |
| 391 | const name = getIdentifier(decl.id); |
| 392 | const init = convertExpr(decl.init, ctx); |
| 393 | const rest: Term = last ? { tag: "var", name, loc: node.loc } : convertStmt(i + 1, ctx); |
| 394 | return { tag: "const", name, init, rest, loc: node.loc }; |
| 395 | } |
| 396 | case "ExportNamedDeclaration": { |
| 397 | if (last && requireReturn) error("return is required", node); |
| 398 | if (!node.declaration || node.declaration.type !== "FunctionDeclaration") { |
| 399 | error("export must have a function declaration", node); |
| 400 | } |
| 401 | return getRecFunc(node.declaration, ctx, last ? null : () => convertStmt(i + 1, ctx)); |
| 402 | } |
| 403 | case "FunctionDeclaration": { |
| 404 | if (last && requireReturn) error("return is required", node); |
| 405 | return getRecFunc(node, ctx, last ? null : () => convertStmt(i + 1, ctx)); |
| 406 | } |
| 407 | case "ExpressionStatement": { |
| 408 | if (last && requireReturn) error("return is required", node); |
| 409 | const body = convertExpr(node.expression, ctx); |
| 410 | return last ? body : { tag: "seq", body, rest: convertStmt(i + 1, ctx), loc: node.loc }; |
| 411 | } |
| 412 | case "ReturnStatement": { |
| 413 | if (!node.argument) error("return must have an argument", node); |
| 414 | return convertExpr(node.argument, ctx); |
| 415 | } |
| 416 | case "IfStatement": { |
| 417 | const cond = convertExpr(node.test, ctx); |
| 418 | const thn = convertStmts([node.consequent], requireReturn, ctx); |
| 419 | if (!node.alternate) error("else clause is requried", node); |
| 420 | const els = convertStmts([node.alternate], requireReturn, ctx); |
| 421 | return { tag: "if", cond, thn, els, loc: node.loc }; |
| 422 | } |
| 423 | case "SwitchStatement": { |
| 424 | const varName = getSwitchVarName(node.discriminant); |
| 425 | const clauses: VariantTerm[] = []; |
| 426 | node.cases.forEach((caseNode) => { |
| 427 | if (!caseNode.test) error("default case is not allowed", caseNode); |
| 428 | const conseq = caseNode.consequent; |
| 429 | const stmts = conseq.length === 1 && conseq[0].type === "BlockStatement" ? conseq[0].body : conseq; |
| 430 | const clause = convertStmts(stmts, requireReturn, ctx); |
| 431 | clauses.push({ label: getLiteralString(caseNode.test), term: clause }); |
| 432 | }); |
| 433 | return { tag: "taggedUnionGet", varName, clauses, loc: node.loc }; |
| 434 | } |
| 435 | default: |
| 436 | error(`unsupported statement node: ${node.type}`, node); |
| 437 | } |
| 438 | } |
| 439 | return convertStmt(0, ctx); |
no test coverage detected