Skip to content

Formatter doesn't trim trailing whitespace in heredoc #15836

Open
@straight-shoota

Description

@straight-shoota

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind:bugA bug in the code. Does not apply to documentation, specs, etc.topic:tools:formatter

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions