-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Style/TrailingWhitespace marks offenses when whitespace is in a string #4517
Comments
Guys, I've looked into implementation of this cop. Wanted to fix this bug, but I have couple questions. Current implementation looks like this:
So it goes through each line of code. Each line of code is a string. To fix this bug I need to work with line of code as with node, right? Do you have any suggestions on proper implementation for this case? Btw, for this cop there is an example that says: it registers an offense for trailing whitespace in a heredoc string , so it was expected behavior for author of the cop. :) |
@smakagon: The high level strategy I would use might be:
Yes. The suggested behaviour needs a configuration option. 🙂 |
@Drenmi thanks for the answer. To store all heredoc line ranges, I need to figure out that it's a heredoc. I was struggling with that because from one side I need to work with code as with an array of strings, but from the other side I want to know if it's a heredoc. |
The |
@jonas054: Hm. That definitely makes it less straightforward than I envisioned. 🤔 |
@Drenmi I played around a little and tried the following: diff --git a/lib/rubocop/cop/layout/trailing_whitespace.rb b/lib/rubocop/cop/layout/trailing_whitespace.rb
index a662b5b..5d70a74 100644
--- a/lib/rubocop/cop/layout/trailing_whitespace.rb
+++ b/lib/rubocop/cop/layout/trailing_whitespace.rb
@@ -5,6 +5,7 @@ module RuboCop
module Layout
# This cop looks for trailing whitespace in the source code.
class TrailingWhitespace < Cop
+ include Heredoc
MSG = 'Trailing whitespace detected.'.freeze
def investigate(processed_source)
@@ -19,6 +20,11 @@ module RuboCop
end
end
+ def on_heredoc(node)
+ heredoc_body = node.loc.heredoc_body
+ offenses.reject! { |o| heredoc_body.overlaps?(o.location) }
+ end
+
def autocorrect(range)
->(corrector) { corrector.remove(range) }
end This is perhaps a bit ugly. We haven't used |
@reitzig Squiggly heredocs ( Add this to your configuration: AllCops:
TargetRubyVersion: 2.3 |
That makes sense. I'll have to see how to configure this for the "built-in" RuboCop in RubyMine. Thanks! For reference: there are issues on the RubyMine bugtracker about this, e.g. this. Amendment: Adding a |
Expected behavior
Writing a multiline string with trailing whitespace for exact formatting must not cause a
Style/TrailingWhitespace
offense because rubocop cannot know the intended use of a string.Actual behavior
Rubocop marks strings with trailing whitespace as a
Style/TrailingWhitespace
offenseSteps to reproduce the problem
Write t.rb:
run rubocop:
I do not expect the cops on lines 2 or 4 to appear.
RuboCop version
Include the output of
rubocop -V
. Here's an example:The text was updated successfully, but these errors were encountered: