Skip to content

Whitespace being added to blank line by fmt inside nested inline func #1165

@Jecoms

Description

@Jecoms

Describe the bug
templ fmt adds whitespace to newlines when the line is:

  • inside an inline func
  • being used to set a struct value
  • that is being passed as input to another templ component

To Reproduce

type Params struct {
	Foo string
}

templ Child(p Params) {
	<p>{ p.Foo }</p>
}

templ Parent(p Params) {
	{{
		bar := (func() string {
			if p.Foo == "" {
				return "Bar"
			}
// no whitespace added here
			return p.Foo
		})()
		baz := Params{
			Foo: (func() string {
				if p.Foo == "" {
					return "Baz"
				}
// no whitespace added here
				return p.Foo
			})(),
		}
	}}
	<div>
		@Child(Params{
			Foo: (func() string {
				if p.Foo == "" {
					return "Foo"
				}
		// <-- whitespace added to blank line here after `fmt` // remove comments to test
				return p.Foo
			})(),
		})
		<p>{ bar }</p>
		<p>{ baz.Foo }</p>
	</div>
}

Expected behavior
No whitespace is added to the blank line inside the inline func
If any whitespace is present in this blank line, then it should be removed.

Desktop (please complete the following information):

  • OS: MacOS
  • templ CLI version v0.3.871
  • Go version 1.24.2
  • gopls version v0.18.1

Additional context
I'm looking to use templ fmt in CI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions