Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: ambiguous child/string expression grammar #892

Open
MilkeeyCat opened this issue Aug 21, 2024 · 4 comments
Open

bug: ambiguous child/string expression grammar #892

MilkeeyCat opened this issue Aug 21, 2024 · 4 comments

Comments

@MilkeeyCat
Copy link

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:

  • OS: Linux
  • templ CLI version: v0.2.747
  • Go version: go1.22.3 linux/amd64
  • gopls version: idk
@a-h
Copy link
Owner

a-h commented Aug 24, 2024

@joerdav - looks like we have ambiguous grammar here.

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

Ideas?

@a-h a-h added help wanted Extra attention is needed parser NeedsDecision Issue needs some more discussion so a decision can be made labels Aug 24, 2024
@joerdav
Copy link
Collaborator

joerdav commented Aug 24, 2024

@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 a-h changed the title Incorrect expression parsing bug: ambiguous child/string expression grammar Aug 24, 2024
@a-h
Copy link
Owner

a-h commented Aug 24, 2024

Sounds like a sensible plan of action to me.

@a-h a-h added NeedsImplementation Needs implementation and removed NeedsDecision Issue needs some more discussion so a decision can be made labels Aug 24, 2024
@joerdav
Copy link
Collaborator

joerdav commented Aug 24, 2024

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!

@linear linear bot removed parser help wanted Extra attention is needed NeedsImplementation Needs implementation labels Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants