Skip to content

fix: prevent templ fmt from adding whitespace to blank lines in inline functions#1287

Merged
a-h merged 1 commit intoa-h:mainfrom
Jecoms:fix/fmt-blank-line-whitespace
Oct 19, 2025
Merged

fix: prevent templ fmt from adding whitespace to blank lines in inline functions#1287
a-h merged 1 commit intoa-h:mainfrom
Jecoms:fix/fmt-blank-line-whitespace

Conversation

@Jecoms
Copy link
Copy Markdown
Contributor

@Jecoms Jecoms commented Oct 19, 2025

Describe the bug
templ fmt adds whitespace to blank lines 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

Root Cause
The TemplElementExpression.Write method in parser/v2/types.go was formatting Go expressions by comparing the original formatted code with a re-indented version. When lines were identical in both versions (which blank lines always are), it would write them with indentation. This caused blank lines to incorrectly receive tab characters.

The Fix
Added a check before writing lines: if a line is blank (contains only whitespace when trimmed), skip indentation and continue to the next line. This ensures blank lines remain truly blank with no trailing whitespace.

Changes

  • Modified TemplElementExpression.Write in parser/v2/types.go to check for blank lines
  • Added comprehensive test case inline_func_blank_lines_no_whitespace.txt

Testing

  • All existing tests pass
  • New test validates the fix for inline functions in three contexts:
    • Regular variable assignment
    • Struct field assignment
    • As templ component arguments
  • Manual testing confirms the formatter is now idempotent
  • templ fmt can now be safely used in CI without introducing unwanted whitespace

Closes #1165

…e functions

Fixes an issue where 'templ fmt' incorrectly added indentation to blank
lines inside inline functions when used as struct field values or as
arguments to templ components.

The TemplElementExpression.Write method now checks if a line is blank
(contains only whitespace) and skips indentation for such lines, ensuring
they remain truly blank with no trailing whitespace.

This makes 'templ fmt' idempotent and safe to use in CI environments.
@a-h a-h merged commit 7be7dd6 into a-h:main Oct 19, 2025
@a-h
Copy link
Copy Markdown
Owner

a-h commented Oct 19, 2025

Thanks!

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

Successfully merging this pull request may close these issues.

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

2 participants