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

Handling trailing whitespace in Heredoc #8692

Merged
merged 3 commits into from Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
* [#8882](https://github.com/rubocop-hq/rubocop/pull/8882): **(Potentially breaking)** RuboCop assumes that Cop classes do not define new `on_<type>` methods at runtime (e.g. via `extend` in `initialize`). ([@marcandre][])
* [#7966](https://github.com/rubocop-hq/rubocop/issues/7966): **(Breaking)** Enable all pending cops for RuboCop 1.0. ([@koic][])
* [#8490](https://github.com/rubocop-hq/rubocop/pull/8490): **(Breaking)** Change logic for cop department name computation. Cops inside deep namespaces (5 or more levels deep) now belong to departments with names that are calculated by joining module names starting from the third one with slashes as separators. For example, cop `Rubocop::Cop::Foo::Bar::Baz` now belongs to `Foo/Bar` department (previously it was `Bar`). ([@dsavochkin][])
* [#8692](https://github.com/rubocop-hq/rubocop/pull/8692): Default changed to disallow `Layout/TrailingWhitespace` in heredoc. ([@marcandre][])

## 0.93.1 (2020-10-12)

Expand Down Expand Up @@ -151,6 +152,7 @@
* [#8661](https://github.com/rubocop-hq/rubocop/pull/8661): Fix an incorrect auto-correct for `Style/MultilineTernaryOperator` when returning a multiline ternary operator expression. ([@koic][])
* [#8526](https://github.com/rubocop-hq/rubocop/pull/8526): Fix a false positive for `Style/CaseEquality` cop when the receiver is not a camel cased constant. ([@koic][])
* [#8673](https://github.com/rubocop-hq/rubocop/issues/8673): Fix the JSON parse error when specifying `--format=json` and `--stdin` options. ([@koic][])
* [#8692](https://github.com/rubocop-hq/rubocop/pull/8692): Fix `Layout/TrailingWhitespace` auto-correction in heredoc. ([@marcandre][])

### Changes

Expand Down
2 changes: 1 addition & 1 deletion config/default.yml
Expand Up @@ -1335,7 +1335,7 @@ Layout/TrailingWhitespace:
Enabled: true
VersionAdded: '0.49'
VersionChanged: '0.83'
AllowInHeredoc: true
AllowInHeredoc: false

#################### Lint ##################################
### Warnings
Expand Down
17 changes: 14 additions & 3 deletions docs/modules/ROOT/pages/cops_layout.adoc
Expand Up @@ -6360,7 +6360,7 @@ x = 0
x = 0
----

==== AllowInHeredoc: false
==== AllowInHeredoc: false (default)

[source,ruby]
----
Expand All @@ -6369,9 +6369,20 @@ x = 0
code = <<~RUBY
x = 0
RUBY

# ok
code = <<~RUBY
x = 0 #{}
RUBY

# good
trailing_whitespace = ' '
code = <<~RUBY
x = 0#{trailing_whitespace}
RUBY
----

==== AllowInHeredoc: true (default)
==== AllowInHeredoc: true

[source,ruby]
----
Expand All @@ -6388,7 +6399,7 @@ RUBY
| Name | Default value | Configurable values

| AllowInHeredoc
| `true`
| `false`
| Boolean
|===

Expand Down
50 changes: 37 additions & 13 deletions lib/rubocop/cop/layout/trailing_whitespace.rb
Expand Up @@ -14,14 +14,25 @@ module Layout
# # good
# x = 0
#
# @example AllowInHeredoc: false
# @example AllowInHeredoc: false (default)
# # The line in this example contains spaces after the 0.
# # bad
# code = <<~RUBY
# x = 0
# RUBY
#
# @example AllowInHeredoc: true (default)
# # ok
# code = <<~RUBY
# x = 0 #{}
# RUBY
#
# # good
# trailing_whitespace = ' '
# code = <<~RUBY
# x = 0#{trailing_whitespace}
# RUBY
#
# @example AllowInHeredoc: true
# # The line in this example contains spaces after the 0.
# # good
# code = <<~RUBY
Expand All @@ -35,36 +46,49 @@ class TrailingWhitespace < Base
MSG = 'Trailing whitespace detected.'

def on_new_investigation
heredoc_ranges = extract_heredoc_ranges(processed_source.ast)
@heredocs = extract_heredocs(processed_source.ast)
processed_source.lines.each_with_index do |line, index|
lineno = index + 1

next unless line.end_with?(' ', "\t")
next if skip_heredoc? && inside_heredoc?(heredoc_ranges, lineno)

range = offense_range(lineno, line)
add_offense(range) do |corrector|
process_line(line, index + 1)
end
end

private

def process_line(line, lineno)
heredoc = find_heredoc(lineno)
return if skip_heredoc? && heredoc

range = offense_range(lineno, line)
add_offense(range) do |corrector|
if heredoc
corrector.insert_after(range, '#{}') unless static?(heredoc) # rubocop:disable Lint/InterpolationCheck
else
corrector.remove(range)
end
end
end

private
def static?(heredoc)
heredoc.loc.expression.source.end_with? "'"
end

def skip_heredoc?
cop_config.fetch('AllowInHeredoc', false)
end

def inside_heredoc?(heredoc_ranges, line_number)
heredoc_ranges.any? { |r| r.include?(line_number) }
def find_heredoc(line_number)
@heredocs.each { |node, r| return node if r.include?(line_number) }
nil
end

def extract_heredoc_ranges(ast)
def extract_heredocs(ast)
return [] unless ast

ast.each_node(:str, :dstr, :xstr).select(&:heredoc?).map do |node|
body = node.location.heredoc_body
(body.first_line...body.last_line)
[node, body.first_line...body.last_line]
end
end

Expand Down
33 changes: 33 additions & 0 deletions spec/rubocop/cop/layout/trailing_whitespace_spec.rb
Expand Up @@ -89,4 +89,37 @@
expect(offenses.size).to eq(1)
end
end

context 'when `AllowInHeredoc` is set to false' do
let(:cop_config) { { 'AllowInHeredoc' => false } }

it 'corrects safely trailing whitespace in a heredoc string' do
expect_offense(<<~RUBY)
x = <<~EXAMPLE
has trailing#{trailing_whitespace}
^ Trailing whitespace detected.
no trailing
EXAMPLE
RUBY

expect_correction(<<~RUBY)
x = <<~EXAMPLE
has trailing \#{}
no trailing
EXAMPLE
RUBY
end

it 'does not correct trailing whitespace in a static heredoc string' do
expect_offense(<<~RUBY)
x = <<~'EXAMPLE'
has trailing#{trailing_whitespace}
^ Trailing whitespace detected.
no trailing
EXAMPLE
RUBY

expect_no_corrections
end
end
end