Skip to content

Commit

Permalink
[Fix rubocop#118] Fix a false positive for DeletePrefix and `Delete…
Browse files Browse the repository at this point in the history
…Prefix` cops

Fixes rubocop#118.

Fix a false positive for `Performance/DeletePrefix` and `Performance/DeletePrefix` cops
when using heredoc.
  • Loading branch information
koic committed May 25, 2020
1 parent 6dc7ba7 commit 7a499d8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#111](/~https://github.com/rubocop-hq/rubocop-performance/issues/111): Fix an error for `Performance/DeletePrefix` and `Performance/DeleteSuffix` cops when using autocorrection with RuboCop 0.81 or lower. ([@koic][])
* [#118](/~https://github.com/rubocop-hq/rubocop-performance/issues/118): Fix a false positive for `Performance/DeletePrefix` and `Performance/DeleteSuffix` cops when using heredoc. ([@koic][])

## 1.6.0 (2020-05-22)

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/delete_prefix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DeletePrefix < Cop
def on_send(node)
gsub_method?(node) do |_, bad_method, _, replace_string|
return unless replace_string.blank?
return if node.receiver.respond_to?(:heredoc?) && node.receiver.heredoc?

good_method = PREFERRED_METHODS[bad_method]

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/performance/delete_suffix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class DeleteSuffix < Cop
def on_send(node)
gsub_method?(node) do |_, bad_method, _, replace_string|
return unless replace_string.blank?
return if node.receiver.respond_to?(:heredoc?) && node.receiver.heredoc?

good_method = PREFERRED_METHODS[bad_method]

Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/performance/delete_prefix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@
end
end

context 'when using heredoc' do
it 'does not register an offense when using `\A` as starting pattern' do
expect_no_offenses(<<~RUBY)
puts <<~HEREDOC.gsub(/\\A /, '')
foo
bar
HEREDOC
RUBY
end

it 'does not register an offense when using `^` as starting pattern' do
expect_no_offenses(<<~RUBY)
puts <<~HEREDOC.gsub(/^ /, '')
foo
bar
HEREDOC
RUBY
end
end

context 'when using a non-blank string as replacement string' do
it 'does not register an offense and corrects when using `gsub`' do
expect_no_offenses(<<~RUBY)
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cop/performance/delete_suffix_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@
end
end

context 'when using heredoc' do
it 'does not register an offense when using `\z` as ending pattern' do
expect_no_offenses(<<~RUBY)
puts <<~HEREDOC.gsub(/ \\z/, '')
foo
bar
HEREDOC
RUBY
end

it 'does not register an offense when using `$` as ending pattern' do
expect_no_offenses(<<~RUBY)
puts <<~HEREDOC.gsub(/ $/, '')
foo
bar
HEREDOC
RUBY
end
end

context 'when using a non-blank string as replacement string' do
it 'does not register an offense and corrects when using `gsub`' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 7a499d8

Please sign in to comment.