a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
8.22k stars 268 forks source link

Switch fallthrough not supported #920

Open jonathangjertsen opened 1 month ago

jonathangjertsen commented 1 month ago

Describe the bug

Fallthrough statements in switch statements do not exist in the generated go file, instead it just outputs the string "fallthrough".

To Reproduce

package main

templ example(x int) {
    switch x {
        case 0:
            fallthrough
        case 1:
            <p>hey</p>
    }
}

Expected behavior

@example(0) should generate the output <p>hey</p>

templ info output

(✓) os [ goos=windows goarch=amd64 ]
(✓) go [ location=C:\Program Files\Go\bin\go.exe version=go version go1.22.0 windows/amd64 ]
(✓) gopls [ location=C:\Users\jrg\go\bin\gopls.exe version=golang.org/x/tools/gopls v0.16.2 ]
(✓) templ [ location=C:\Users\jrg\go\bin\templ.exe version=v0.2.771 ]
bcklin commented 3 weeks ago

Golang does not perform fall-through in switch statements. See https://go.dev/tour/flowcontrol/9

jonathangjertsen commented 3 weeks ago

Golang does not perform fall-through in switch statements. See https://go.dev/tour/flowcontrol/9

Unless you use the fallthrough keyword. 🙂

a-h commented 2 weeks ago

Thanks for the report.

This should be fairly straightforward to sort out. Need to add a parser and node type for it, and a validator to make sure it only appears as a child of the case statement, and that there there are no nodes between the fallthrough and the next case node.