a-h / templ

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

bug: ambiguous child/string expression grammar #892

Open MilkeeyCat opened 3 weeks ago

MilkeeyCat commented 3 weeks ago

Describe the bug Templ generates wrong code for an expression which has a component in front of it.

To Reproduce

templ Test() {
    test
}

templ TestThingy(value string) {
    <div>@Test() { value }</div>
}

Expected behavior The code snippet from above generates <div>test</div> and doesn't print value from { value } expression. It does work though if you either put any character between component call & expression or put expression on a new line.

Also when I run formatter it formats code like so:

templ TestThingy(value string) {
    <div>
        @Test() {
            value 
        }
    </div>
}

(like a function definition? xd)

templ info output

usage: templ <command> [<args>...]

templ - build HTML UIs with Go

See docs at https://templ.guide

commands:
  generate   Generates Go code from templ files
  fmt        Formats templ files
  lsp        Starts a language server for templ files
  version    Prints the version

Desktop:

a-h commented 2 weeks ago

@joerdav - looks like we have ambiguous grammar here.

@Test() { // Children of "@Test()" }
@Test() { "string to output" }

Ideas?

joerdav commented 2 weeks ago

@a-h one potentially breaking option would be to disallow block elements on a single line? Make a new line after { and before } a requirement. Otherwise it will be detected as interpolation?

a-h commented 2 weeks ago

Sounds like a sensible plan of action to me.

joerdav commented 2 weeks ago

I wonder if there is something we can do to communicate this to affected users? It could be surprising for the contents of the curly braces to be printed as children rather than interpolated.

I've also noted in this example that we don't stop a user from calling a component with children, even if the children are never rendered. One to think about!