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

Strange behavior of the auto-correction for Layout/TrailingWhitespace #9392

Closed
khiav223577 opened this issue Jan 19, 2021 · 2 comments
Closed

Comments

@khiav223577
Copy link
Contributor

Expected behavior

Expect trailing whitespaces to be removed.

Actual behavior

It is replaced with #{' '}

Steps to reproduce the problem

Add example.rb:

example.rb

class Example
end

Example.module_eval(<<-CODE, __FILE__, __LINE__ + 1)
  def self.test
    p 'test' 
  end
CODE

Example.test

Run auto-correcting:

$ rubocop example.rb --auto-correct --only Layout/TrailingWhitespace

example.rb (auto-corrected)

class Example
end

Example.module_eval(<<-CODE, __FILE__, __LINE__ + 1)
  def self.test
    p 'test'#{' '}
  end
CODE

Example.test

RuboCop version

$ rubocop -V
warning: parser/current is loading parser/ruby25, which recognizes
warning: 2.5.8-compliant syntax, but you are running 2.5.5.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1.8.1 (using Parser 3.0.0.0, rubocop-ast 1.3.0, running on ruby 2.5.5 x86_64-linux)
@dvandersluis
Copy link
Member

The cop adds that on purpose in heredocs (see #8914). @marcandre might have more insight as to why?

@marcandre
Copy link
Contributor

marcandre commented Jan 19, 2021

Indeed, this is by design.

The idea is that this Cop is safe and (thus will auto-correct without introducing a bug).

There is no way for RuboCop to know if you intended the trailing whitespace in the heredoc or not; the cop assume you did, and the correction makes it possible to see it. You are of course welcome to modify it and remove it altogether.

It also makes it easier for people like me that have their editors set to automatically remove trailing whitespace 😆

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

No branches or pull requests

3 participants