crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Formatter doesn't trim trailing whitespace in heredoc

Open straight-shoota opened this issue 5 months ago • 3 comments
trafficstars

The formatter usually trims all whitespace from a line that only contains whitespace.

But it does not do that in heredocs. For example, the formatter would remove the two white spaces between 1 and 2, but not between 3 and 4.

1
  
2

<<-TXT
  3
  
  4
  TXT

Removing the spaces in a heredoc is entirely fine, as long as its fewer characters than the heredoc indent of the end delimiter. Many editors would automatically trim this anyway on save.

https://github.com/crystal-lang/crystal/blob/c16d805b3f06c723d3e521d73c326207f34d2b31/spec/std/spec/list_tags_spec.cr

Between 5 and 6 there are 4 spaces, but the indent is only 4 characters. So maybe should not be changed because the whitespace is part of the heredoc content.

<<-TXT
  5
    
  6
  TXT

Editors' autotrim-on-save feature is usually not aware of special heredoc semantics. So in practice, this exception might be hard to use because developer tooling isn't inclined to maintain a file with an explicit whitespace-only line in a heredoc. So it's probably fine to ignore this special case and always trim all whitespace even in heredocs.

straight-shoota avatar May 27 '25 14:05 straight-shoota