Navigation Menu

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

Here doc alignment autocorrection is wrong / cumbersome #9945

Closed
thisismydesign opened this issue Jul 21, 2021 · 3 comments
Closed

Here doc alignment autocorrection is wrong / cumbersome #9945

thisismydesign opened this issue Jul 21, 2021 · 3 comments
Assignees
Labels

Comments

@thisismydesign
Copy link

thisismydesign commented Jul 21, 2021

Actual behavior

FactoryBot.define do
  factory :something do
    text do <<~EOF
Long here doc with several empty lines

bla

bla
EOF
    end
  end
end

rubocop -a output and result:

FactoryBot.define do
  factory :something do
    text do
      <<~EOF
                Long here doc with several empty lines
        #{'        '}
                bla
        #{'        '}
                bla
      EOF
    end
  end
end
Inspecting 1 file
C

Offenses:

spec/factories/code_reviews.rb:5:12: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
    text do 
           ^
spec/factories/code_reviews.rb:5:13: C: [Corrected] Layout/MultilineBlockLayout: Block body expression is on the same line as the block start.
    text do <<~EOF
            ^^^^^^
spec/factories/code_reviews.rb:6:1: C: [Corrected] Layout/HeredocIndentation: Use 2 spaces for indentation in a heredoc.
Long here doc with several empty lines ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/factories/code_reviews.rb:7:1: C: [Corrected] Layout/HeredocIndentation: Use 2 spaces for indentation in a heredoc.
      Long here doc with several empty lines ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/factories/code_reviews.rb:8:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
spec/factories/code_reviews.rb:10:1: C: [Corrected] Layout/TrailingWhitespace: Trailing whitespace detected.
spec/factories/code_reviews.rb:11:1: C: [Corrected] Layout/ClosingHeredocIndentation: EOF is not aligned with text do <<~EOF.
EOF
^^^
spec/factories/code_reviews.rb:12:1: C: [Corrected] Layout/ClosingHeredocIndentation: EOF is not aligned with <<~EOF.
    EOF
^^^^^^^

1 file inspected, 8 offenses detected, 8 offenses corrected

Note that the indentation of non-empty lines is actually incorrect.

I can then replace #{' '} with an empty line and run again to get a much more reasonable result:

FactoryBot.define do
  factory :something do
    text do
      <<~EOF
        Long here doc with several empty lines

        bla

        bla
      EOF
    end
  end
end
Inspecting 1 file
C

Offenses:

spec/factories/code_reviews.rb:7:1: C: [Corrected] Layout/HeredocIndentation: Use 2 spaces for indentation in a heredoc.
                Long here doc with several empty lines ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected

The 2 are indentical because <<~ vs <<- but the latter is definitely more readable.

Expected behavior

I would like the second result automatically.

RuboCop version

Include the output of rubocop -V or bundle exec rubocop -V if using Bundler.
If you see extension cop versions (e.g. rubocop-performance, rubocop-rspec, and others)
output by rubocop -V, include them as well. Here's an example:

$ [bundle exec] rubocop -V
1.18.3 (using Parser 3.0.2.0, rubocop-ast 1.7.0, running on ruby 2.6.6 x86_64-linux)
  - rubocop-rails 2.11.3
  - rubocop-rspec 2.4.0
@jonas054
Copy link
Collaborator

What happens here is that Layout/HeredocIndentation, when it changes the indentation of the lines in the heredoc, also adds spaces on the empty lines. This is unnecessary, but it doesn't change the semantics of the code. The resulting heredoc string has the same value as before.

Then Layout/TrailingWhitespace comes in and inserts the string interpolation markers on the whitespace lines, and this is a bug. It changes the string values.

I'll try to fix it.

@jonas054 jonas054 self-assigned this Jul 23, 2021
@jonas054 jonas054 added the bug label Jul 23, 2021
@koic koic closed this as completed in fa5ad0f Jul 24, 2021
koic added a commit that referenced this issue Jul 24, 2021
[Fix #9945] Fix heredoc indentation in trailing space corrections
@thisismydesign
Copy link
Author

Thanks again @jonas054 much appreciated!

@schmijos
Copy link
Contributor

schmijos commented Dec 1, 2021

Wouldn't you rather write this anyways?

FactoryBot.define do
  factory :something do
    text { <<~EOF }
              Long here doc with several empty lines
      #{'        '}
              bla
      #{'        '}
              bla
    EOF
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants