forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes rubocop#704. Enforces the use of squiggly heredoc over `strip_heredoc`. ```ruby # bad <<EOS.strip_heredoc some text EOS # bad <<-EOS.strip_heredoc some text EOS # good <<~EOS some text EOS ```
- Loading branch information
Showing
5 changed files
with
162 additions
and
0 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 @@ | ||
* [#704](https://github.com/rubocop/rubocop-rails/issues/704): Add new `Rails/StripHeredoc` 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Enforces the use of squiggly heredoc over `strip_heredoc`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# <<EOS.strip_heredoc | ||
# some text | ||
# EOS | ||
# | ||
# # bad | ||
# <<-EOS.strip_heredoc | ||
# some text | ||
# EOS | ||
# | ||
# # good | ||
# <<~EOS | ||
# some text | ||
# EOS | ||
# | ||
class StripHeredoc < Base | ||
extend AutoCorrector | ||
extend TargetRubyVersion | ||
|
||
MSG = 'Use squiggly heredoc (`<<~`) instead of `strip_heredoc`.' | ||
|
||
minimum_target_ruby_version 2.3 | ||
|
||
def on_send(node) | ||
return unless node.method?(:strip_heredoc) | ||
return unless (receiver = node.receiver) | ||
return unless receiver.str_type? || receiver.dstr_type? | ||
return unless receiver.respond_to?(:heredoc?) && receiver.heredoc? | ||
|
||
register_offense(node, receiver) | ||
end | ||
|
||
private | ||
|
||
def register_offense(node, heredoc_identifier) | ||
add_offense(node) do |corrector| | ||
replacement = heredoc_identifier.source.gsub(/\A<<-?/, '<<~') | ||
|
||
corrector.replace(heredoc_identifier, replacement) | ||
corrector.remove(node.loc.dot) | ||
corrector.remove(node.loc.selector) | ||
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,99 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::StripHeredoc, :config do | ||
context 'Ruby <= 2.2', :ruby22 do | ||
it 'does not register an offense when using `strip_heredoc`' do | ||
expect_no_offenses(<<~RUBY) | ||
<<-EOS.strip_heredoc | ||
some text | ||
EOS | ||
RUBY | ||
end | ||
end | ||
|
||
context 'Ruby >= 2.3', :ruby23 do | ||
it 'registers an offense when using `strip_heredoc` with `<<`' do | ||
expect_offense(<<~RUBY) | ||
<<EOS.strip_heredoc | ||
^^^^^^^^^^^^^^^^^^^ Use squiggly heredoc (`<<~`) instead of `strip_heredoc`. | ||
some text | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
<<~EOS | ||
some text | ||
EOS | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `strip_heredoc` with `<<-`' do | ||
expect_offense(<<~RUBY) | ||
<<-EOS.strip_heredoc | ||
^^^^^^^^^^^^^^^^^^^^ Use squiggly heredoc (`<<~`) instead of `strip_heredoc`. | ||
some text | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
<<~EOS | ||
some text | ||
EOS | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `strip_heredoc.do_something`' do | ||
expect_offense(<<~RUBY) | ||
<<-EOS.strip_heredoc.do_something | ||
^^^^^^^^^^^^^^^^^^^^ Use squiggly heredoc (`<<~`) instead of `strip_heredoc`. | ||
some text | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
<<~EOS.do_something | ||
some text | ||
EOS | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `strip_heredoc` with multiline text' do | ||
expect_offense(<<~RUBY) | ||
<<-EOS.strip_heredoc | ||
^^^^^^^^^^^^^^^^^^^^ Use squiggly heredoc (`<<~`) instead of `strip_heredoc`. | ||
some text | ||
some text | ||
EOS | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
<<~EOS | ||
some text | ||
some text | ||
EOS | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using squiggly heredoc' do | ||
expect_no_offenses(<<~RUBY) | ||
<<~EOS | ||
some text | ||
EOS | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `do_something.strip_heredoc`' do | ||
expect_no_offenses(<<~RUBY) | ||
<<-EOS.do_something.strip_heredoc | ||
some text | ||
EOS | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `strip_heredoc` without receiver' do | ||
expect_no_offenses(<<~RUBY) | ||
strip_heredoc | ||
RUBY | ||
end | ||
end | ||
end |