Skip to content

Commit

Permalink
Add new InternalAffairs/RedundantExpectOffenseArguments cop
Browse files Browse the repository at this point in the history
This PR adds new `InternalAffairs/RedundantExpectOffenseArguments` cop,
which checks for redundant arguments of `RuboCop::RSpec::ExpectOffense`'s methods.

```ruby
# bad
expect_no_offenses('code', keyword: keyword)

# good
expect_no_offenses('code')
```
  • Loading branch information
koic authored and bbatsov committed Feb 13, 2024
1 parent 1acdaef commit d8d8474
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/rubocop/cop/internal_affairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require_relative 'internal_affairs/processed_source_buffer_name'
require_relative 'internal_affairs/redundant_context_config_parameter'
require_relative 'internal_affairs/redundant_described_class_as_subject'
require_relative 'internal_affairs/redundant_expect_offense_arguments'
require_relative 'internal_affairs/redundant_let_rubocop_config_new'
require_relative 'internal_affairs/redundant_location_argument'
require_relative 'internal_affairs/redundant_message_argument'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module RuboCop
module Cop
module InternalAffairs
# Checks for redundant arguments of `RuboCop::RSpec::ExpectOffense`'s methods.
#
# @example
#
# # bad
# expect_no_offenses('code', keyword: keyword)
#
# # good
# expect_no_offenses('code')
#
class RedundantExpectOffenseArguments < Base
extend AutoCorrector

MSG = 'Remove the redundant arguments.'
RESTRICT_ON_SEND = %i[expect_no_offenses].freeze

def on_send(node)
return if node.arguments.one? || !node.arguments[1]&.hash_type?

range = node.first_argument.source_range.end.join(node.last_argument.source_range.end)

add_offense(range) do |corrector|
corrector.remove(range)
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true

RSpec.describe RuboCop::Cop::InternalAffairs::RedundantExpectOffenseArguments, :config do
it 'registers an offense when using `expect_no_offenses` with string and single keyword arguments' do
expect_offense(<<~RUBY)
expect_no_offenses('code', keyword: keyword)
^^^^^^^^^^^^^^^^^^ Remove the redundant arguments.
RUBY

expect_correction(<<~RUBY)
expect_no_offenses('code')
RUBY
end

it 'registers an offense when using `expect_no_offenses` with heredoc and single keyword arguments' do
expect_offense(<<~RUBY)
expect_no_offenses(<<~CODE, keyword: keyword)
^^^^^^^^^^^^^^^^^^ Remove the redundant arguments.
CODE
RUBY

expect_correction(<<~RUBY)
expect_no_offenses(<<~CODE)
CODE
RUBY
end

it 'registers an offense when using `expect_no_offenses` with heredoc multiple keyword arguments' do
expect_offense(<<~RUBY)
expect_no_offenses(<<~CODE, foo: foo, bar: bar)
^^^^^^^^^^^^^^^^^^^^ Remove the redundant arguments.
CODE
RUBY

expect_correction(<<~RUBY)
expect_no_offenses(<<~CODE)
CODE
RUBY
end

it 'does not register an offense when using `expect_no_offenses` with string argument only' do
expect_no_offenses(<<~RUBY)
expect_no_offenses('code')
RUBY
end

it 'does not register an offense when using `expect_no_offenses` with heredoc argument only' do
expect_no_offenses(<<~RUBY)
expect_no_offenses(<<~CODE)
CODE
RUBY
end

it 'does not register an offense when using `expect_no_offenses` with string and positional arguments' do
expect_no_offenses(<<~RUBY)
expect_no_offenses('code', file)
RUBY
end

it 'does not register an offense when using `expect_no_offenses` with heredoc and positional arguments' do
expect_no_offenses(<<~RUBY)
expect_no_offenses(<<~CODE, file)
CODE
RUBY
end

it 'does not crash when using `expect_no_offenses` with no arguments' do
expect_no_offenses(<<~RUBY)
expect_no_offenses
CODE
RUBY
end
end
2 changes: 1 addition & 1 deletion spec/rubocop/cop/lint/literal_as_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

context '>= Ruby 2.7', :ruby27 do
it "accepts an offense for literal #{lit} in case match with a match var" do
expect_no_offenses(<<~RUBY, lit: lit)
expect_no_offenses(<<~RUBY)
case %{lit}
in x then top
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/lint/non_atomic_file_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@

%i[rm_r rmtree].each do |remove_method|
it 'does not register an offense when use `FileTest.exist?` before remove recursive file' do
expect_no_offenses(<<~RUBY, remove_method: remove_method)
expect_no_offenses(<<~RUBY)
if FileTest.exist?(path)
FileUtils.#{remove_method}(path)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/lint/unmodified_reduce_accumulator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,13 @@

context 'argument count' do
it 'ignores when there are not enough block arguments' do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
(1..4).#{method}(0) { |acc| acc.foo }
RUBY
end

it 'ignores when there is a splat argument' do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
values.#{method}(0) { |*x| x[0] + x[1] }
RUBY
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def foo; end
end

it 'does not register an offense when using #{access_modifier} in a block' do
expect_no_offenses(<<~RUBY, access_modifier: access_modifier)
expect_no_offenses(<<~RUBY)
module MyModule
singleton_methods.each { |method| #{access_modifier}(method) }
end
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/style/format_string_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
end
else
it 'does not register offenses for annotated style' do
expect_no_offenses(<<~RUBY, annotated: annotated, template: template)
expect_no_offenses(<<~RUBY)
<<-HEREDOC
foo %{template} + bar %{annotated}
HEREDOC
Expand All @@ -166,7 +166,7 @@
"a\#{b}#{template} c\#{d}#{annotated_to_template} e\#{f}"
RUBY
else
expect_no_offenses(<<~'RUBY', annotated: annotated, template: template)
expect_no_offenses(<<~'RUBY')
"a#{b}%{template} c#{d}%{annotated} e#{f}"
RUBY
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/map_to_hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@

context "`#{method}` without `to_h`" do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
foo.#{method} { |x| x * 2 }
RUBY
end
Expand Down
2 changes: 1 addition & 1 deletion spec/rubocop/cop/style/map_to_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@

context "`#{method}` without `to_set`" do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
foo.#{method} { |x| x * 2 }
RUBY
end
Expand Down
8 changes: 4 additions & 4 deletions spec/rubocop/cop/style/symbol_proc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
end

it "does not register an offense when receiver is a hash literal and using `#{method}` with a block" do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
{foo: 42}.#{method} {|item| item.foo }
RUBY
end
Expand All @@ -193,7 +193,7 @@
end

it "does not register an offense when receiver is a array literal and using `#{method}` with a block" do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
[1, 2, 3].#{method} {|item| item.foo }
RUBY
end
Expand Down Expand Up @@ -372,7 +372,7 @@
end

it "does not register an offense when receiver is a hash literal and using `#{method}` with a numblock" do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
{foo: 42}.#{method} { _1.foo }
RUBY
end
Expand All @@ -391,7 +391,7 @@
end

it "does not register an offense when receiver is a array literal and using `#{method}` with a numblock" do
expect_no_offenses(<<~RUBY, method: method)
expect_no_offenses(<<~RUBY)
[1, 2, 3].#{method} { _1.foo }
RUBY
end
Expand Down

0 comments on commit d8d8474

Please sign in to comment.