generateHoistedMatch generates code for argument context (fn(match ...)). Produces: var tmp TYPE; switch { case ...: tmp = value }; fn(tmp) Returns temp var name as expression replacement. Falls back to IIFE if type cannot be inferred.
()
| 801 | // Returns temp var name as expression replacement. |
| 802 | // Falls back to IIFE if type cannot be inferred. |
| 803 | func (g *MatchCodeGen) generateHoistedMatch() ast.CodeGenResult { |
| 804 | tmpVar := g.TempVar("result") |
| 805 | varType := g.Context.VarType |
| 806 | |
| 807 | if varType == "" { |
| 808 | // IIFE fallback when type cannot be inferred |
| 809 | g.generateMatchIIFE() |
| 810 | return g.Result() |
| 811 | } |
| 812 | |
| 813 | // Generate hoisted code: var tmp TYPE; switch { case ...: tmp = value } |
| 814 | hoistedBuf := &bytes.Buffer{} |
| 815 | hoistedBuf.WriteString(fmt.Sprintf("var %s %s\n", tmpVar, varType)) |
| 816 | |
| 817 | // Save current buffer and switch to hoistedBuf |
| 818 | origBuf := g.Buf |
| 819 | g.Buf = *hoistedBuf |
| 820 | |
| 821 | // Generate switch with assignments to tmpVar |
| 822 | g.generateMatchSwitchWithAssignment(tmpVar) |
| 823 | |
| 824 | // Get the hoisted code from the buffer |
| 825 | hoistedCode := g.Buf.Bytes() |
| 826 | |
| 827 | // Restore original buffer |
| 828 | g.Buf = origBuf |
| 829 | |
| 830 | // Build result |
| 831 | result := g.Result() |
| 832 | result.HoistedCode = hoistedCode // Hoisted code goes BEFORE the statement |
| 833 | result.Output = []byte(tmpVar) // Replace match expression with tmpVar |
| 834 | |
| 835 | return result |
| 836 | } |
| 837 | |
| 838 | // generateMatchSwitchWithAssignment generates switch statement that assigns to varName. |
| 839 | // Example: switch x.(type) { case A: varName = "a"; case B: varName = "b" } |
no test coverage detected