Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Checks for using empty heredoc to reduce redundancy. ```ruby # bad <<~EOS EOS <<-EOS EOS <<EOS EOS # good '' # bad do_something(<<~EOS) EOS do_something(<<-EOS) EOS do_something(<<EOS) EOS # good do_something('') ``` ## Other Information I noticed the following redundancy in the RuboCop test code: ```ruby expect_correction(<<~RUBY) RUBY ``` This cop solves it generically.
- Loading branch information
Showing
6 changed files
with
141 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#10820](https://github.com/rubocop/rubocop/pull/10820): Add new `Style/EmptyHeredoc` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for using empty heredoc to reduce redundancy. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# <<~EOS | ||
# EOS | ||
# | ||
# <<-EOS | ||
# EOS | ||
# | ||
# <<EOS | ||
# EOS | ||
# | ||
# # good | ||
# '' | ||
# | ||
# # bad | ||
# do_something(<<~EOS) | ||
# EOS | ||
# | ||
# do_something(<<-EOS) | ||
# EOS | ||
# | ||
# do_something(<<EOS) | ||
# EOS | ||
# | ||
# # good | ||
# do_something('') | ||
# | ||
class EmptyHeredoc < Base | ||
include Heredoc | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Use an empty string literal instead of heredoc.' | ||
|
||
def on_heredoc(node) | ||
heredoc_body = node.loc.heredoc_body | ||
|
||
return unless heredoc_body.source.empty? | ||
|
||
add_offense(node) do |corrector| | ||
heredoc_end = node.loc.heredoc_end | ||
|
||
corrector.replace(node, "''") | ||
corrector.remove(range_by_whole_lines(heredoc_body, include_final_newline: true)) | ||
corrector.remove(range_by_whole_lines(heredoc_end, include_final_newline: true)) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::EmptyHeredoc, :config do | ||
it 'registers an offense when using empty `<<~EOS` heredoc' do | ||
expect_offense(<<~RUBY) | ||
<<~EOS | ||
^^^^^^ Use an empty string literal instead of heredoc. | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
'' | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using empty `<<-EOS` heredoc' do | ||
expect_offense(<<~RUBY) | ||
<<-EOS | ||
^^^^^^ Use an empty string literal instead of heredoc. | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
'' | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using empty `<<EOS` heredoc' do | ||
expect_offense(<<~RUBY) | ||
<<EOS | ||
^^^^^ Use an empty string literal instead of heredoc. | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
'' | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using empty heredoc single argument' do | ||
expect_offense(<<~RUBY) | ||
do_something(<<~EOS) | ||
^^^^^^ Use an empty string literal instead of heredoc. | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
do_something('') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using empty heredoc argument with other argument' do | ||
expect_offense(<<~RUBY) | ||
do_something(<<~EOS, arg) | ||
^^^^^^ Use an empty string literal instead of heredoc. | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
do_something('', arg) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using not empty heredoc' do | ||
expect_no_offenses(<<~RUBY) | ||
<<~EOS | ||
Hello. | ||
EOS | ||
RUBY | ||
end | ||
end |